Files
oh-my-pi/packages/coding-agent/test/export-subsessions.test.ts
T
can1357 162c9ca422 feat(coding-agent): sealed /share behind encrypted links and embedded subagent transcripts in exports
- Added `src/export/share.ts`: `/share` now snapshots the session JSON, gzips and seals it with a fresh AES-256-GCM key, and pushes the blob to a secret gist or the share server (1 MB cap with image/string/entry truncation via `sealToFit`); links are `<serverUrl>/<id>#<key>` with the key only in the fragment.
- Added `share-loader.js` and `scripts/generate-share-viewer.ts` building the static viewer the relay serves at `GET /s/<id>`: it fetches the sealed blob, decrypts in-browser, and hands the JSON to the export template via `window.__OMP_SESSION_DATA__`.
- Reworked the `/share` command in `command-controller.ts`/`builtin-registry.ts` off the plaintext-gist HTML upload, exported `LoadedCustomShare`, and exposed the session `SecretObfuscator` getter on `AgentSession` for redaction.
- Added `share.serverUrl` and `share.redactSecrets` settings backed by `DEFAULT_SHARE_URL` from pi-wire.
- HTML exports now embed subagent transcripts: `collectSubSessions` walks `<session>/<AgentId>.jsonl` recursively into `SessionData.subSessions`, with `includeSubSessions` opt-out and the exported `buildSessionData` reused by share snapshots.
- Added `share.test.ts` (snapshot/seal/server-url contracts) and `export-subsessions.test.ts`.
2026-06-12 15:00:41 +02:00

79 lines
2.9 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import * as fs from "node:fs/promises";
import * as os from "node:os";
import * as path from "node:path";
import { collectSubSessions } from "../src/export/html";
/**
* Contract: a session at `<dir>/<name>.jsonl` embeds subagent transcripts from
* `<dir>/<name>/<AgentId>.jsonl` (recursively) under slash-joined keys, with
* parent links and last-entry leaf ids. Corrupt/empty/backup files are skipped.
*/
function sessionJsonl(id: string, entryIds: string[]): string {
const lines = [
JSON.stringify({ type: "session", version: 3, id, timestamp: "2026-06-12T00:00:00.000Z", cwd: "/tmp" }),
];
let parent: string | null = null;
for (const entryId of entryIds) {
lines.push(
JSON.stringify({
type: "model_change",
id: entryId,
parentId: parent,
timestamp: "2026-06-12T00:00:01.000Z",
model: "test/model",
}),
);
parent = entryId;
}
return `${lines.join("\n")}\n`;
}
describe("collectSubSessions", () => {
let root: string;
let mainFile: string;
beforeEach(async () => {
root = await fs.mkdtemp(path.join(os.tmpdir(), "omp-subsessions-"));
mainFile = path.join(root, "main.jsonl");
await Bun.write(mainFile, sessionJsonl("main", ["m1"]));
});
afterEach(async () => {
await fs.rm(root, { recursive: true, force: true });
});
test("collects nested subagent sessions with parent links and leaf ids", async () => {
await Bun.write(path.join(root, "main/Alpha.jsonl"), sessionJsonl("alpha", ["a1", "a2"]));
await Bun.write(path.join(root, "main/Alpha/Child.jsonl"), sessionJsonl("child", ["c1"]));
await Bun.write(path.join(root, "main/Beta.jsonl"), sessionJsonl("beta", ["b1"]));
const subs = await collectSubSessions(mainFile);
expect(Object.keys(subs).sort()).toEqual(["Alpha", "Alpha/Child", "Beta"]);
expect(subs.Alpha).toMatchObject({ agentId: "Alpha", parent: null, leafId: "a2" });
expect(subs.Alpha.entries.map(e => e.id)).toEqual(["a1", "a2"]);
expect(subs.Alpha.header?.id).toBe("alpha");
expect(subs["Alpha/Child"]).toMatchObject({ agentId: "Child", parent: "Alpha", leafId: "c1" });
expect(subs.Beta).toMatchObject({ agentId: "Beta", parent: null, leafId: "b1" });
});
test("skips corrupt, empty, backup, and non-jsonl files", async () => {
await Bun.write(path.join(root, "main/Good.jsonl"), sessionJsonl("good", ["g1"]));
await Bun.write(path.join(root, "main/corrupt.jsonl"), "{not json\n");
await Bun.write(path.join(root, "main/empty.jsonl"), "");
await Bun.write(path.join(root, "main/Good.jsonl.123.bak"), sessionJsonl("bak", ["x1"]));
await Bun.write(path.join(root, "main/notes.md"), "# notes\n");
const subs = await collectSubSessions(mainFile);
expect(Object.keys(subs)).toEqual(["Good"]);
});
test("returns empty record when no subagent dir exists", async () => {
expect(await collectSubSessions(mainFile)).toEqual({});
expect(await collectSubSessions(path.join(root, "not-a-session"))).toEqual({});
});
});