Files
oh-my-pi/packages/coding-agent/test/tools/eval-format-javascript.test.ts
T
can1357 ff49b986d5 feat(coding-agent/tools): introduced language-specific code formatters for display rendering
- Added language-specific code formatters for JavaScript, Julia, Python, and Ruby to improve display rendering.
- Integrated display formatting into browser run and eval render tools while preserving verbatim execution.
- Added comprehensive test suites verifying formatting stability, lexical safety, and streaming behavior.
2026-07-24 16:26:03 +02:00

100 lines
3.3 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import { formatJavaScriptForDisplay } from "@oh-my-pi/pi-coding-agent/tools/eval-format/javascript";
describe("formatJavaScriptForDisplay", () => {
it("expands compact control flow, objects, and arrays", () => {
expect(formatJavaScriptForDisplay("if (ready){const item = { value: 1 };use(item);}else{fallback();}")).toBe(
[
"if (ready) {",
" const item = {",
" value: 1",
" };",
" use(item);",
"} else {",
" fallback();",
"}",
].join("\n"),
);
expect(formatJavaScriptForDisplay("const rows = [{ value: 1 },{ value: 2 }];")).toBe(
[
"const rows = [{",
" value: 1",
"}, {",
" value: 2",
"}];",
].join("\n"),
);
});
it("keeps for-loop header semicolons inline", () => {
expect(formatJavaScriptForDisplay("for(;;){tick();}for(let i=0;i<2;i++){work(i);}")).toBe(
[
"for (;;) {",
" tick();",
"}",
"for (let i=0; i<2; i++) {",
" work(i);",
"}",
].join("\n"),
);
});
it("does not split literals, templates, regexes, or comments", () => {
const doubleQuoted = String.raw`"a\";{b}"`;
const singleQuoted = String.raw`'a\';{b}'`;
const template = '`raw;{${fn({ value: "}" })}}`';
const regex = "/[;{}]+/g";
const lineComment = "// keep ; { }";
const blockComment = "/* keep ; { } */";
const source = `const double = ${doubleQuoted};const single = ${singleQuoted};const template = ${template};const regex = ${regex}; ${lineComment}\n${blockComment}done();`;
expect(formatJavaScriptForDisplay(source)).toBe(
[
`const double = ${doubleQuoted};`,
`const single = ${singleQuoted};`,
`const template = ${template};`,
`const regex = ${regex}; ${lineComment}`,
`${blockComment}done();`,
].join("\n"),
);
});
it("returns unfinished literals, comments, and blocks without inventing closers", () => {
const samples: Array<{ source: string; expected: string }> = [
{ source: "const value = `raw;${call({ x: 1", expected: "const value = `raw;${call({ x: 1" },
{ source: "/* unfinished ; {", expected: "/* unfinished ; {" },
{ source: "// unfinished ; {", expected: "// unfinished ; {" },
{ source: "const pattern = /[;{]", expected: "const pattern = /[;{]" },
{ source: "if (ready){work();", expected: "if (ready) {\n work();" },
];
for (const sample of samples) {
expect(() => formatJavaScriptForDisplay(sample.source)).not.toThrow();
expect(formatJavaScriptForDisplay(sample.source)).toBe(sample.expected);
}
});
it("is idempotent", () => {
const source = "try{const result = { ok: true };use(result);}catch(error){report(error);}finally{cleanup();}";
const formatted = formatJavaScriptForDisplay(source);
expect(formatJavaScriptForDisplay(formatted)).toBe(formatted);
});
it("never changes already committed lines while a prefix grows", () => {
const source = "if(flag){const value={text:`a;${item}`};run(value);}else{for(;;){tick();}}";
let committed: string[] = [];
for (let end = 1; end <= source.length; end++) {
let formatted = "";
expect(() => {
formatted = formatJavaScriptForDisplay(source.slice(0, end));
}).not.toThrow();
const lines = formatted.split("\n");
const nextCommitted = lines.slice(0, -1);
expect(nextCommitted.slice(0, committed.length)).toEqual(committed);
committed = nextCommitted;
}
});
});