co-mono/packages/coding-agent/test/session-manager/migration.test.ts
Mario Zechner c6fc084534 Merge hooks and custom-tools into unified extensions system (#454)
Breaking changes:
- Settings: 'hooks' and 'customTools' arrays replaced with 'extensions'
- CLI: '--hook' and '--tool' flags replaced with '--extension' / '-e'
- API: HookMessage renamed to CustomMessage, role 'hookMessage' to 'custom'
- API: FileSlashCommand renamed to PromptTemplate
- API: discoverSlashCommands() renamed to discoverPromptTemplates()
- Directories: commands/ renamed to prompts/ for prompt templates

Migration:
- Session version bumped to 3 (auto-migrates v2 sessions)
- Old 'hookMessage' role entries converted to 'custom'

Structural changes:
- src/core/hooks/ and src/core/custom-tools/ merged into src/core/extensions/
- src/core/slash-commands.ts renamed to src/core/prompt-templates.ts
- examples/hooks/ and examples/custom-tools/ merged into examples/extensions/
- docs/hooks.md and docs/custom-tools.md merged into docs/extensions.md

New test coverage:
- test/extensions-runner.test.ts (10 tests)
- test/extensions-discovery.test.ts (26 tests)
- test/prompt-templates.test.ts
2026-01-05 01:43:35 +01:00

78 lines
2.3 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { type FileEntry, migrateSessionEntries } from "../../src/core/session-manager.js";
describe("migrateSessionEntries", () => {
it("should add id/parentId to v1 entries", () => {
const entries: FileEntry[] = [
{ type: "session", id: "sess-1", timestamp: "2025-01-01T00:00:00Z", cwd: "/tmp" },
{ type: "message", timestamp: "2025-01-01T00:00:01Z", message: { role: "user", content: "hi", timestamp: 1 } },
{
type: "message",
timestamp: "2025-01-01T00:00:02Z",
message: {
role: "assistant",
content: [{ type: "text", text: "hello" }],
api: "test",
provider: "test",
model: "test",
usage: { input: 1, output: 1, cacheRead: 0, cacheWrite: 0 },
stopReason: "stop",
timestamp: 2,
},
},
] as FileEntry[];
migrateSessionEntries(entries);
// Header should have version set (v3 is current after hookMessage->custom migration)
expect((entries[0] as any).version).toBe(3);
// Entries should have id/parentId
const msg1 = entries[1] as any;
const msg2 = entries[2] as any;
expect(msg1.id).toBeDefined();
expect(msg1.id.length).toBe(8);
expect(msg1.parentId).toBeNull();
expect(msg2.id).toBeDefined();
expect(msg2.id.length).toBe(8);
expect(msg2.parentId).toBe(msg1.id);
});
it("should be idempotent (skip already migrated)", () => {
const entries: FileEntry[] = [
{ type: "session", id: "sess-1", version: 2, timestamp: "2025-01-01T00:00:00Z", cwd: "/tmp" },
{
type: "message",
id: "abc12345",
parentId: null,
timestamp: "2025-01-01T00:00:01Z",
message: { role: "user", content: "hi", timestamp: 1 },
},
{
type: "message",
id: "def67890",
parentId: "abc12345",
timestamp: "2025-01-01T00:00:02Z",
message: {
role: "assistant",
content: [{ type: "text", text: "hello" }],
api: "test",
provider: "test",
model: "test",
usage: { input: 1, output: 1, cacheRead: 0, cacheWrite: 0 },
stopReason: "stop",
timestamp: 2,
},
},
] as FileEntry[];
migrateSessionEntries(entries);
// IDs should be unchanged
expect((entries[1] as any).id).toBe("abc12345");
expect((entries[2] as any).id).toBe("def67890");
expect((entries[2] as any).parentId).toBe("abc12345");
});
});