forked from GeeDotDee/ai-text-watermark-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.node.mjs
More file actions
73 lines (69 loc) · 2.73 KB
/
Copy pathcli.node.mjs
File metadata and controls
73 lines (69 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import assert from "node:assert/strict";
import test from "node:test";
import { execFile } from "node:child_process";
import { promisify } from "node:util";
import { writeFileSync, unlinkSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
const execFileAsync = promisify(execFile);
const cliPath = join(process.cwd(), "src/cli.mjs");
test("CLI handles default single text file", async () => {
const tmpFile = join(tmpdir(), `test-single-${Date.now()}.txt`);
writeFileSync(tmpFile, "hello\u200B world", "utf8");
try {
const { stdout } = await execFileAsync(process.execPath, [cliPath, tmpFile]);
const parsed = JSON.parse(stdout);
assert.equal(parsed.totalArtifacts, 1);
assert.equal(parsed.cleanedText, "hello world");
} finally {
unlinkSync(tmpFile);
}
});
test("CLI processes JSONL input in batch mode", async () => {
const tmpFile = join(tmpdir(), `test-batch-${Date.now()}.jsonl`);
const lines = [
JSON.stringify({ text: "clean line" }),
JSON.stringify({ text: "hidden\u200B artifact" }),
JSON.stringify("raw string with \u00AD soft hyphen")
];
writeFileSync(tmpFile, lines.join("\n") + "\n", "utf8");
try {
const { stdout } = await execFileAsync(process.execPath, [cliPath, tmpFile, "--jsonl"]);
const outputLines = stdout.trim().split("\n").map((l) => JSON.parse(l));
assert.equal(outputLines.length, 3);
assert.equal(outputLines[0].totalArtifacts, 0);
assert.equal(outputLines[1].totalArtifacts, 1);
assert.equal(outputLines[1].cleanedText, "hidden artifact");
assert.equal(outputLines[2].totalArtifacts, 1);
assert.equal(outputLines[2].cleanedText, "raw string with soft hyphen");
} finally {
unlinkSync(tmpFile);
}
});
test("CLI reports line errors on malformed JSONL records without crashing", async () => {
const tmpFile = join(tmpdir(), `test-malformed-${Date.now()}.jsonl`);
const lines = [
JSON.stringify({ text: "valid line 1" }),
"{ malformed json",
JSON.stringify({ otherField: 123 }),
JSON.stringify({ text: "valid line 4" })
];
writeFileSync(tmpFile, lines.join("\n"), "utf8");
try {
let errRes;
try {
await execFileAsync(process.execPath, [cliPath, tmpFile, "--jsonl"]);
} catch (err) {
errRes = err;
}
assert.ok(errRes, "Command should exit with non-zero status on malformed lines");
assert.match(errRes.stderr, /Error on line 2/);
assert.match(errRes.stderr, /Error on line 3/);
const outputLines = errRes.stdout.trim().split("\n").map((l) => JSON.parse(l));
assert.equal(outputLines.length, 2);
assert.equal(outputLines[0].cleanedText, "valid line 1");
assert.equal(outputLines[1].cleanedText, "valid line 4");
} finally {
unlinkSync(tmpFile);
}
});