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 "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Option from "effect/Option";
import * as Queue from "effect/Queue";
import * as Ref from "effect/Ref";
import * as TestClock from "effect/testing/TestClock";
import {
THREAD_ID,
CACHED_SNAPSHOT_SEQUENCE,
BASE_THREAD,
ACTIVE_THREAD,
awaitThreadState,
makeHarness,
snapshot,
titleUpdated,
deleted,
} from "./threads-sync.test-support.ts";
describe("EnvironmentThreads", () => {
it.effect("publishes cached data immediately from a warm cache", () =>
Effect.gen(function* () {
const harness = yield* makeHarness({ cached: BASE_THREAD });
const state = yield* awaitThreadState(harness.observed, (value) => Option.isSome(value.data));
expect(Option.getOrThrow(state.data)).toEqual(BASE_THREAD);
expect(Option.isNone(state.error)).toBe(true);
}),
);
it.effect("resumes a warm cache via afterSequence without an HTTP fetch", () =>
Effect.gen(function* () {
const harness = yield* makeHarness({ cached: BASE_THREAD });
// The warm cache reaches live from the cached data, and a live event
// applies on top of it.
yield* Queue.offer(harness.inputs, titleUpdated("Live title", CACHED_SNAPSHOT_SEQUENCE + 1));
yield* awaitThreadState(
harness.observed,
(value) =>
value.status === "live" &&
Option.isSome(value.data) &&
value.data.value.title === "Live title",
);
// The subscription resumed from the cached sequence and never fetched the
// full snapshot over HTTP.
expect(yield* Ref.get(harness.lastSubscribeAfterSequence)).toBe(CACHED_SNAPSHOT_SEQUENCE);
expect(yield* Ref.get(harness.loaderCalls)).toBe(0);
}),
);
it.effect("reduces live events and persists the latest thread", () =>
Effect.gen(function* () {
const harness = yield* makeHarness({ cached: BASE_THREAD });
yield* Queue.offer(harness.inputs, snapshot(BASE_THREAD));
yield* Queue.offer(harness.inputs, titleUpdated("Live title"));
const state = yield* awaitThreadState(
harness.observed,
(value) =>
value.status === "live" &&
Option.isSome(value.data) &&
value.data.value.title === "Live title",
);
yield* TestClock.adjust("500 millis");
yield* Effect.yieldNow;
expect(Option.getOrThrow(state.data).title).toBe("Live title");
expect((yield* Ref.get(harness.savedThreads)).at(-1)?.thread.title).toBe("Live title");
expect((yield* Ref.get(harness.savedThreads)).at(-1)?.snapshotSequence).toBe(2);
}),
);
it.effect("does not persist active thread snapshots during streaming or teardown", () =>
Effect.gen(function* () {
const savedThreads = yield* Effect.scoped(
Effect.gen(function* () {
const harness = yield* makeHarness({ cached: ACTIVE_THREAD });
yield* awaitThreadState(
harness.observed,
(value) =>
value.status === "live" &&
Option.isSome(value.data) &&
value.data.value.session?.status === "running",
);
yield* TestClock.adjust("500 millis");
yield* Effect.yieldNow;
expect(yield* Ref.get(harness.savedThreads)).toEqual([]);
return harness.savedThreads;
}),
);
expect(yield* Ref.get(savedThreads)).toEqual([]);
}),
);
it.effect("removes cached data when the thread is deleted", () =>
Effect.gen(function* () {
const harness = yield* makeHarness({ cached: BASE_THREAD });
yield* Queue.offer(harness.inputs, snapshot(BASE_THREAD));
yield* Queue.offer(harness.inputs, deleted());
const state = yield* awaitThreadState(
harness.observed,
(value) => value.status === "deleted",
);
expect(Option.isNone(state.data)).toBe(true);
expect(yield* Ref.get(harness.removedThreads)).toEqual([THREAD_ID]);
}),
);
it.effect("does not resurrect a deleted thread when the app returns to the foreground", () =>
Effect.gen(function* () {
const harness = yield* makeHarness({
cached: BASE_THREAD,
completionMarker: true,
httpSnapshot: Option.some({
snapshotSequence: 4,
thread: { ...BASE_THREAD, title: "Stale HTTP thread" },
}),
});
yield* Queue.offer(harness.inputs, snapshot(BASE_THREAD));
yield* Queue.offer(harness.inputs, deleted());
yield* awaitThreadState(harness.observed, (value) => value.status === "deleted");
expect(yield* Ref.get(harness.loaderCalls)).toBe(0);
yield* Queue.offer(harness.wakeups, "application-active");
for (let attempt = 0; attempt < 100; attempt += 1) {
if ((yield* Ref.get(harness.subscriptionCount)) >= 2) break;
yield* Effect.yieldNow;
}
const latest = yield* Ref.get(harness.latest);
expect(yield* Ref.get(harness.subscriptionCount)).toBe(2);
expect(yield* Ref.get(harness.loaderCalls)).toBe(0);
expect(latest.status).toBe("deleted");
expect(Option.isNone(latest.data)).toBe(true);
}),
);
});