-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeline.test.ts
More file actions
78 lines (66 loc) · 3.37 KB
/
Copy pathtimeline.test.ts
File metadata and controls
78 lines (66 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { TimelineProvider } from "../src/history/timeline.js";
import { EventStore } from "../src/history/event-store.js";
import * as fs from "fs";
import * as path from "path";
import * as os from "os";
describe("TimelineProvider", () => {
const testDir = path.join(os.tmpdir(), "refiner-timeline-test-" + Date.now());
beforeEach(() => {
process.env.PROMPT_REFINER_GLOBAL_DIR = testDir;
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true, force: true });
}
(EventStore as any).instance = null;
});
afterEach(() => {
const store = EventStore.getInstance();
store.close();
if (fs.existsSync(testDir)) {
fs.rmSync(testDir, { recursive: true, force: true });
}
});
it("should provide a unified and sorted timeline", () => {
const store = EventStore.getInstance();
// Insert a prompt (oldest)
store.recordPrompt({
id: "p1", client: "cli", raw_prompt: "old prompt", timestamp: "2026-04-12T10:00:00Z"
} as any);
// Insert an event (newest)
store.recordEvent({
id: "e1", event_type: "test", summary: "new event", timestamp: "2026-04-12T12:00:00Z"
} as any);
const provider = new TimelineProvider();
const timeline = provider.getUnifiedTimeline();
expect(timeline.length).toBeGreaterThanOrEqual(2);
expect(timeline[0].summary).toBe("new event");
expect(timeline[1].type).toBe("prompt");
});
it("uses empty detail fallbacks and applies the requested limit", () => {
const provider = new TimelineProvider();
const prepare = vi.fn()
.mockReturnValueOnce({ all: vi.fn().mockReturnValue([{ type: "prompt", id: "p", timestamp: "2026-01-01", summary: "p", details: null }]) })
.mockReturnValueOnce({ all: vi.fn().mockReturnValue([{ type: "commit", id: "c", timestamp: "2026-01-03", summary: "c", details: null }]) })
.mockReturnValueOnce({ all: vi.fn().mockReturnValue([{ type: "log", id: "e", timestamp: "2026-01-02", summary: "e", details: null }]) })
.mockReturnValueOnce({ all: vi.fn().mockReturnValue([{ type: "execution", id: "x", timestamp: "2025-01-04", summary: "x", details: null }]) });
(provider as any).eventStore = { db: { prepare } };
expect(provider.getUnifiedTimeline(2)).toEqual([
expect.objectContaining({ id: "c", details: { files: [] } }),
expect.objectContaining({ id: "e", details: {} }),
]);
});
it("does not crash when stored timeline JSON is malformed", () => {
const provider = new TimelineProvider();
const prepare = vi.fn()
.mockReturnValueOnce({ all: vi.fn().mockReturnValue([]) })
.mockReturnValueOnce({ all: vi.fn().mockReturnValue([{ type: "commit", id: "c", timestamp: "2026-01-03", summary: "c", details: "{" }]) })
.mockReturnValueOnce({ all: vi.fn().mockReturnValue([{ type: "log", id: "e", timestamp: "2026-01-02", summary: "e", details: "not-json" }]) })
.mockReturnValueOnce({ all: vi.fn().mockReturnValue([{ type: "execution", id: "x", timestamp: "2026-01-01", summary: "x", details: "[" }]) });
(provider as any).eventStore = { db: { prepare } };
expect(provider.getUnifiedTimeline()).toEqual([
expect.objectContaining({ id: "c", details: { files: [] } }),
expect.objectContaining({ id: "e", details: {} }),
expect.objectContaining({ id: "x", details: {} }),
]);
});
});