import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import { resolve } from "node:path";
import { test } from "node:test";
import { build } from "esbuild";
import { compile } from "octane/compiler";
const file = resolve("src/components/skill-script-output.tsx");
const compiled = compile(await readFile(file, "utf8"), file, {
mode: "server",
});
const bundle = await build({
stdin: {
contents: `${compiled.code}\nexport { renderToStaticMarkup } from "octane/server";`,
resolveDir: resolve("src/components"),
loader: "ts",
},
bundle: true,
write: false,
platform: "node",
format: "esm",
target: "es2022",
});
const { skillScriptOutput, SkillScriptOutput, renderToStaticMarkup } =
await import(
`data:text/javascript;base64,${Buffer.from(bundle.outputFiles[0].text).toString("base64")}`
);
const output = () => ({
kind: "skill-script",
skill: {
source: "installed",
name: "report-helper",
version: "1.2.3",
fingerprint: "a".repeat(64),
},
path: "scripts/report.js",
result: {
ok: false,
result: null,
stdout: "\n雪🦊",
stderr: "Synthetic script failure",
exitCode: 1,
error: {
code: "script_failed",
message: "Skill script failed. ",
},
truncated: true,
},
workspace: {
files: [{ path: "result.txt", content: "Private file body" }],
truncated: true,
},
});
test("script output projects bounded display fields without walking arbitrary results or files", () => {
const value = output();
Object.defineProperty(value.result, "result", {
get() {
throw new Error("Arbitrary return value was inspected");
},
});
Object.defineProperty(value.workspace.files[0], "content", {
get() {
throw new Error("Workspace content was inspected");
},
});
const projected = skillScriptOutput(value);
assert.ok(projected);
assert.equal("result" in projected.result, false);
assert.deepEqual(projected.workspace.files, [{ path: "result.txt" }]);
for (const invalid of [
null,
{},
{ ...output(), kind: "mcp" },
{
...output(),
workspace: {
files: Array.from({ length: 9 }, () => ({ path: "x.txt" })),
truncated: false,
},
},
]) {
assert.equal(skillScriptOutput(invalid), undefined);
}
for (const text of [
"x".repeat(8193),
"\u0001".repeat(1400),
"🦊".repeat(2100),
]) {
const invalid = output();
invalid.result.stdout = text;
assert.equal(
skillScriptOutput(invalid),
undefined,
"escaped JSON bytes enforce the display budget",
);
}
const longError = output();
longError.result.error.message = "x".repeat(241);
assert.equal(skillScriptOutput(longError), undefined);
const boundary = output();
boundary.result.stdout = "x".repeat(8190);
assert.ok(skillScriptOutput(boundary));
});
test("compiled Skill output is collapsed, shows exit status and renders script text inertly", () => {
const value = skillScriptOutput(output());
const { html } = renderToStaticMarkup(SkillScriptOutput, { output: value });
assert.match(html, /]*>/);
assert.doesNotMatch(html, /]*\bopen\b/);
assert.match(html, /Exit status 1/);
assert.match(html, /Output truncated/);
assert.match(html, /<script>/);
assert.doesNotMatch(html, /