Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
import { describe, expect, it } from "vite-plus/test";
import {
ApprovalRequestId,
CommandId,
ProjectId,
ProviderInstanceId,
ThreadId,
} from "@akeru/contracts";
import type { OrchestrationThread } from "@akeru/contracts";
import { applyThreadDetailEvent } from "./threadReducer.ts";
import { baseEventFields, baseThread } from "./threadReducer.test-support.ts";
describe("applyThreadDetailEvent", () => {
describe("project events", () => {
it("returns unchanged for project.created", () => {
const result = applyThreadDetailEvent(baseThread, {
...baseEventFields,
sequence: 1,
occurredAt: "2026-04-01T01:00:00.000Z",
aggregateKind: "project",
aggregateId: ProjectId.make("project-1"),
type: "project.created",
payload: {
projectId: ProjectId.make("project-1"),
title: "T3 Code",
workspaceRoot: "/repo",
repositoryIdentity: null,
defaultModelSelection: null,
scripts: [],
createdAt: "2026-04-01T01:00:00.000Z",
updatedAt: "2026-04-01T01:00:00.000Z",
},
});
expect(result.kind).toBe("unchanged");
});
});
describe("thread.created", () => {
it("creates a fresh thread", () => {
const result = applyThreadDetailEvent(baseThread, {
...baseEventFields,
sequence: 1,
occurredAt: "2026-04-01T01:00:00.000Z",
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-2"),
type: "thread.created",
payload: {
threadId: ThreadId.make("thread-2"),
projectId: ProjectId.make("project-1"),
title: "New Thread",
modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
runtimeMode: "full-access",
interactionMode: "default",
branch: "main",
worktreePath: null,
createdAt: "2026-04-01T01:00:00.000Z",
updatedAt: "2026-04-01T01:00:00.000Z",
},
});
expect(result.kind).toBe("updated");
if (result.kind === "updated") {
expect(result.thread.id).toBe("thread-2");
expect(result.thread.title).toBe("New Thread");
expect(result.thread.branch).toBe("main");
expect(result.thread.messages).toEqual([]);
expect(result.thread.session).toBeNull();
}
});
});
describe("thread.deleted", () => {
it("returns deleted signal", () => {
const result = applyThreadDetailEvent(baseThread, {
...baseEventFields,
sequence: 2,
occurredAt: "2026-04-01T02:00:00.000Z",
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-1"),
type: "thread.deleted",
payload: {
threadId: ThreadId.make("thread-1"),
deletedAt: "2026-04-01T02:00:00.000Z",
},
});
expect(result.kind).toBe("deleted");
});
});
describe("thread.archived / thread.unarchived", () => {
it("sets archivedAt and clears title regeneration", () => {
const regeneratingThread: OrchestrationThread = {
...baseThread,
titleRegeneration: {
requestId: CommandId.make("regenerate-title"),
startedAt: "2026-04-01T02:00:00.000Z",
},
};
const result = applyThreadDetailEvent(regeneratingThread, {
...baseEventFields,
sequence: 3,
occurredAt: "2026-04-01T03:00:00.000Z",
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-1"),
type: "thread.archived",
payload: {
threadId: ThreadId.make("thread-1"),
archivedAt: "2026-04-01T03:00:00.000Z",
updatedAt: "2026-04-01T03:00:00.000Z",
},
});
expect(result.kind).toBe("updated");
if (result.kind === "updated") {
expect(result.thread.archivedAt).toBe("2026-04-01T03:00:00.000Z");
expect(result.thread.titleRegeneration).toBeNull();
}
});
it("clears archivedAt", () => {
const archivedThread = { ...baseThread, archivedAt: "2026-04-01T03:00:00.000Z" };
const result = applyThreadDetailEvent(archivedThread, {
...baseEventFields,
sequence: 4,
occurredAt: "2026-04-01T04:00:00.000Z",
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-1"),
type: "thread.unarchived",
payload: {
threadId: ThreadId.make("thread-1"),
updatedAt: "2026-04-01T04:00:00.000Z",
},
});
expect(result.kind).toBe("updated");
if (result.kind === "updated") {
expect(result.thread.archivedAt).toBeNull();
}
});
});
describe("thread.settled / thread.unsettled", () => {
it("sets the settled override and timestamp", () => {
const settledAt = "2026-04-01T05:00:00.000Z";
const result = applyThreadDetailEvent(baseThread, {
...baseEventFields,
sequence: 5,
occurredAt: settledAt,
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-1"),
type: "thread.settled",
payload: {
threadId: ThreadId.make("thread-1"),
settledAt,
updatedAt: settledAt,
},
});
expect(result.kind).toBe("updated");
if (result.kind === "updated") {
expect(result.thread.settledOverride).toBe("settled");
expect(result.thread.settledAt).toBe(settledAt);
}
});
it.each([
["user", "active"],
["activity", null],
] as const)("unsettles for %s with override %s", (reason, settledOverride) => {
const settledThread: OrchestrationThread = {
...baseThread,
settledOverride: "settled",
settledAt: "2026-04-01T05:00:00.000Z",
};
const updatedAt = "2026-04-01T06:00:00.000Z";
const result = applyThreadDetailEvent(settledThread, {
...baseEventFields,
sequence: 6,
occurredAt: updatedAt,
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-1"),
type: "thread.unsettled",
payload: {
threadId: ThreadId.make("thread-1"),
reason,
updatedAt,
},
});
expect(result.kind).toBe("updated");
if (result.kind === "updated") {
expect(result.thread.settledOverride).toBe(settledOverride);
expect(result.thread.settledAt).toBeNull();
}
});
});
describe("thread.pinned / thread.unpinned", () => {
it("sets pinnedAt", () => {
const pinnedAt = "2026-04-01T05:00:00.000Z";
const result = applyThreadDetailEvent(baseThread, {
...baseEventFields,
sequence: 5,
occurredAt: pinnedAt,
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-1"),
type: "thread.pinned",
payload: {
threadId: ThreadId.make("thread-1"),
pinnedAt,
updatedAt: pinnedAt,
},
});
expect(result.kind).toBe("updated");
if (result.kind === "updated") {
expect(result.thread.pinnedAt).toBe(pinnedAt);
}
});
it("clears pinnedAt", () => {
const pinnedThread: OrchestrationThread = {
...baseThread,
pinnedAt: "2026-04-01T05:00:00.000Z",
};
const updatedAt = "2026-04-01T06:00:00.000Z";
const result = applyThreadDetailEvent(pinnedThread, {
...baseEventFields,
sequence: 6,
occurredAt: updatedAt,
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-1"),
type: "thread.unpinned",
payload: {
threadId: ThreadId.make("thread-1"),
updatedAt,
},
});
expect(result.kind).toBe("updated");
if (result.kind === "updated") {
expect(result.thread.pinnedAt).toBeNull();
}
});
});
describe("thread.meta-updated", () => {
it("patches title and branch", () => {
const result = applyThreadDetailEvent(baseThread, {
...baseEventFields,
sequence: 5,
occurredAt: "2026-04-01T05:00:00.000Z",
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-1"),
type: "thread.meta-updated",
payload: {
threadId: ThreadId.make("thread-1"),
title: "Updated Title",
branch: "feature/demo",
updatedAt: "2026-04-01T05:00:00.000Z",
},
});
expect(result.kind).toBe("updated");
if (result.kind === "updated") {
expect(result.thread.title).toBe("Updated Title");
expect(result.thread.branch).toBe("feature/demo");
// Model selection should be unchanged since it wasn't in the payload
expect(result.thread.modelSelection).toEqual(baseThread.modelSelection);
}
});
it("sets and clears a linked pull request", () => {
const linkedPullRequest = {
projectId: ProjectId.make("project-1"),
repository: "pingdotgg/t3code",
number: 42,
url: "https://github.com/pingdotgg/t3code/pull/42",
};
const linked = applyThreadDetailEvent(baseThread, {
...baseEventFields,
sequence: 5,
occurredAt: "2026-04-01T05:00:00.000Z",
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-1"),
type: "thread.meta-updated",
payload: {
threadId: ThreadId.make("thread-1"),
linkedPullRequest,
updatedAt: "2026-04-01T05:00:00.000Z",
},
});
expect(linked.kind).toBe("updated");
if (linked.kind !== "updated") return;
expect(linked.thread.linkedPullRequest).toEqual(linkedPullRequest);
const cleared = applyThreadDetailEvent(linked.thread, {
...baseEventFields,
sequence: 6,
occurredAt: "2026-04-01T06:00:00.000Z",
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-1"),
type: "thread.meta-updated",
payload: {
threadId: ThreadId.make("thread-1"),
linkedPullRequest: null,
updatedAt: "2026-04-01T06:00:00.000Z",
},
});
expect(cleared.kind).toBe("updated");
if (cleared.kind === "updated") {
expect(cleared.thread.linkedPullRequest).toBeNull();
}
});
});
describe("no-op events", () => {
it("returns unchanged for approval-response-requested", () => {
const result = applyThreadDetailEvent(baseThread, {
...baseEventFields,
sequence: 15,
occurredAt: "2026-04-01T13:00:00.000Z",
aggregateKind: "thread",
aggregateId: ThreadId.make("thread-1"),
type: "thread.approval-response-requested",
payload: {
threadId: ThreadId.make("thread-1"),
requestId: ApprovalRequestId.make("req-1"),
decision: "accept",
createdAt: "2026-04-01T13:00:00.000Z",
},
});
expect(result.kind).toBe("unchanged");
});
});
});