|
| 1 | +import { describe, expect, it } from "@effect/vitest"; |
| 2 | +import { |
| 3 | + EnvironmentId, |
| 4 | + ProjectId, |
| 5 | + ProviderInstanceId, |
| 6 | + ThreadId, |
| 7 | + type OrchestrationV2ThreadShell, |
| 8 | +} from "@t3tools/contracts"; |
| 9 | +import * as DateTime from "effect/DateTime"; |
| 10 | +import * as Effect from "effect/Effect"; |
| 11 | +import * as Schema from "effect/Schema"; |
| 12 | + |
| 13 | +import { |
| 14 | + ORCHESTRATION_CACHE_SCHEMA_VERSION, |
| 15 | + StoredOrchestrationShellSnapshot, |
| 16 | +} from "./orchestrationCache.ts"; |
| 17 | +import { makeStoredShellSnapshotEncoder } from "./shellCacheEncoding.ts"; |
| 18 | + |
| 19 | +const NOW = DateTime.makeUnsafe("2026-07-29T12:00:00.000Z"); |
| 20 | +const PROJECT_ID = ProjectId.make("project-1"); |
| 21 | +const PROVIDER_INSTANCE_ID = ProviderInstanceId.make("codex"); |
| 22 | + |
| 23 | +const makeThread = (index: number): OrchestrationV2ThreadShell => { |
| 24 | + const id = ThreadId.make(`thread-${index}`); |
| 25 | + return { |
| 26 | + id, |
| 27 | + projectId: PROJECT_ID, |
| 28 | + title: `Thread ${index}`, |
| 29 | + providerInstanceId: PROVIDER_INSTANCE_ID, |
| 30 | + modelSelection: { instanceId: PROVIDER_INSTANCE_ID, model: "gpt-5.4" }, |
| 31 | + runtimeMode: "full-access", |
| 32 | + interactionMode: "default", |
| 33 | + branch: null, |
| 34 | + worktreePath: null, |
| 35 | + activeProviderThreadId: null, |
| 36 | + lineage: { rootThreadId: id, parentThreadId: null, relationshipToParent: null }, |
| 37 | + forkedFrom: null, |
| 38 | + createdBy: "user", |
| 39 | + creationSource: "web", |
| 40 | + latestRunId: null, |
| 41 | + activeRunId: null, |
| 42 | + status: "idle", |
| 43 | + pendingRuntimeRequest: null, |
| 44 | + latestVisibleMessage: null, |
| 45 | + latestUserMessageAt: NOW, |
| 46 | + activityRunStartedAt: null, |
| 47 | + activityRunStatus: null, |
| 48 | + hasActionableProposedPlan: false, |
| 49 | + itemCount: 0, |
| 50 | + visibleItemCount: 0, |
| 51 | + createdAt: NOW, |
| 52 | + updatedAt: NOW, |
| 53 | + archivedAt: null, |
| 54 | + settledOverride: null, |
| 55 | + settledAt: NOW, |
| 56 | + unsettledAt: null, |
| 57 | + lastVisitedAt: null, |
| 58 | + deletedAt: null, |
| 59 | + }; |
| 60 | +}; |
| 61 | + |
| 62 | +const makeStored = (threads: ReadonlyArray<OrchestrationV2ThreadShell>) => ({ |
| 63 | + schemaVersion: ORCHESTRATION_CACHE_SCHEMA_VERSION, |
| 64 | + environmentId: EnvironmentId.make("environment-1"), |
| 65 | + snapshot: { |
| 66 | + schemaVersion: 1 as const, |
| 67 | + snapshotSequence: 3, |
| 68 | + projects: [], |
| 69 | + threads, |
| 70 | + archivedThreads: [], |
| 71 | + }, |
| 72 | +}); |
| 73 | + |
| 74 | +describe("shell cache encoder with a caller-supplied yield", () => { |
| 75 | + const encodeOriginal = Schema.encodeEffect( |
| 76 | + Schema.fromJsonString(StoredOrchestrationShellSnapshot), |
| 77 | + ); |
| 78 | + |
| 79 | + it.effect("keeps output identical to the full encode when only one row changes", () => |
| 80 | + Effect.gen(function* () { |
| 81 | + let yields = 0; |
| 82 | + const encode = makeStoredShellSnapshotEncoder({ |
| 83 | + yieldToHost: Effect.sync(() => { |
| 84 | + yields += 1; |
| 85 | + }), |
| 86 | + }); |
| 87 | + const threads = Array.from({ length: 70 }, (_, index) => makeThread(index)); |
| 88 | + const first = makeStored(threads); |
| 89 | + expect(yield* encode(first)).toBe(yield* encodeOriginal(first)); |
| 90 | + // 70 rows are three 32-row chunks, so the first save yields between them. |
| 91 | + expect(yields).toBeGreaterThanOrEqual(2); |
| 92 | + |
| 93 | + const second = makeStored([{ ...threads[0]!, title: "Renamed" }, ...threads.slice(1)]); |
| 94 | + expect(yield* encode(second)).toBe(yield* encodeOriginal(second)); |
| 95 | + }), |
| 96 | + ); |
| 97 | + |
| 98 | + it.effect("does not yield again when every row was already encoded", () => |
| 99 | + Effect.gen(function* () { |
| 100 | + let yields = 0; |
| 101 | + const encode = makeStoredShellSnapshotEncoder({ |
| 102 | + yieldToHost: Effect.sync(() => { |
| 103 | + yields += 1; |
| 104 | + }), |
| 105 | + }); |
| 106 | + const stored = makeStored(Array.from({ length: 70 }, (_, index) => makeThread(index))); |
| 107 | + yield* encode(stored); |
| 108 | + const afterFirst = yields; |
| 109 | + yield* encode(makeStored(stored.snapshot.threads)); |
| 110 | + expect(yields).toBe(afterFirst); |
| 111 | + }), |
| 112 | + ); |
| 113 | +}); |
0 commit comments