Something went wrong. Try again.
An agent harness for spec-driven development.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173import { describe, expect, test } from "vitest";import { buildEventsFromChanges, buildWorkItemsFromEvents, queuedWorkItems } from "../src/changes.js";import type { AgentConsumerRow, CharterDatabaseSnapshot, SpecChange, SpecImportPlan } from "../src/types.js";
describe("work queue", () => { test("creates queued work items from project events", () => { const plan: SpecImportPlan = { projectRoot: "/tmp/project", specRoot: "/tmp/project/spec", generatedAt: "2026-01-01T00:00:00.000Z", documents: [], rows: [ { id: "runtime", path: "runtime.md", title: "Runtime", kind: "architecture", status: "active", body: "# Runtime", frontmatterJson: "{}", headingsJson: "[]", sha256: "abc123abc123", sizeBytes: 10, mtimeMs: 1, importedAt: "2026-01-01T00:00:00.000Z", }, ], events: [], }; const changes: SpecChange[] = [ { type: "added", documentId: "runtime", path: "runtime.md", title: "Runtime", currentSha256: "abc123abc123", }, ];
const events = buildEventsFromChanges(plan, changes); const items = buildWorkItemsFromEvents(plan, events, changes);
expect(items).toHaveLength(1); expect(items[0]?.status).toBe("queued"); expect(items[0]?.eventId).toBe(events[0]?.id); expect(items[0]?.attachedDocumentIds).toEqual(["runtime"]); });
test("matches active consumers by event type and document selector", () => { const plan: SpecImportPlan = { projectRoot: "/tmp/project", specRoot: "/tmp/project/spec", generatedAt: "2026-01-01T00:00:00.000Z", documents: [], rows: [ { id: "features/login", path: "features/login.md", title: "Login", kind: "task", status: "active", body: "# Login", frontmatterJson: "{}", headingsJson: "[]", sha256: "abc123abc123", sizeBytes: 10, mtimeMs: 1, importedAt: "2026-01-01T00:00:00.000Z", }, ], events: [], }; const changes: SpecChange[] = [{ type: "added", documentId: "features/login", path: "features/login.md", title: "Login", currentSha256: "abc123abc123" }]; const consumers: AgentConsumerRow[] = [ { id: "feature-consumer", name: "Feature consumer", status: "active", runnerKind: "script", runnerCommand: "npm test", runnerConfigJson: "{}", eventTypes: ["spec.document.upserted"], documentSelectors: ["features/**"], promptDocumentIds: [], defaultContextDocumentIds: [], cursorJson: "{}", createdAt: "2026-01-01T00:00:00.000Z", updatedAt: "2026-01-01T00:00:00.000Z", }, { id: "inactive-consumer", name: "Inactive consumer", status: "paused", runnerKind: "script", runnerCommand: "npm test", runnerConfigJson: "{}", eventTypes: ["spec.document.upserted"], documentSelectors: ["features/**"], promptDocumentIds: [], defaultContextDocumentIds: [], cursorJson: "{}", createdAt: "2026-01-01T00:00:00.000Z", updatedAt: "2026-01-01T00:00:00.000Z", }, ];
const events = buildEventsFromChanges(plan, changes); const items = buildWorkItemsFromEvents(plan, events, changes, consumers);
expect(items[0]?.matchedConsumerIds).toEqual(["feature-consumer"]); });
test("returns queued work items in creation order", () => { const snapshot: CharterDatabaseSnapshot = { projectId: "test", updatedAt: "2026-01-01T00:00:00.000Z", documents: {}, pathIndex: {}, events: [], consumers: {}, packets: {}, requests: {}, workItems: { done: { id: "done", kind: "spec-change", status: "done", eventId: "event-done", documentId: "done-doc", path: "done.md", title: "Done", createdAt: "2026-01-01T00:00:01.000Z", updatedAt: "2026-01-01T00:00:01.000Z", authority: "importer.sync", attachedDocumentIds: ["done-doc"], matchedConsumerIds: [], }, second: { id: "second", kind: "spec-change", status: "queued", eventId: "event-second", documentId: "second-doc", path: "second.md", title: "Second", createdAt: "2026-01-01T00:00:02.000Z", updatedAt: "2026-01-01T00:00:02.000Z", authority: "importer.sync", attachedDocumentIds: ["second-doc"], matchedConsumerIds: [], }, first: { id: "first", kind: "spec-change", status: "queued", eventId: "event-first", documentId: "first-doc", path: "first.md", title: "First", createdAt: "2026-01-01T00:00:00.000Z", updatedAt: "2026-01-01T00:00:00.000Z", authority: "importer.sync", attachedDocumentIds: ["first-doc"], matchedConsumerIds: [], }, }, };
expect(queuedWorkItems(snapshot).map((item) => item.id)).toEqual(["first", "second"]); });});