Files
t3code/packages/client-runtime/src/state/shell.test.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

235 lines
7.8 KiB
TypeScript

import type { ServerConfig } from "@t3tools/contracts";
import { EnvironmentId } from "@t3tools/contracts";
import { describe, expect, it } from "@effect/vitest";
import * as Option from "effect/Option";
import * as DateTime from "effect/DateTime";
import { Atom, AtomRegistry } from "effect/unstable/reactivity";
import { PrimaryConnectionTarget } from "../connection/model.ts";
import type { EnvironmentShellState } from "./shell.ts";
import { createEnvironmentServerConfigsAtom, createEnvironmentShellSummaryAtom } from "./shell.ts";
import { v2ThreadShell } from "./orchestrationV2TestFixtures.ts";
const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
const OTHER_ENVIRONMENT_ID = EnvironmentId.make("environment-2");
function environmentEntry(environmentId: EnvironmentId, label: string) {
return {
target: new PrimaryConnectionTarget({
environmentId,
label,
httpBaseUrl: `https://${environmentId}.example.test`,
wsBaseUrl: `wss://${environmentId}.example.test`,
}),
profile: Option.none(),
enabled: true,
};
}
function shellState(input: {
readonly status: EnvironmentShellState["status"];
readonly updatedAt?: string;
readonly error?: string;
readonly snapshotSequence?: number;
}): EnvironmentShellState {
return {
snapshot:
input.updatedAt === undefined
? Option.none()
: Option.some({
schemaVersion: 1,
snapshotSequence: input.snapshotSequence ?? 1,
projects: [],
threads: [
{
...v2ThreadShell,
updatedAt: DateTime.makeUnsafe(input.updatedAt),
},
],
archivedThreads: [],
}),
status: input.status,
error: input.error === undefined ? Option.none() : Option.some(input.error),
};
}
function makeHarness() {
const shellStateAtoms = Atom.family((environmentId: EnvironmentId) =>
Atom.make<EnvironmentShellState>(
environmentId === ENVIRONMENT_ID
? shellState({
status: "cached",
updatedAt: "2026-06-01T00:00:00.000Z",
})
: shellState({
status: "synchronizing",
updatedAt: "2026-06-02T00:00:00.000Z",
error: "Retrying.",
}),
),
);
const configAtoms = Atom.family((_environmentId: EnvironmentId) =>
Atom.make<ServerConfig | null>(null),
);
const catalogValueAtom = Atom.make({
isReady: true,
entries: new Map([
[ENVIRONMENT_ID, environmentEntry(ENVIRONMENT_ID, "Environment")],
[OTHER_ENVIRONMENT_ID, environmentEntry(OTHER_ENVIRONMENT_ID, "Other environment")],
]),
});
const summaryAtom = createEnvironmentShellSummaryAtom({
catalogValueAtom,
shellStateValueAtom: shellStateAtoms,
});
const serverConfigsAtom = createEnvironmentServerConfigsAtom({
catalogValueAtom,
serverConfigValueAtom: configAtoms,
});
return {
catalogValueAtom,
registry: AtomRegistry.make(),
shellStateAtom: shellStateAtoms,
configAtom: configAtoms,
summaryAtom,
serverConfigsAtom,
};
}
describe("environment shell projections", () => {
it("summarizes shell state and preserves identity when only irrelevant snapshot data changes", () => {
const harness = makeHarness();
const summary = harness.registry.get(harness.summaryAtom);
expect(summary).toEqual({
hasSnapshot: true,
hasSynchronizingShell: true,
hasCachedShell: true,
hasLiveShell: false,
firstError: "Retrying.",
});
harness.registry.set(
harness.shellStateAtom(ENVIRONMENT_ID),
shellState({
status: "cached",
updatedAt: "2026-06-01T00:00:00.000Z",
snapshotSequence: 2,
}),
);
expect(harness.registry.get(harness.summaryAtom)).toBe(summary);
});
it("does not notify summary subscribers when thread timestamps advance", () => {
const harness = makeHarness();
const initial = harness.registry.get(harness.summaryAtom);
let changes = 0;
const unsubscribe = harness.registry.subscribe(harness.summaryAtom, () => changes++);
try {
for (let index = 0; index < 20; index++) {
harness.registry.set(
harness.shellStateAtom(ENVIRONMENT_ID),
shellState({
status: "cached",
updatedAt: `2026-07-01T00:00:${String(index).padStart(2, "0")}.000Z`,
snapshotSequence: index + 2,
}),
);
expect(harness.registry.get(harness.summaryAtom)).toBe(initial);
}
expect(changes).toBe(0);
} finally {
unsubscribe();
harness.registry.dispose();
}
});
it("notifies for shell availability, status, errors, and enabled environment changes", () => {
const harness = makeHarness();
let changes = 0;
const unsubscribe = harness.registry.subscribe(harness.summaryAtom, () => changes++);
const setCatalog = (entries: ReturnType<typeof environmentEntry>[]) => {
harness.registry.set(harness.catalogValueAtom, {
isReady: true,
entries: new Map(entries.map((entry) => [entry.target.environmentId, entry])),
});
return harness.registry.get(harness.summaryAtom);
};
try {
expect(setCatalog([])).toEqual({
hasSnapshot: false,
hasSynchronizingShell: false,
hasCachedShell: false,
hasLiveShell: false,
firstError: null,
});
expect(setCatalog([environmentEntry(ENVIRONMENT_ID, "Environment")])).toMatchObject({
hasSnapshot: true,
hasCachedShell: true,
});
harness.registry.set(harness.shellStateAtom(ENVIRONMENT_ID), shellState({ status: "empty" }));
expect(harness.registry.get(harness.summaryAtom)).toMatchObject({
hasSnapshot: false,
hasCachedShell: false,
});
harness.registry.set(
harness.shellStateAtom(ENVIRONMENT_ID),
shellState({ status: "synchronizing", error: "Retrying." }),
);
expect(harness.registry.get(harness.summaryAtom)).toMatchObject({
hasSnapshot: false,
hasSynchronizingShell: true,
firstError: "Retrying.",
});
harness.registry.set(
harness.shellStateAtom(ENVIRONMENT_ID),
shellState({ status: "live", updatedAt: "2026-07-01T00:00:00.000Z" }),
);
expect(harness.registry.get(harness.summaryAtom)).toMatchObject({
hasSnapshot: true,
hasSynchronizingShell: false,
hasLiveShell: true,
firstError: null,
});
harness.registry.set(
harness.shellStateAtom(ENVIRONMENT_ID),
shellState({
status: "live",
updatedAt: "2026-07-01T00:00:00.000Z",
error: "Disconnected.",
}),
);
expect(harness.registry.get(harness.summaryAtom).firstError).toBe("Disconnected.");
harness.registry.set(
harness.shellStateAtom(ENVIRONMENT_ID),
shellState({ status: "live", updatedAt: "2026-07-01T00:00:00.000Z" }),
);
expect(harness.registry.get(harness.summaryAtom).firstError).toBeNull();
expect(
setCatalog([{ ...environmentEntry(ENVIRONMENT_ID, "Environment"), enabled: false }]),
).toMatchObject({ hasSnapshot: false, hasLiveShell: false });
expect(changes).toBe(8);
} finally {
unsubscribe();
harness.registry.dispose();
}
});
it("preserves server-config map identity until a config reference changes", () => {
const harness = makeHarness();
const empty = harness.registry.get(harness.serverConfigsAtom);
const config = { cwd: "/repo" } as ServerConfig;
harness.registry.set(harness.configAtom(ENVIRONMENT_ID), config);
const withConfig = harness.registry.get(harness.serverConfigsAtom);
expect(withConfig).not.toBe(empty);
expect(withConfig.get(ENVIRONMENT_ID)).toBe(config);
harness.registry.set(harness.configAtom(ENVIRONMENT_ID), config);
expect(harness.registry.get(harness.serverConfigsAtom)).toBe(withConfig);
});
});