Files
t3code/apps/web/src/state/threads.test.ts
T
2026-09-24 21:57:35 -07:00

201 lines
6.5 KiB
TypeScript

import {
EMPTY_ENVIRONMENT_THREAD_STATE,
type EnvironmentThreadState,
} from "@t3tools/client-runtime/state/threads";
import {
EnvironmentId,
ProjectId,
ProviderInstanceId,
ThreadId,
type OrchestrationSessionStatus,
type OrchestrationThread,
type OrchestrationThreadShell,
} from "@t3tools/contracts";
import * as Option from "effect/Option";
import { AsyncResult, Atom, AtomRegistry } from "effect/unstable/reactivity";
import { describe, expect, it } from "vite-plus/test";
import { createRunningThreadKeepAliveAtom } from "./threads";
const LOCAL = EnvironmentId.make("local");
const REMOTE = EnvironmentId.make("remote");
function session(threadId: ThreadId, status: OrchestrationSessionStatus) {
return {
threadId,
status,
providerName: "codex",
runtimeMode: "full-access",
activeTurnId: null,
lastError: null,
updatedAt: "2026-09-24T00:00:00.000Z",
} satisfies OrchestrationThread["session"];
}
function shell(id: string, status: OrchestrationSessionStatus | null) {
const threadId = ThreadId.make(id);
return {
id: threadId,
session: status === null ? null : session(threadId, status),
} satisfies Pick<OrchestrationThreadShell, "id" | "session">;
}
function detail(
id: string,
status: OrchestrationSessionStatus,
overrides: Partial<EnvironmentThreadState> = {},
) {
const threadId = ThreadId.make(id);
const thread: OrchestrationThread = {
id: threadId,
projectId: ProjectId.make("project"),
title: id,
modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
runtimeMode: "full-access",
interactionMode: "default",
branch: null,
worktreePath: null,
latestTurn: null,
createdAt: "2026-09-24T00:00:00.000Z",
updatedAt: "2026-09-24T00:00:00.000Z",
archivedAt: null,
settledOverride: null,
settledAt: null,
pullRequests: [],
deletedAt: null,
messages: [],
proposedPlans: [],
activities: [],
checkpoints: [],
session: session(threadId, status),
};
return AsyncResult.success<EnvironmentThreadState>({
...EMPTY_ENVIRONMENT_THREAD_STATE,
status: "live",
data: Option.some(thread),
...overrides,
});
}
function makeHarness() {
// Registry cleanup runs only on `flush`, like the real deferred task.
const tasks: Array<() => void> = [];
const registry = AtomRegistry.make({
scheduleTask: (task) => {
tasks.push(task);
return () => {};
},
});
const flush = () => {
for (let task = tasks.shift(); task !== undefined; task = tasks.shift()) task();
};
const environmentIds = Atom.make<ReadonlyArray<EnvironmentId>>([LOCAL, REMOTE]).pipe(
Atom.keepAlive,
);
const threads = Atom.family((_environmentId: EnvironmentId) =>
Atom.make<ReadonlyArray<ReturnType<typeof shell>>>([]).pipe(Atom.keepAlive),
);
// Stand-ins for the thread state atoms. Each one lives only while mounted,
// as the real stream does.
const keys = new Set<string>();
const states = Atom.family((_key: string) =>
Atom.make<AsyncResult.AsyncResult<EnvironmentThreadState>>(
AsyncResult.success(EMPTY_ENVIRONMENT_THREAD_STATE),
),
);
const stateAtom = (environmentId: EnvironmentId, threadId: string) => {
const key = `${environmentId}:${threadId}`;
keys.add(key);
return states(key);
};
const keepAlive = createRunningThreadKeepAliveAtom({
environmentIdsAtom: environmentIds,
threadsAtom: threads,
stateAtom,
});
registry.mount(keepAlive);
return {
registry,
environmentIds,
threads,
stateAtom,
keepAlive,
openStreams: () => {
flush();
return [...keys].filter((key) => registry.getNodes().has(states(key))).toSorted();
},
};
}
describe("createRunningThreadKeepAliveAtom", () => {
it("keeps running threads open across shell updates and thread view visits", () => {
const h = makeHarness();
h.registry.set(h.threads(LOCAL), [
shell("a", "running"),
shell("b", "ready"),
shell("c", null),
]);
h.registry.set(h.threads(REMOTE), [shell("d", "starting")]);
expect(h.openStreams()).toEqual(["local:a", "remote:d"]);
// A thread view that comes and goes shares the kept stream.
const live = detail("a", "running");
h.registry.set(h.stateAtom(LOCAL, "a"), live);
h.registry.mount(h.stateAtom(LOCAL, "a"))();
// A shell update that starts or stops nothing does not rebuild the set.
const kept = h.registry.get(h.keepAlive);
h.registry.set(h.threads(LOCAL), [shell("a", "running"), shell("b", "ready")]);
expect(h.registry.get(h.keepAlive)).toBe(kept);
expect(h.openStreams()).toEqual(["local:a", "remote:d"]);
expect(h.registry.get(h.stateAtom(LOCAL, "a"))).toBe(live);
});
it("holds a stopped thread until its own stream is live and shows the stop", () => {
const h = makeHarness();
h.registry.set(h.threads(LOCAL), [
shell("a", "running"),
shell("b", "running"),
shell("c", "running"),
]);
// "b" has not loaded yet. "c" hit a stream error.
h.registry.set(h.stateAtom(LOCAL, "a"), detail("a", "running"));
h.registry.set(
h.stateAtom(LOCAL, "c"),
detail("c", "running", { status: "cached", error: Option.some("Could not sync.") }),
);
// The shell reports the stops first. A failed stream cannot deliver its
// stop, so only it is released now.
h.registry.set(h.threads(LOCAL), [
shell("a", "ready"),
shell("b", "ready"),
shell("c", "ready"),
]);
expect(h.openStreams()).toEqual(["local:a", "local:b"]);
h.registry.set(h.stateAtom(LOCAL, "a"), detail("a", "ready"));
h.registry.set(h.stateAtom(LOCAL, "b"), detail("b", "ready", { status: "synchronizing" }));
expect(h.openStreams()).toEqual(["local:b"]);
h.registry.set(h.stateAtom(LOCAL, "b"), detail("b", "ready"));
expect(h.openStreams()).toEqual([]);
});
it("follows environments that connect and go away", () => {
const h = makeHarness();
h.registry.set(h.environmentIds, [LOCAL]);
h.registry.set(h.threads(REMOTE), [shell("d", "running")]);
expect(h.openStreams()).toEqual([]);
h.registry.set(h.environmentIds, [LOCAL, REMOTE]);
expect(h.openStreams()).toEqual(["remote:d"]);
// Removal drops every mount, including one still waiting for its stop.
h.registry.set(h.stateAtom(REMOTE, "d"), detail("d", "running"));
h.registry.set(h.threads(REMOTE), [shell("d", "ready")]);
expect(h.openStreams()).toEqual(["remote:d"]);
h.registry.set(h.environmentIds, [LOCAL]);
expect(h.openStreams()).toEqual([]);
});
});