|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { promises as fs } from "node:fs"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import { test, type TestContext } from "node:test"; |
| 6 | +import { readSession, writeSession } from "@zharwing/memory-store"; |
| 7 | +import { MemoryService } from "../memory-service.js"; |
| 8 | +import { STALE_SESSION_CLOSE_REASON } from "./session-service.js"; |
| 9 | + |
| 10 | +test("starting a session auto-closes sessions left active on an earlier day", async (t) => { |
| 11 | + const service = new MemoryService({ memoryRoot: await tempMemoryRoot(t) }); |
| 12 | + const project = await createProject(service, "Auto Close Rollover"); |
| 13 | + |
| 14 | + const staleTimestamp = hoursAgo(48); |
| 15 | + const yesterday = await service.startSession({ |
| 16 | + projectId: project.id, |
| 17 | + taskTitle: "Yesterday's abandoned work", |
| 18 | + agent: "node-test" |
| 19 | + }); |
| 20 | + await backdateSession(yesterday.filePath!, staleTimestamp); |
| 21 | + |
| 22 | + const today = await service.startSession({ |
| 23 | + projectId: project.id, |
| 24 | + taskTitle: "Today's work", |
| 25 | + agent: "node-test" |
| 26 | + }); |
| 27 | + assert.equal(today.status, "active"); |
| 28 | + |
| 29 | + const sessions = await service.listSessions({ projectId: project.id }); |
| 30 | + const staleRow = sessions.find((session) => session.id === yesterday.id); |
| 31 | + assert.equal(staleRow?.status, "closed"); |
| 32 | + assert.equal(staleRow?.closedReason, STALE_SESSION_CLOSE_REASON); |
| 33 | + // The close is housekeeping, not activity: the stale session keeps its own |
| 34 | + // recency so it does not outrank today's session in the list. |
| 35 | + assert.equal(staleRow?.updated, staleTimestamp); |
| 36 | + assert.equal(sessions[0]?.id, today.id); |
| 37 | + |
| 38 | + // Auto-close still leaves a searchable TLDR behind. |
| 39 | + assert.equal(staleRow?.summarySource, "deterministic"); |
| 40 | + assert.ok(staleRow?.summary); |
| 41 | + |
| 42 | + const active = await service.getActiveSession({ projectId: project.id }); |
| 43 | + assert.equal(active?.id, today.id); |
| 44 | +}); |
| 45 | + |
| 46 | +test("start_or_resume starts a new session instead of resuming a previous day's log", async (t) => { |
| 47 | + const service = new MemoryService({ memoryRoot: await tempMemoryRoot(t) }); |
| 48 | + const project = await createProject(service, "Auto Close Resume"); |
| 49 | + |
| 50 | + const yesterday = await service.startOrResumeSession({ projectId: project.id, agent: "node-test" }); |
| 51 | + await backdateSession(yesterday.filePath!, hoursAgo(48)); |
| 52 | + |
| 53 | + const resumed = await service.startOrResumeSession({ projectId: project.id, agent: "node-test" }); |
| 54 | + assert.notEqual(resumed.id, yesterday.id); |
| 55 | + assert.equal(resumed.status, "active"); |
| 56 | + |
| 57 | + const sameDay = await service.startOrResumeSession({ projectId: project.id, agent: "node-test" }); |
| 58 | + assert.equal(sameDay.id, resumed.id); |
| 59 | +}); |
| 60 | + |
| 61 | +test("startup state stops recommending a previous day's session", async (t) => { |
| 62 | + const memoryRoot = await tempMemoryRoot(t); |
| 63 | + const service = new MemoryService({ memoryRoot }); |
| 64 | + const project = await createProject(service, "Auto Close Startup"); |
| 65 | + |
| 66 | + const session = await service.startSession({ |
| 67 | + projectId: project.id, |
| 68 | + taskTitle: "Left open overnight", |
| 69 | + agent: "node-test" |
| 70 | + }); |
| 71 | + |
| 72 | + const sameDay = await service.getStartupState({ projectId: project.id, workingDirectory: memoryRoot }); |
| 73 | + if (sameDay.notModified) assert.fail("startup must return a snapshot"); |
| 74 | + assert.equal(sameDay.recommendedAction, "resume-active"); |
| 75 | + |
| 76 | + await backdateSession(session.filePath!, hoursAgo(48)); |
| 77 | + |
| 78 | + const nextDay = await service.getStartupState({ projectId: project.id, workingDirectory: memoryRoot }); |
| 79 | + if (nextDay.notModified) assert.fail("startup must return a snapshot"); |
| 80 | + assert.equal(nextDay.recommendedAction, "start-new"); |
| 81 | +}); |
| 82 | + |
| 83 | +test("auto-close leaves an already summarized session's TLDR untouched", async (t) => { |
| 84 | + const service = new MemoryService({ memoryRoot: await tempMemoryRoot(t) }); |
| 85 | + const project = await createProject(service, "Auto Close Summary"); |
| 86 | + |
| 87 | + const yesterday = await service.startSession({ |
| 88 | + projectId: project.id, |
| 89 | + taskTitle: "Summarized already", |
| 90 | + agent: "node-test" |
| 91 | + }); |
| 92 | + const stored = await readSession(yesterday.filePath!); |
| 93 | + await writeSession({ |
| 94 | + ...stored, |
| 95 | + started: hoursAgo(48), |
| 96 | + updated: hoursAgo(48), |
| 97 | + summary: "Hand written TLDR.", |
| 98 | + summaryGeneratedAt: hoursAgo(48), |
| 99 | + summarySource: "manual" |
| 100 | + }); |
| 101 | + |
| 102 | + await service.startSession({ projectId: project.id, taskTitle: "Next day", agent: "node-test" }); |
| 103 | + |
| 104 | + const sessions = await service.listSessions({ projectId: project.id }); |
| 105 | + const staleRow = sessions.find((session) => session.id === yesterday.id); |
| 106 | + assert.equal(staleRow?.status, "closed"); |
| 107 | + assert.equal(staleRow?.summary, "Hand written TLDR."); |
| 108 | + assert.equal(staleRow?.summarySource, "manual"); |
| 109 | +}); |
| 110 | + |
| 111 | +async function createProject(service: MemoryService, projectName: string) { |
| 112 | + const preview = await service.prepareProjectCreation({ projectName, createPointerFile: false }); |
| 113 | + return service.createProject({ preview }); |
| 114 | +} |
| 115 | + |
| 116 | +async function backdateSession(filePath: string, timestamp: string): Promise<void> { |
| 117 | + const session = await readSession(filePath); |
| 118 | + await writeSession({ ...session, started: timestamp, updated: timestamp }); |
| 119 | +} |
| 120 | + |
| 121 | +function hoursAgo(hours: number): string { |
| 122 | + return new Date(Date.now() - hours * 60 * 60 * 1000).toISOString(); |
| 123 | +} |
| 124 | + |
| 125 | +async function tempMemoryRoot(t: TestContext): Promise<string> { |
| 126 | + const memoryRoot = await fs.mkdtemp(path.join(os.tmpdir(), "zharwing-daemon-test-")); |
| 127 | + t.after(() => fs.rm(memoryRoot, { recursive: true, force: true })); |
| 128 | + return memoryRoot; |
| 129 | +} |
0 commit comments