Files
t3tui/test/turns.test.ts
venomade b40f09e56f Bump vendored T3 Code to 42c9107983 (v0.1.12-nightly) and catch up
- vendor/t3code submodule pinned to 42c9107983; deps in package.json
  aligned with the upstream catalog, including upstream's effect@4.1.5
  patch via patchedDependencies (patches/effect@4.1.5.patch)
- nix flake.nix devShell provides bun locally (no guix home install)
- scripts/link-vendor.ts now copies the vendored packages into
  node_modules/@t3tools and applies tscPatches for the spots where
  classic tsc can't infer what upstream's Effect language service can,
  so `bun run typecheck` runs clean under full strict mode
- protocol drift fixes across model/ui/client: usage-limit copy, context
  window snapshot, checkpoint totals, turn diff summaries, alert
  observation, "once" schedules, composer trigger kinds, PR keying,
  thread menu state, secret request answers, terminal summaries,
  snapshot-required shell events, revert checkpoint lookup
- new parity features over the typed RPC: fork a turn into a new thread,
  rewind a message with its attachments restored to the composer, guest
  share links and turn-into-project in the thread menu
- tests updated and added (301 pass); typecheck, lint and build clean

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-11-24 00:52:30 +00:00

59 lines
4.7 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { CheckpointId, CheckpointRef, CheckpointScopeId, MessageId, NodeId, ProviderSessionId, ProviderThreadId, ProviderTurnId, RunAttemptId, RunId, type OrchestrationV2ThreadProjection } from "@t3tools/contracts";
import type { TimelineEntriesProjection } from "../vendor/t3code/apps/web/src/session-logic.ts";
import { v2Now, v2Projection } from "../vendor/t3code/packages/client-runtime/src/state/orchestrationV2TestFixtures.ts";
import { adjacentTurn, diffTurn, findRevertCheckpoint, turnTargets } from "../src/model/turns.ts";
const providerThreadId = ProviderThreadId.make("pt");
const scopeId = CheckpointScopeId.make("scope");
function checkpoint(ordinal: number, runOrdinal: number | null = ordinal) {
return { id: CheckpointId.make(`cp-${ordinal}`), threadId: v2Projection.thread.id, scopeId, runId: runOrdinal === null ? null : RunId.make(`run-${runOrdinal}`), nodeId: NodeId.make("node"), parentCheckpointId: null, ordinalWithinScope: ordinal, appRunOrdinal: runOrdinal, ref: CheckpointRef.make(`refs/checkpoints/${ordinal}`), status: "ready" as const,
files: ordinal === 2 ? [] : [{ path: "file.ts", kind: "modified", additions: 1, deletions: 0 }], capturedAt: v2Now };
}
function entries(): TimelineEntriesProjection["entries"] {
return [1, 2, 5].flatMap((ordinal) => [{ kind: "message" as const, id: `user-${ordinal}`, createdAt: "2026-06-20T00:00:00.000Z", message: { id: MessageId.make(`user-${ordinal}`), role: "user" as const, text: `prompt ${ordinal}`, runId: RunId.make(`run-${ordinal}`), streaming: false, inputIntent: "turn_start" as const, createdAt: "2026-06-20T00:00:00.000Z", updatedAt: "2026-06-20T00:00:00.000Z" } }]);
}
function projection(): OrchestrationV2ThreadProjection {
const runs = [1, 2, 5].map((ordinal) => ({ id: RunId.make(`run-${ordinal}`), ordinal, activeAttemptId: RunAttemptId.make(`attempt-${ordinal}`), status: "completed" }));
return { ...v2Projection, thread: { ...v2Projection.thread, activeProviderThreadId: providerThreadId }, checkpoints: [checkpoint(0, null), checkpoint(1), checkpoint(2), checkpoint(3, 5)],
runs, attempts: [], providerThreads: [{ id: providerThreadId, providerSessionId: ProviderSessionId.make("session"), providerInstanceId: v2Projection.thread.modelSelection.instanceId }],
providerTurns: runs.map((run) => ({ id: ProviderTurnId.make(`pt-${run.ordinal}`), runAttemptId: run.activeAttemptId, ordinal: run.ordinal, providerThreadId })),
} as unknown as OrchestrationV2ThreadProjection;
}
describe("turn cursor and checkpoint targets", () => {
test("navigates by message identity after earlier turns load", () => {
const turns = turnTargets(projection(), entries(), null, true);
expect(adjacentTurn(turns, null, -1)?.messageId).toBe(MessageId.make("user-5"));
expect(adjacentTurn(turns, "user-5", -1)?.messageId).toBe(MessageId.make("user-2"));
expect(adjacentTurn(turns, "user-1", -1)?.messageId).toBe(MessageId.make("user-1"));
expect(adjacentTurn(turns, "user-5", 1)?.messageId).toBe(MessageId.make("user-5"));
expect(adjacentTurn([], null, -1)).toBeUndefined();
});
test("rewinds before the prompt, using visible predecessors across ordinal gaps", () => {
const turns = turnTargets(projection(), entries(), null, true);
expect(turns.map((turn) => turn.revertTurnCount)).toEqual([0, 1, 2]);
expect(findRevertCheckpoint(projection(), turns[2]!.revertTurnCount!)?.id).toBe(CheckpointId.make("cp-2"));
});
test("does not mistake the first message in paged history for the thread start", () => {
const turns = turnTargets(projection(), entries().slice(1), null, false);
expect(turns.map((turn) => turn.revertTurnCount)).toEqual([undefined, 2]);
});
test("does not offer a running turn or a detached provider conversation for rollback", () => {
expect(turnTargets(projection(), entries(), RunId.make("run-5"), true)[2]?.revertTurnCount).toBeUndefined();
expect(turnTargets({ ...projection(), providerThreads: [] }, entries(), null, true).every((turn) => turn.revertTurnCount === undefined)).toBe(true);
});
test("defaults to the latest changed turn; a selection never falls back to another turn", () => {
const turns = turnTargets(projection(), entries(), null, true);
expect(diffTurn(turns, null)?.checkpointTurnCount).toBe(5);
expect(diffTurn(turns.slice(0, 2), null)?.checkpointTurnCount).toBe(1);
expect(diffTurn(turns, "user-2")?.checkpointTurnCount).toBe(2);
expect(diffTurn(turns, "missing")).toBeUndefined();
});
test("keeps only one anchor for a turn with steering messages", () => {
const all = entries();
const first = all[0]!;
expect(turnTargets(projection(), [first, first, ...all.slice(1)], null, true)).toHaveLength(3);
});
});