147 lines
6.0 KiB
TypeScript
147 lines
6.0 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import fc from "fast-check";
|
|
import {
|
|
MAX_EXPANDED_STEP_PROMPT_BYTES,
|
|
MAX_INTERPOLATED_STEP_OUTPUT_BYTES,
|
|
interpolateWorkflowTask,
|
|
joinWorkflowItemOutputs,
|
|
splitWorkflowItems,
|
|
} from "../src/workflow-run.ts";
|
|
|
|
// Independent of the exported constants so a changed budget fails the test.
|
|
const OUTPUT_BUDGET = 50 * 1024;
|
|
const PROMPT_BUDGET = 100 * 1024;
|
|
|
|
function context(args: Record<string, string> = {}, outputs: Record<string, string> = {}, item?: string) {
|
|
return { args, outputs: new Map(Object.entries(outputs)), item };
|
|
}
|
|
|
|
test("splitWorkflowItems keeps nonblank lines in order and trims their edges", () => {
|
|
assert.deepEqual(splitWorkflowItems(""), []);
|
|
assert.deepEqual(splitWorkflowItems(" \n\t\r\n"), []);
|
|
assert.deepEqual(splitWorkflowItems("src/a.ts\n\n src/b.ts \r\nsrc/c.ts\r\n"), ["src/a.ts", "src/b.ts", "src/c.ts"]);
|
|
assert.deepEqual(splitWorkflowItems("no trailing newline"), ["no trailing newline"]);
|
|
});
|
|
|
|
test("property: every split item is a trimmed, nonempty source line in source order", () => {
|
|
const lineArb = fc.string({ maxLength: 40 }).map((line) => line.replace(/[\r\n]/g, " "));
|
|
fc.assert(
|
|
fc.property(fc.array(lineArb, { maxLength: 30 }), fc.constantFrom("\n", "\r\n", "\r"), (lines, separator) => {
|
|
const blankPrefix = ["", " ", "\t", " \t "];
|
|
const source = lines
|
|
.map((line, index) => `${blankPrefix[index % blankPrefix.length]}${line}${blankPrefix[(index + 1) % blankPrefix.length]}`)
|
|
.join(separator);
|
|
const expected = lines.map((line) => line.trim()).filter((line) => line.length > 0);
|
|
assert.deepEqual(splitWorkflowItems(source), expected);
|
|
}),
|
|
{ numRuns: 100 },
|
|
);
|
|
});
|
|
|
|
test("an each-step item substitutes {item} in a single pass, never re-expanding it", () => {
|
|
const template = "Audit {item} for {args.glob}; again {item}.";
|
|
const item = "src/{item}.ts {args.glob} {steps.scan.output}";
|
|
assert.equal(
|
|
interpolateWorkflowTask(template, context({ glob: "auth" }, {}, item)),
|
|
`Audit ${item} for auth; again ${item}.`,
|
|
);
|
|
// Non-each steps keep {item} literal, as before.
|
|
assert.equal(interpolateWorkflowTask("{item}", context()), "{item}");
|
|
assert.equal(interpolateWorkflowTask("no placeholder", context({}, {}, "x")), "no placeholder");
|
|
});
|
|
|
|
test("the expanded item prompt is bounded to the prompt budget with a visible marker", () => {
|
|
const piece = "x".repeat(PROMPT_BUDGET * 2);
|
|
const result = interpolateWorkflowTask("{item}", context({}, {}, piece));
|
|
const [prefix, note] = result.split("\n\n[Prompt truncated: ");
|
|
|
|
assert.ok(Buffer.byteLength(prefix, "utf8") <= PROMPT_BUDGET);
|
|
assert.equal(prefix.includes("\uFFFD"), false);
|
|
assert.match(note ?? "", /^\d+ bytes omitted\]$/);
|
|
assert.ok(piece.startsWith(prefix));
|
|
});
|
|
|
|
test("the joined result is one header-and-output section per item in item order", () => {
|
|
assert.equal(joinWorkflowItemOutputs([]), "");
|
|
assert.equal(
|
|
joinWorkflowItemOutputs([
|
|
{ item: "src/a.ts", output: "missing auth" },
|
|
{ item: "src/b.ts", output: "" },
|
|
{ item: "src/c.ts", output: "clean" },
|
|
]),
|
|
"### src/a.ts\n\nmissing auth\n\n### src/b.ts\n\n\n\n### src/c.ts\n\nclean",
|
|
);
|
|
});
|
|
|
|
test("property: a joined result stays inside the output budget and keeps every item in order", () => {
|
|
const itemArb = fc.stringMatching(/^[A-Za-z0-9_./ -]{0,20}$/).filter((item) => item.trim().length > 0);
|
|
const outputArb = fc.string({ maxLength: 2000 });
|
|
fc.assert(
|
|
fc.property(fc.array(fc.record({ item: itemArb, output: outputArb }), { maxLength: 20 }), (items) => {
|
|
const joined = joinWorkflowItemOutputs(items, 4096);
|
|
assert.ok(Buffer.byteLength(joined, "utf8") <= 4096);
|
|
assert.equal(joined.includes("\uFFFD"), false);
|
|
let cursor = 0;
|
|
for (const { item } of items) {
|
|
const header = `### ${item}`;
|
|
const index = joined.indexOf(header, cursor);
|
|
assert.notEqual(index, -1, `missing header for ${JSON.stringify(item)} in ${JSON.stringify(joined)}`);
|
|
cursor = index + header.length;
|
|
}
|
|
}),
|
|
{ numRuns: 100 },
|
|
);
|
|
});
|
|
|
|
test("property: unicode outputs are truncated on UTF-8 boundaries and never exceed the budget", () => {
|
|
const outputArb = fc.array(fc.constantFrom("a", "é", "😀", "中", "\n"), { maxLength: 3000 }).map((parts) => parts.join(""));
|
|
fc.assert(
|
|
fc.property(fc.array(outputArb, { minLength: 1, maxLength: 12 }), (outputs) => {
|
|
const joined = joinWorkflowItemOutputs(outputs.map((output, index) => ({ item: `item-${index}`, output })), 2048);
|
|
assert.ok(Buffer.byteLength(joined, "utf8") <= 2048);
|
|
assert.equal(joined.includes("\uFFFD"), false);
|
|
for (const [index] of outputs.entries()) {
|
|
assert.ok(joined.includes(`### item-${index}`));
|
|
}
|
|
}),
|
|
{ numRuns: 100 },
|
|
);
|
|
});
|
|
|
|
test("one giant item cannot crowd out the other items' fair share", () => {
|
|
const giant = "x".repeat(OUTPUT_BUDGET * 4);
|
|
const joined = joinWorkflowItemOutputs(
|
|
[
|
|
{ item: "giant", output: giant },
|
|
{ item: "small-1", output: "first finding" },
|
|
{ item: "small-2", output: "second finding" },
|
|
],
|
|
OUTPUT_BUDGET,
|
|
);
|
|
|
|
assert.ok(Buffer.byteLength(joined, "utf8") <= OUTPUT_BUDGET);
|
|
assert.ok(joined.includes("### giant\n\n"));
|
|
assert.ok(joined.includes("### small-1\n\nfirst finding"));
|
|
assert.ok(joined.includes("### small-2\n\nsecond finding"));
|
|
assert.match(joined, /\[Output truncated: \d+ bytes omitted\]/);
|
|
assert.equal(joined.indexOf("### giant"), 0);
|
|
assert.ok(joined.indexOf("### small-1") > joined.indexOf("### giant"));
|
|
assert.ok(joined.indexOf("### small-2") > joined.indexOf("### small-1"));
|
|
});
|
|
|
|
test("failed items leave empty slots without disturbing their neighbours", () => {
|
|
const joined = joinWorkflowItemOutputs([
|
|
{ item: "ok-1", output: "kept" },
|
|
{ item: "failed", output: "" },
|
|
{ item: "ok-2", output: "also kept" },
|
|
]);
|
|
assert.equal(joined, "### ok-1\n\nkept\n\n### failed\n\n\n\n### ok-2\n\nalso kept");
|
|
assert.equal(joined.includes("failed\n\nkept"), false);
|
|
});
|
|
|
|
test("the pure budgets match the exported constants", () => {
|
|
assert.equal(MAX_INTERPOLATED_STEP_OUTPUT_BYTES, OUTPUT_BUDGET);
|
|
assert.equal(MAX_EXPANDED_STEP_PROMPT_BYTES, PROMPT_BUDGET);
|
|
});
|