sailfork / t3code-github / apps/ web/ src/ test-fixtures.ts

import { presentThreadShell } from "@t3tools/client-runtime/state/shell";
import {
  EnvironmentId,
  MessageId,
  RunId,
  TurnItemId,
  type OrchestrationV2ProjectedTurnItem,
  type OrchestrationV2TurnItem,
  ProjectId,
  ProviderInstanceId,
  ThreadId,
  type OrchestrationV2ThreadProjection,
} from "@t3tools/contracts";
import * as DateTime from "effect/DateTime";

import type { ChatMessage, Thread } from "./types";

const DEFAULT_TIMESTAMP = "2026-01-01T00:00:00.000Z";

export type ThreadFixtureOverrides = Partial<Thread> & {
  readonly messages?: ReadonlyArray<ChatMessage>;
  readonly proposedPlans?: ReadonlyArray<unknown>;
};

export function makeThreadProjectionFixture(): OrchestrationV2ThreadProjection {
  const now = DateTime.makeUnsafe(DEFAULT_TIMESTAMP);
  const id = ThreadId.make("thread-test");
  const providerInstanceId = ProviderInstanceId.make("codex");
  return {
    thread: {
      id,
      projectId: ProjectId.make("project-test"),
      title: "Thread",
      providerInstanceId,
      modelSelection: { instanceId: providerInstanceId, model: "gpt-5.4" },
      runtimeMode: "full-access",
      interactionMode: "default",
      branch: null,
      worktreePath: null,
      activeProviderThreadId: null,
      lineage: { rootThreadId: id, parentThreadId: null, relationshipToParent: null },
      forkedFrom: null,
      createdBy: "user",
      creationSource: "web",
      createdAt: now,
      updatedAt: now,
      archivedAt: null,
      settledOverride: null,
      settledAt: null,
      lastVisitedAt: null,
      deletedAt: null,
    },
    runs: [],
    attempts: [],
    nodes: [],
    subagents: [],
    providerSessions: [],
    providerThreads: [],
    providerTurns: [],
    runtimeRequests: [],
    messages: [],
    plans: [],
    turnItems: [],
    checkpointScopes: [],
    checkpoints: [],
    contextHandoffs: [],
    contextTransfers: [],
    visibleTurnItems: [],
    updatedAt: now,
  };
}

export function makeThreadFixture(overrides: ThreadFixtureOverrides = {}): Thread {
  const environmentId = overrides.environmentId ?? EnvironmentId.make("environment-test");
  const id = overrides.id ?? ThreadId.make("thread-test");
  const projectId = overrides.projectId ?? ProjectId.make("project-test");
  const providerInstanceId =
    overrides.providerInstanceId ??
    overrides.modelSelection?.instanceId ??
    ProviderInstanceId.make("codex");
  const modelSelection = overrides.modelSelection ?? {
    instanceId: providerInstanceId,
    model: "gpt-5.4",
  };
  const createdAt = DateTime.makeUnsafe(DEFAULT_TIMESTAMP);
  const updatedAt = DateTime.makeUnsafe(DEFAULT_TIMESTAMP);
  const archivedAt =
    overrides.archivedAt === null || overrides.archivedAt === undefined
      ? null
      : DateTime.makeUnsafe(overrides.archivedAt);
  const deletedAt =
    overrides.deletedAt === null || overrides.deletedAt === undefined
      ? null
      : DateTime.makeUnsafe(overrides.deletedAt);
  const shell = presentThreadShell(environmentId, {
    id,
    projectId,
    title: overrides.title ?? "Thread",
    providerInstanceId,
    modelSelection,
    runtimeMode: overrides.runtimeMode ?? "full-access",
    interactionMode: overrides.interactionMode ?? "default",
    branch: overrides.branch ?? null,
    worktreePath: overrides.worktreePath ?? null,
    activeProviderThreadId: overrides.activeProviderThreadId ?? null,
    lineage: overrides.lineage ?? {
      rootThreadId: id,
      parentThreadId: null,
      relationshipToParent: null,
    },
    forkedFrom: overrides.forkedFrom ?? null,
    createdBy: "user",
    creationSource: "web",
    latestRunId: overrides.latestRun?.runId ?? null,
    activeRunId: overrides.runtime?.activeRunId ?? null,
    status: overrides.runtime?.status ?? "idle",
    pendingRuntimeRequest: null,
    latestVisibleMessage: null,
    latestUserMessageAt:
      overrides.latestUserMessageAt === null || overrides.latestUserMessageAt === undefined
        ? null
        : DateTime.makeUnsafe(overrides.latestUserMessageAt),
    hasActionableProposedPlan: overrides.hasActionableProposedPlan ?? false,
    itemCount: overrides.itemCount ?? 0,
    visibleItemCount: overrides.visibleItemCount ?? 0,
    createdAt,
    updatedAt,
    archivedAt,
    settledOverride: overrides.settledOverride ?? null,
    settledAt:
      overrides.settledAt === null || overrides.settledAt === undefined
        ? null
        : DateTime.makeUnsafe(overrides.settledAt),
    deletedAt,
  });

  return { ...shell, ...overrides };
}

export function makeStreamingTimelineFixture(text = "") {
  const threadId = ThreadId.make("streaming-thread");
  const runId = RunId.make("live-run");
  const historyRunId = RunId.make("history-run");
  const time = (second: number) => new Date(Date.UTC(2026, 8, 4, 0, 0, second)).toISOString();
  const base = (id: string, run: typeof runId, second: number) => ({
    id: TurnItemId.make(id),
    threadId,
    runId: run,
    nodeId: null,
    providerThreadId: null,
    providerTurnId: null,
    nativeItemRef: null,
    parentItemId: null,
    ordinal: second,
    status: "completed" as const,
    title: null,
    startedAt: DateTime.makeUnsafe(time(second)),
    completedAt: DateTime.makeUnsafe(time(second)),
    updatedAt: DateTime.makeUnsafe(time(second)),
  });
  const items: OrchestrationV2TurnItem[] = [
    {
      ...base("history-user-item", historyRunId, 0),
      type: "user_message",
      messageId: MessageId.make("history-user"),
      text: "Inspect",
      attachments: [],
      inputIntent: "turn_start",
      createdBy: "user",
      creationSource: "web",
    },
    {
      ...base("history-work", historyRunId, 1),
      type: "command_execution",
      status: "failed",
      input: "vp test",
      output: "Test failed",
      exitCode: 1,
    },
    {
      ...base("history-assistant-item", historyRunId, 3),
      type: "assistant_message",
      messageId: MessageId.make("history-assistant"),
      text: "Done",
      streaming: false,
      updatedAt: DateTime.makeUnsafe(time(4)),
    },
    {
      ...base("live-user-item", runId, 5),
      type: "user_message",
      messageId: MessageId.make("live-user"),
      text: "Continue",
      attachments: [],
      inputIntent: "turn_start",
      createdBy: "user",
      creationSource: "web",
    },
    {
      ...base("live-work", runId, 6),
      type: "dynamic_tool",
      toolName: "read_file",
      input: { path: "/repo/src/index.ts" },
      output: "Contents",
    },
    {
      ...base("live-assistant-item", runId, 7),
      type: "assistant_message",
      messageId: MessageId.make("live-assistant"),
      text,
      streaming: true,
      status: "running",
      completedAt: null,
    },
  ];
  const visibleTurnItems: OrchestrationV2ProjectedTurnItem[] = items.map((item, position) => ({
    position,
    visibility: "local",
    sourceThreadId: threadId,
    sourceItemId: item.id,
    item,
  }));
  return { threadId, runId, historyRunId, visibleTurnItems, time };
}