Implement explicit session lifecycle and task-scoped worktrees
This commit is contained in:
116
tests/session-lifecycle.test.ts
Normal file
116
tests/session-lifecycle.test.ts
Normal file
@@ -0,0 +1,116 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { execFile } from "node:child_process";
|
||||
import { mkdtemp, mkdir, readFile, writeFile, stat } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { resolve } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
import {
|
||||
FileSystemSessionMetadataStore,
|
||||
SessionWorktreeManager,
|
||||
type SessionMetadata,
|
||||
} from "../src/agents/session-lifecycle.js";
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
async function git(args: string[]): Promise<string> {
|
||||
const { stdout } = await execFileAsync("git", args, {
|
||||
encoding: "utf8",
|
||||
});
|
||||
return stdout.trim();
|
||||
}
|
||||
|
||||
test("session metadata store persists and updates session metadata", async () => {
|
||||
const stateRoot = await mkdtemp(resolve(tmpdir(), "ai-ops-session-store-"));
|
||||
const store = new FileSystemSessionMetadataStore({ stateRoot });
|
||||
|
||||
const created = await store.createSession({
|
||||
sessionId: "session-abc",
|
||||
projectPath: resolve(stateRoot, "project"),
|
||||
baseWorkspacePath: resolve(stateRoot, "worktrees", "session-abc", "base"),
|
||||
});
|
||||
|
||||
assert.equal(created.sessionStatus, "active");
|
||||
assert.equal(created.sessionId, "session-abc");
|
||||
|
||||
const listed = await store.listSessions();
|
||||
assert.equal(listed.length, 1);
|
||||
assert.equal(listed[0]?.sessionId, "session-abc");
|
||||
|
||||
const updated = await store.updateSession("session-abc", {
|
||||
sessionStatus: "closed",
|
||||
});
|
||||
assert.equal(updated.sessionStatus, "closed");
|
||||
|
||||
const readBack = await store.readSession("session-abc");
|
||||
assert.equal(readBack?.sessionStatus, "closed");
|
||||
});
|
||||
|
||||
test("session worktree manager provisions and merges task worktrees", async () => {
|
||||
const root = await mkdtemp(resolve(tmpdir(), "ai-ops-session-worktree-"));
|
||||
const projectPath = resolve(root, "project");
|
||||
const worktreeRoot = resolve(root, "worktrees");
|
||||
|
||||
await mkdir(projectPath, { recursive: true });
|
||||
await git(["init", projectPath]);
|
||||
await git(["-C", projectPath, "config", "user.name", "AI Ops"]);
|
||||
await git(["-C", projectPath, "config", "user.email", "ai-ops@example.local"]);
|
||||
await writeFile(resolve(projectPath, "README.md"), "# project\n", "utf8");
|
||||
await git(["-C", projectPath, "add", "README.md"]);
|
||||
await git(["-C", projectPath, "commit", "-m", "initial commit"]);
|
||||
|
||||
const manager = new SessionWorktreeManager({
|
||||
worktreeRoot,
|
||||
baseRef: "HEAD",
|
||||
});
|
||||
|
||||
const sessionId = "session-1";
|
||||
const baseWorkspacePath = manager.resolveBaseWorkspacePath(sessionId);
|
||||
|
||||
await manager.initializeSessionBaseWorkspace({
|
||||
sessionId,
|
||||
projectPath,
|
||||
baseWorkspacePath,
|
||||
});
|
||||
|
||||
const baseStats = await stat(baseWorkspacePath);
|
||||
assert.equal(baseStats.isDirectory(), true);
|
||||
|
||||
const taskWorktreePath = (
|
||||
await manager.ensureTaskWorktree({
|
||||
sessionId,
|
||||
taskId: "task-1",
|
||||
baseWorkspacePath,
|
||||
})
|
||||
).taskWorktreePath;
|
||||
|
||||
await writeFile(resolve(taskWorktreePath, "feature.txt"), "task output\n", "utf8");
|
||||
|
||||
await manager.mergeTaskIntoBase({
|
||||
taskId: "task-1",
|
||||
baseWorkspacePath,
|
||||
taskWorktreePath,
|
||||
});
|
||||
|
||||
const mergedFile = await readFile(resolve(baseWorkspacePath, "feature.txt"), "utf8");
|
||||
assert.equal(mergedFile, "task output\n");
|
||||
|
||||
const session: SessionMetadata = {
|
||||
sessionId,
|
||||
projectPath,
|
||||
baseWorkspacePath,
|
||||
sessionStatus: "active",
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
};
|
||||
|
||||
await manager.closeSession({
|
||||
session,
|
||||
taskWorktreePaths: [],
|
||||
mergeBaseIntoProject: false,
|
||||
});
|
||||
|
||||
await assert.rejects(() => stat(baseWorkspacePath), {
|
||||
code: "ENOENT",
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user