Files
united-workforce/packages/agent-builtin/__tests__/session.test.ts
T
xiaoju 06e959e7a5
CI / check (pull_request) Failing after 1m39s
test: add unit tests for core modules (#35)
Cover high-priority untested modules:
- util: base32, result, refs-field, storage-root, log-tag
- util-agent: storage (normalizeWorkflowConfig, resolveStorageRoot), run (parseArgv)
- agent-builtin: tools (read-file, write-file, run-command), session, detail

627 → 719 tests (+92), all passing.

Refs #35
2026-06-04 04:35:33 +00:00

66 lines
2.0 KiB
TypeScript

import { existsSync } from "node:fs";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import type { BuiltinTurnPayload } from "../src/types.js";
import {
appendSessionTurn,
initSessionDir,
readSessionTurns,
removeSession,
} from "../src/session.js";
describe("session", () => {
let storageRoot: string;
beforeEach(async () => {
storageRoot = await mkdtemp(join(tmpdir(), "builtin-session-"));
});
afterEach(async () => {
await rm(storageRoot, { recursive: true, force: true });
});
const makeTurn = (role: "assistant" | "tool", content: string): BuiltinTurnPayload => ({
role,
content,
toolCalls: null,
reasoning: null,
});
test("initSessionDir creates directory", async () => {
await initSessionDir(storageRoot);
expect(existsSync(join(storageRoot, "sessions"))).toBe(true);
});
test("append + read roundtrip", async () => {
await initSessionDir(storageRoot);
const sid = "test-session-1";
const t1 = makeTurn("tool", "hello");
const t2 = makeTurn("assistant", "hi there");
await appendSessionTurn(storageRoot, sid, t1);
await appendSessionTurn(storageRoot, sid, t2);
const turns = await readSessionTurns(storageRoot, sid);
expect(turns).toEqual([t1, t2]);
});
test("read from non-existent returns []", async () => {
const turns = await readSessionTurns(storageRoot, "no-such-session");
expect(turns).toEqual([]);
});
test("removeSession deletes file", async () => {
await initSessionDir(storageRoot);
const sid = "to-remove";
await appendSessionTurn(storageRoot, sid, makeTurn("tool", "bye"));
await removeSession(storageRoot, sid);
const turns = await readSessionTurns(storageRoot, sid);
expect(turns).toEqual([]);
});
test("removeSession on non-existent does not throw", async () => {
await expect(removeSession(storageRoot, "ghost")).resolves.toBeUndefined();
});
});