11import assert from "node:assert/strict" ;
22import { readFileSync } from "node:fs" ;
33import test from "node:test" ;
4+ import MarkdownIt from "markdown-it" ;
45
56import {
67 canPatchReviewComment ,
@@ -28,6 +29,33 @@ import { createReportHelpers } from "../dist/clawsweeper-report-helpers.js";
2829import { normalizeRepo , repositoryProfileFor } from "../dist/repository-profiles.js" ;
2930import type { DecisionKind , Evidence } from "../dist/clawsweeper-types.js" ;
3031
32+ function markdownLinkDestinations ( markdown : string ) : Set < string > {
33+ const destinations = new Set < string > ( ) ;
34+ const parser = new MarkdownIt ( { html : true } ) ;
35+ for ( const token of parser . parse ( markdown , { } ) ) {
36+ if ( token . type !== "inline" ) continue ;
37+ for ( const child of token . children ?? [ ] ) {
38+ if ( child . type !== "link_open" ) continue ;
39+ const href = child . attrGet ( "href" ) ;
40+ if ( typeof href === "string" ) destinations . add ( href ) ;
41+ }
42+ }
43+ return destinations ;
44+ }
45+
46+ test ( "Markdown destination assertions reject prose and lookalike links inside details" , ( ) => {
47+ const expected = "https://docs.openclaw.ai/tools" ;
48+ const lookalike = "https://docs.openclaw.ai.invalid/tools" ;
49+ const misleading = `<details>\n<summary>Evidence</summary>\n\n${ expected } \n\n[${ expected } ](${ lookalike } )\n\n</details>` ;
50+ const destinations = markdownLinkDestinations ( misleading ) ;
51+ assert . deepEqual ( destinations , new Set ( [ lookalike ] ) ) ;
52+ assert . equal ( destinations . has ( expected ) , false ) ;
53+ assert . equal (
54+ markdownLinkDestinations ( misleading . replace ( lookalike , expected ) ) . has ( expected ) ,
55+ true ,
56+ ) ;
57+ } ) ;
58+
3159const evidenceLinks = createRepositoryLinks ( {
3260 reportRepo : "openclaw/clawsweeper-state" ,
3361 normalizeRepo,
@@ -162,23 +190,27 @@ test("repository evidence survives structured decision, report, parse and both c
162190 ) ;
163191 assert . doesNotMatch ( comment , / d i d n o t c o m p l e t e | i n f r a s t r u c t u r e f a i l u r e / ) ;
164192 for ( const output of [ report , comment ] ) {
165- assert . ok ( output . includes ( source ) , output ) ;
166- assert . ok ( output . includes ( commit ) , output ) ;
193+ const destinations = markdownLinkDestinations ( output ) ;
194+ assert . ok ( destinations . has ( source ) , output ) ;
195+ assert . ok ( destinations . has ( commit ) , output ) ;
167196 assert . ok (
168- output . includes (
197+ destinations . has (
169198 `https://github.com/openclaw/openclaw/blob/${ "b" . repeat ( 40 ) } /src/config.ts#L12` ,
170199 ) ,
171200 ) ;
172201 assert . ok (
173- output . includes (
202+ destinations . has (
174203 `https://github.com/openai/codex/blob/${ dependencyEvidence . sha } /docs/config.md` ,
175204 ) ,
176205 ) ;
177- assert . ok ( ! output . includes ( "https://docs.openclaw.ai/config" ) ) ;
178- assert . ok ( ! output . includes ( "openclaw/ openclaw/blob/78c290807ce7/codex-rs" ) ) ;
206+ assert . ok ( destinations . has ( visionUrl ) , output ) ;
207+ assert . equal ( destinations . has ( "https://docs. openclaw.ai/config" ) , false ) ;
179208 }
180- assert . ok ( comment . includes ( "https://docs.openclaw.ai/tools" ) ) ;
181- assert . ok ( comment . includes ( "https://github.com/openclaw/openclaw/blob/main/VISION.md" ) ) ;
209+ const commentDestinations = markdownLinkDestinations ( comment ) ;
210+ assert . ok ( commentDestinations . has ( "https://docs.openclaw.ai/tools" ) ) ;
211+ assert . ok (
212+ commentDestinations . has ( "https://github.com/openclaw/openclaw/blob/main/VISION.md" ) ,
213+ ) ;
182214 assert . ok ( comment . includes ( `[\`source.json\`](${ source } )` ) ) ;
183215 const visionReference = `[\`VISION.md\`](${ visionUrl } )` ;
184216 const linkedDetail = detail . includes ( "`VISION.md`" )
@@ -188,7 +220,10 @@ test("repository evidence survives structured decision, report, parse and both c
188220 comment . includes ( `- **dependency vision:** ${ linkedDetail } ` ) ,
189221 `${ kind } : ${ detail } \n${ comment } ` ,
190222 ) ;
191- assert . ok ( ! comment . includes ( "https://github.com/openai/codex/blob/main/VISION.md" ) ) ;
223+ assert . equal (
224+ commentDestinations . has ( "https://github.com/openai/codex/blob/main/VISION.md" ) ,
225+ false ,
226+ ) ;
192227 }
193228 }
194229} ) ;
@@ -216,10 +251,15 @@ test("explicit GitHub destinations preserve full identity and historical same-re
216251 explicit ,
217252 kind === "close" ? "implemented_on_main" : "none" ,
218253 ) ;
219- assert . ok ( comment . includes ( source ) ) ;
220- assert . ok ( comment . includes ( commit ) ) ;
221- assert . ok ( renderReviewCommentFromReport ( report , "none" ) . includes ( source ) ) ;
222- assert . ok ( renderReviewCommentFromReport ( legacy , "none" ) . includes ( `/commit/${ "a" . repeat ( 40 ) } ` ) ) ;
254+ const destinations = markdownLinkDestinations ( comment ) ;
255+ assert . ok ( destinations . has ( source ) ) ;
256+ assert . ok ( destinations . has ( commit ) ) ;
257+ assert . ok ( markdownLinkDestinations ( renderReviewCommentFromReport ( report , "none" ) ) . has ( source ) ) ;
258+ assert . ok (
259+ markdownLinkDestinations ( renderReviewCommentFromReport ( legacy , "none" ) ) . has (
260+ `https://github.com/openclaw/openclaw/commit/${ "a" . repeat ( 40 ) } ` ,
261+ ) ,
262+ ) ;
223263 }
224264} ) ;
225265
@@ -251,17 +291,23 @@ test("unresolved evidence and conflicting destinations never acquire target link
251291 . split ( "\n" )
252292 . find ( ( line ) => line . startsWith ( "- **dependency source:**" ) ) ;
253293 assert . ok ( evidence , comment ) ;
254- assert . doesNotMatch ( evidence , / \] \( h t t p s : \/ \/ g i t h u b \. c o m \/ / , JSON . stringify ( entry ) ) ;
294+ assert . equal ( markdownLinkDestinations ( evidence ) . size , 0 , JSON . stringify ( entry ) ) ;
255295 }
256296 }
257297 const missingSha = evidenceReport ( [
258298 { ...dependencyEvidence , sha : null , file : "docs/config.md" , line : null } ,
259299 ] ) ;
260- assert . doesNotMatch ( missingSha , / o p e n a i \/ c o d e x \/ b l o b \/ a { 40 } / ) ;
261- assert . doesNotMatch (
300+ for ( const output of [
301+ missingSha ,
262302 renderReviewCommentFromReport ( missingSha , "implemented_on_main" ) ,
263- / d o c s \. o p e n c l a w \. a i \/ c o n f i g / ,
264- ) ;
303+ ] ) {
304+ const destinations = markdownLinkDestinations ( output ) ;
305+ assert . equal (
306+ destinations . has ( `https://github.com/openai/codex/blob/${ "a" . repeat ( 40 ) } /docs/config.md` ) ,
307+ false ,
308+ ) ;
309+ assert . equal ( destinations . has ( "https://docs.openclaw.ai/config" ) , false ) ;
310+ }
265311 const target = evidenceReport ( [
266312 {
267313 ...dependencyEvidence ,
@@ -272,8 +318,8 @@ test("unresolved evidence and conflicting destinations never acquire target link
272318 } ,
273319 ] ) ;
274320 assert . ok (
275- renderReviewCommentFromReport ( target , "implemented_on_main" ) . includes (
276- `/blob/${ "a" . repeat ( 40 ) } /src/config.ts#L12` ,
321+ markdownLinkDestinations ( renderReviewCommentFromReport ( target , "implemented_on_main" ) ) . has (
322+ `https://github.com/openclaw/openclaw /blob/${ "a" . repeat ( 40 ) } /src/config.ts#L12` ,
277323 ) ,
278324 ) ;
279325} ) ;
0 commit comments