-
-
Notifications
You must be signed in to change notification settings - Fork 409
fix(detector): add --source-mode to the bundled detect.js #340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
conorbronsdon
merged 2 commits into
conorbronsdon:main
from
ayduanzf-hub:fix/244-detect-source-mode
Sep 23, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| "use strict"; | ||
|
|
||
| // Parity between the bundled ai-writing-detector script and the root CLI | ||
| // (#244). Both load detector/patterns.js, but detect.js had drifted: it could | ||
| // not reach rendered-Markdown mode, so a Markdown file with YAML frontmatter | ||
| // scored "Minimal AI signals" through detect.js and "Clean" through the root | ||
| // CLI, and passing the flag threw an uncaught "unknown argument" stack trace. | ||
|
|
||
| const assert = require("assert"); | ||
| const { spawnSync } = require("child_process"); | ||
| const fs = require("fs"); | ||
| const os = require("os"); | ||
| const path = require("path"); | ||
|
|
||
| const ROOT_CLI = path.join(__dirname, "..", "bin", "avoid-ai-writing.js"); | ||
| const DETECT = path.join(__dirname, "..", "skills", "ai-writing-detector", "scripts", "detect.js"); | ||
|
|
||
| function run(cli, args) { | ||
| return spawnSync(process.execPath, [cli, ...args], { encoding: "utf8" }); | ||
| } | ||
|
|
||
| const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "aaw-detect-")); | ||
| const draft = path.join(tmp, "draft.md"); | ||
| fs.writeFileSync( | ||
| draft, | ||
| [ | ||
| "---", | ||
| "title: Delve into the vibrant landscape", | ||
| "description: A testament to seamless synergy", | ||
| "tags: [draft]", | ||
| "---", | ||
| "", | ||
| "The team met on Tuesday and agreed the next steps for the release.", | ||
| "", | ||
| ].join("\n"), | ||
| "utf8", | ||
| ); | ||
|
|
||
| // The same file and the same flags produce the same analysis through both entry | ||
| // points. The root CLI takes a positional path, detect.js takes --file; that | ||
| // difference is deliberate and stays. | ||
| // | ||
| // Every context the shared detector accepts is exercised here, because the | ||
| // bundled parser is a second copy of the root CLI's validation: a context the | ||
| // root CLI accepts but detect.js rejects is a parity break that only shows up | ||
| // on the invocation that uses it. | ||
| const CONTEXTS = ["general", "technical", "marketing", "personal"]; | ||
|
|
||
| for (const context of CONTEXTS) { | ||
| const rootOut = run(ROOT_CLI, ["--context", context, "--source-mode", "rendered-markdown", draft]); | ||
| assert.strictEqual(rootOut.status, 0, `root CLI --context ${context}: ${rootOut.stderr}`); | ||
| const detectOut = run(DETECT, ["--file", draft, "--context", context, "--source-mode", "rendered-markdown"]); | ||
| assert.strictEqual(detectOut.status, 0, `detect.js --context ${context}: ${detectOut.stderr}`); | ||
| assert.deepStrictEqual( | ||
| JSON.parse(detectOut.stdout), | ||
| JSON.parse(rootOut.stdout), | ||
| `--context ${context} must agree between the two entry points`, | ||
| ); | ||
| assert.strictEqual(JSON.parse(detectOut.stdout).stats.contextMode, context); | ||
| } | ||
|
|
||
| const detectOut = run(DETECT, ["--file", draft, "--context", "general", "--source-mode", "rendered-markdown"]); | ||
| assert.strictEqual(detectOut.status, 0, detectOut.stderr); | ||
| assert.strictEqual(JSON.parse(detectOut.stdout).stats.sourceMode, "rendered-markdown"); | ||
|
|
||
| // The flag is not a no-op: plain mode still scores the YAML frontmatter as | ||
| // prose, which is the false positive rendered-markdown removes. | ||
| const plainOut = run(DETECT, ["--file", draft, "--context", "general"]); | ||
| assert.strictEqual(plainOut.status, 0, plainOut.stderr); | ||
| const plain = JSON.parse(plainOut.stdout); | ||
| const rendered = JSON.parse(detectOut.stdout); | ||
| assert.strictEqual(plain.stats.sourceMode, "plain"); | ||
| assert.strictEqual(rendered.issues.length, 0, "rendered-markdown must not score the frontmatter"); | ||
| assert.strictEqual(rendered.label, "Clean"); | ||
| assert.ok( | ||
| plain.issues.length > 0, | ||
| "plain mode must still score the frontmatter, or this flag has nothing to remove", | ||
| ); | ||
|
|
||
| // Blank input still reports the selected modes. analyzeText() returns an empty | ||
| // stats object for it, and the root CLI fills in contextMode and sourceMode; | ||
| // without the same normalization the bundled script returned stats: {} and the | ||
| // two entry points disagreed on an explicitly selected mode. | ||
| const blank = path.join(tmp, "blank.md"); | ||
| fs.writeFileSync(blank, "", "utf8"); | ||
| const whitespace = path.join(tmp, "whitespace.md"); | ||
| fs.writeFileSync(whitespace, " \n\t\n", "utf8"); | ||
|
|
||
| for (const [label, blankFile] of [["empty", blank], ["whitespace-only", whitespace]]) { | ||
| for (const sourceMode of ["plain", "rendered-markdown"]) { | ||
| const rootOut = run(ROOT_CLI, ["--context", "marketing", "--source-mode", sourceMode, blankFile]); | ||
| assert.strictEqual(rootOut.status, 0, `root CLI ${label} ${sourceMode}: ${rootOut.stderr}`); | ||
| const detectOut = run(DETECT, ["--file", blankFile, "--context", "marketing", "--source-mode", sourceMode]); | ||
| assert.strictEqual(detectOut.status, 0, `detect.js ${label} ${sourceMode}: ${detectOut.stderr}`); | ||
| assert.deepStrictEqual( | ||
| JSON.parse(detectOut.stdout), | ||
| JSON.parse(rootOut.stdout), | ||
| `${label} input with --source-mode ${sourceMode} must agree between the two entry points`, | ||
| ); | ||
| const stats = JSON.parse(detectOut.stdout).stats; | ||
| assert.strictEqual(stats.contextMode, "marketing", `${label}: selected context must stay visible`); | ||
| assert.strictEqual(stats.sourceMode, sourceMode, `${label}: selected source mode must stay visible`); | ||
| } | ||
| } | ||
|
|
||
| // Usage errors exit 2 with the usage message and no stack trace. | ||
| const errorCases = [ | ||
| ["unknown argument", ["--nope"]], | ||
| ["missing --file value", ["--file"]], | ||
| ["missing --context value", ["--context"]], | ||
| ["invalid context", ["--context", "nope"]], | ||
| ["missing --source-mode value", ["--source-mode"]], | ||
| ["invalid source mode", ["--source-mode", "nope"]], | ||
| ]; | ||
|
|
||
| for (const [label, args] of errorCases) { | ||
| const res = run(DETECT, args); | ||
| assert.strictEqual(res.status, 2, `${label}: expected exit 2`); | ||
| assert.strictEqual(res.stdout, "", `${label}: expected no stdout`); | ||
| assert.ok(res.stderr.includes("Usage: detect.js"), `${label}: expected the usage message`); | ||
| assert.ok(!/\n\s+at /.test(res.stderr), `${label}: expected no stack trace`); | ||
| } | ||
|
|
||
| // --help prints the usage and exits 0. | ||
| const help = run(DETECT, ["--help"]); | ||
| assert.strictEqual(help.status, 0); | ||
| assert.ok(help.stdout.includes("Usage: detect.js")); | ||
| assert.ok(help.stdout.includes("--source-mode <plain|rendered-markdown>")); | ||
|
|
||
| fs.rmSync(tmp, { recursive: true, force: true }); | ||
| console.log("ai-writing-detector bundled script: ok"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.