Fix project recreation keys and update web tests

This commit is contained in:
justsomelegs committed 2026-04-03 10:25:55 +01:00
1 parent 8e5c244740
commit 0737ff435c
4 files changed
+171 -75

No files matched your search

+140 -64
View File
@@ -1,6 +1,7 @@
import { ProjectId, ThreadId, TurnId } from "@t3tools/contracts";
import { afterEach, describe, expect, it, vi } from "vitest";
import { useStore } from "../store";
import { type Thread } from "../types";
import {
MAX_HIDDEN_MOUNTED_TERMINAL_THREADS,
@@ -178,7 +179,7 @@ const makeThread = (input?: {
startedAt: string | null;
completedAt: string | null;
} | null;
}) => ({
}): Thread => ({
id: input?.id ?? ThreadId.makeUnsafe("thread-1"),
codexThreadId: null,
projectId: ProjectId.makeUnsafe("project-1"),
@@ -205,94 +206,172 @@ const makeThread = (input?: {
activities: [],
});
function setStoreThreads(threads: ReadonlyArray<ReturnType<typeof makeThread>>) {
const projectId = ProjectId.makeUnsafe("project-1");
useStore.setState({
projectIds: [projectId],
projectById: {
[projectId]: {
id: projectId,
name: "Project",
cwd: "/tmp/project",
defaultModelSelection: {
provider: "codex",
model: "gpt-5.4",
},
createdAt: "2026-03-29T00:00:00.000Z",
updatedAt: "2026-03-29T00:00:00.000Z",
scripts: [],
},
},
threadIds: threads.map((thread) => thread.id),
threadIdsByProjectId: {
[projectId]: threads.map((thread) => thread.id),
},
threadShellById: Object.fromEntries(
threads.map((thread) => [
thread.id,
{
id: thread.id,
codexThreadId: thread.codexThreadId,
projectId: thread.projectId,
title: thread.title,
modelSelection: thread.modelSelection,
runtimeMode: thread.runtimeMode,
interactionMode: thread.interactionMode,
error: thread.error,
createdAt: thread.createdAt,
archivedAt: thread.archivedAt,
updatedAt: thread.updatedAt,
branch: thread.branch,
worktreePath: thread.worktreePath,
},
]),
),
threadSessionById: Object.fromEntries(threads.map((thread) => [thread.id, thread.session])),
threadTurnStateById: Object.fromEntries(
threads.map((thread) => [
thread.id,
{
latestTurn: thread.latestTurn,
...(thread.pendingSourceProposedPlan
? { pendingSourceProposedPlan: thread.pendingSourceProposedPlan }
: {}),
},
]),
),
messageIdsByThreadId: Object.fromEntries(
threads.map((thread) => [thread.id, thread.messages.map((message) => message.id)]),
),
messageByThreadId: Object.fromEntries(
threads.map((thread) => [
thread.id,
Object.fromEntries(thread.messages.map((message) => [message.id, message])),
]),
),
activityIdsByThreadId: Object.fromEntries(
threads.map((thread) => [thread.id, thread.activities.map((activity) => activity.id)]),
),
activityByThreadId: Object.fromEntries(
threads.map((thread) => [
thread.id,
Object.fromEntries(thread.activities.map((activity) => [activity.id, activity])),
]),
),
proposedPlanIdsByThreadId: Object.fromEntries(
threads.map((thread) => [thread.id, thread.proposedPlans.map((plan) => plan.id)]),
),
proposedPlanByThreadId: Object.fromEntries(
threads.map((thread) => [
thread.id,
Object.fromEntries(thread.proposedPlans.map((plan) => [plan.id, plan])),
]),
),
turnDiffIdsByThreadId: Object.fromEntries(
threads.map((thread) => [
thread.id,
thread.turnDiffSummaries.map((summary) => summary.turnId),
]),
),
turnDiffSummaryByThreadId: Object.fromEntries(
threads.map((thread) => [
thread.id,
Object.fromEntries(thread.turnDiffSummaries.map((summary) => [summary.turnId, summary])),
]),
),
sidebarThreadSummaryById: {},
bootstrapComplete: true,
});
}
afterEach(() => {
vi.useRealTimers();
vi.restoreAllMocks();
useStore.setState((state) => ({
...state,
projects: [],
threads: [],
bootstrapComplete: true,
}));
setStoreThreads([]);
});
describe("waitForStartedServerThread", () => {
it("resolves immediately when the thread is already started", async () => {
const threadId = ThreadId.makeUnsafe("thread-started");
useStore.setState((state) => ({
...state,
threads: [
makeThread({
id: threadId,
latestTurn: {
turnId: TurnId.makeUnsafe("turn-started"),
state: "running",
requestedAt: "2026-03-29T00:00:01.000Z",
startedAt: "2026-03-29T00:00:01.000Z",
completedAt: null,
},
}),
],
}));
setStoreThreads([
makeThread({
id: threadId,
latestTurn: {
turnId: TurnId.makeUnsafe("turn-started"),
state: "running",
requestedAt: "2026-03-29T00:00:01.000Z",
startedAt: "2026-03-29T00:00:01.000Z",
completedAt: null,
},
}),
]);
await expect(waitForStartedServerThread(threadId)).resolves.toBe(true);
});
it("waits for the thread to start via subscription updates", async () => {
const threadId = ThreadId.makeUnsafe("thread-wait");
useStore.setState((state) => ({
...state,
threads: [makeThread({ id: threadId })],
}));
setStoreThreads([makeThread({ id: threadId })]);
const promise = waitForStartedServerThread(threadId, 500);
useStore.setState((state) => ({
...state,
threads: [
makeThread({
id: threadId,
latestTurn: {
turnId: TurnId.makeUnsafe("turn-started"),
state: "running",
requestedAt: "2026-03-29T00:00:01.000Z",
startedAt: "2026-03-29T00:00:01.000Z",
completedAt: null,
},
}),
],
}));
setStoreThreads([
makeThread({
id: threadId,
latestTurn: {
turnId: TurnId.makeUnsafe("turn-started"),
state: "running",
requestedAt: "2026-03-29T00:00:01.000Z",
startedAt: "2026-03-29T00:00:01.000Z",
completedAt: null,
},
}),
]);
await expect(promise).resolves.toBe(true);
});
it("handles the thread starting between the initial read and subscription setup", async () => {
const threadId = ThreadId.makeUnsafe("thread-race");
useStore.setState((state) => ({
...state,
threads: [makeThread({ id: threadId })],
}));
setStoreThreads([makeThread({ id: threadId })]);
const originalSubscribe = useStore.subscribe.bind(useStore);
let raced = false;
vi.spyOn(useStore, "subscribe").mockImplementation((listener) => {
if (!raced) {
raced = true;
useStore.setState((state) => ({
...state,
threads: [
makeThread({
id: threadId,
latestTurn: {
turnId: TurnId.makeUnsafe("turn-race"),
state: "running",
requestedAt: "2026-03-29T00:00:01.000Z",
startedAt: "2026-03-29T00:00:01.000Z",
completedAt: null,
},
}),
],
}));
setStoreThreads([
makeThread({
id: threadId,
latestTurn: {
turnId: TurnId.makeUnsafe("turn-race"),
state: "running",
requestedAt: "2026-03-29T00:00:01.000Z",
startedAt: "2026-03-29T00:00:01.000Z",
completedAt: null,
},
}),
]);
}
return originalSubscribe(listener);
});
@@ -304,10 +383,7 @@ describe("waitForStartedServerThread", () => {
vi.useFakeTimers();
const threadId = ThreadId.makeUnsafe("thread-timeout");
useStore.setState((state) => ({
...state,
threads: [makeThread({ id: threadId })],
}));
setStoreThreads([makeThread({ id: threadId })]);
const promise = waitForStartedServerThread(threadId, 500);
await vi.advanceTimersByTimeAsync(500);
@@ -95,7 +95,7 @@ describe("MessagesTimeline", () => {
expect(markup).toContain("Terminal 1 lines 1-5");
expect(markup).toContain("lucide-terminal");
expect(markup).toContain("yoo what&#x27;s ");
});
}, 10_000);
it("renders context compaction entries in the normal work log", async () => {
const { MessagesTimeline } = await import("./MessagesTimeline");
+7 -1
View File
@@ -491,6 +491,9 @@ describe("incremental orchestration updates", () => {
expect(projectsOf(next)[0]?.id).toBe(recreatedProjectId);
expect(projectsOf(next)[0]?.cwd).toBe("/tmp/project");
expect(projectsOf(next)[0]?.name).toBe("Project Recreated");
expect(next.projectIds).toEqual([recreatedProjectId]);
expect(next.projectById[originalProjectId]).toBeUndefined();
expect(next.projectById[recreatedProjectId]?.id).toBe(recreatedProjectId);
});
it("removes stale project index entries when thread.created recreates a thread under a new project", () => {
@@ -660,7 +663,10 @@ describe("incremental orchestration updates", () => {
expect(threadsOf(next)[0]?.messages[0]?.text).toBe("hello world");
expect(threadsOf(next)[0]?.latestTurn?.state).toBe("running");
expect(threadsOf(next)[1]).toBe(thread2);
expect(next.threadShellById[thread2.id]).toBe(state.threadShellById[thread2.id]);
expect(next.threadSessionById[thread2.id]).toBe(state.threadSessionById[thread2.id]);
expect(next.messageIdsByThreadId[thread2.id]).toBe(state.messageIdsByThreadId[thread2.id]);
expect(next.messageByThreadId[thread2.id]).toBe(state.messageByThreadId[thread2.id]);
});
it("applies replay batches in sequence and updates session state", () => {
+23 -9
View File
@@ -1020,15 +1020,29 @@ export function applyOrchestrationEvent(state: AppState, event: OrchestrationEve
projectId === event.payload.projectId ||
state.projectById[projectId]?.cwd === event.payload.workspaceRoot,
) ?? null;
const resolvedProjectId = existingProjectId ?? nextProject.id;
const projectById = {
...state.projectById,
[resolvedProjectId]: nextProject,
};
const projectIds =
existingProjectId === null && !state.projectIds.includes(nextProject.id)
? [...state.projectIds, nextProject.id]
: state.projectIds;
let projectById = state.projectById;
let projectIds = state.projectIds;
if (existingProjectId !== null && existingProjectId !== nextProject.id) {
const { [existingProjectId]: _removedProject, ...restProjectById } = state.projectById;
projectById = {
...restProjectById,
[nextProject.id]: nextProject,
};
projectIds = state.projectIds.map((projectId) =>
projectId === existingProjectId ? nextProject.id : projectId,
);
} else {
projectById = {
...state.projectById,
[nextProject.id]: nextProject,
};
projectIds =
existingProjectId === null && !state.projectIds.includes(nextProject.id)
? [...state.projectIds, nextProject.id]
: state.projectIds;
}
return {
...state,
projectById,