Open History to see the last commit
1import {
2 EnvironmentId,
3 MessageId,
4 ProjectId,
5 ProviderInstanceId,
6 ThreadId,
7} from "@t3tools/contracts";
8import { serializeAssistantCitation } from "@t3tools/shared/assistantCitations";
9import { describe, expect, it } from "vite-plus/test";
10
11import { serializeComposerMessageForServer } from "./composerContext";
12
13import {
14 buildProjectThreadStartTurnInput,
15 deriveThreadTitleFromPrompt,
16 type ProjectThreadStartTurnSpec,
17} from "./projectThreadStartTurn";
18
19describe("project thread title", () => {
20 it("normalizes draft and queued titles while keeping the mobile length limit", () => {
21 expect(
22 deriveThreadTitleFromPrompt(
23 " Fix [src/app.ts](t3-context://v1/mention/source) so the sidebar remains readable when a thread starts with an image",
24 ),
25 ).toBe("Fix src/app.ts so the sidebar remains readable when a thread starts w...");
26 });
27
28 it("keeps ordinary titles and the empty-prompt fallback", () => {
29 expect(deriveThreadTitleFromPrompt(" Fix\n the parser ")).toBe("Fix the parser");
30 expect(deriveThreadTitleFromPrompt(" \n ")).toBe("New thread");
31 });
32
33 it.each([
34 {
35 type: "image" as const,
36 name: "photo.png",
37 mimeType: "image/png",
38 text: "",
39 title: "Attached image",
40 },
41 {
42 type: "file" as const,
43 name: "report.pdf",
44 mimeType: "application/pdf",
45 text: "",
46 title: "File: report.pdf",
47 },
48 {
49 type: "file" as const,
50 name: "report.pdf",
51 mimeType: "application/pdf",
52 text: "[report.pdf](t3-context://v1/file/report)",
53 title: "report.pdf",
54 },
55 ])("titles a $type-only message without changing its data", ({ text, title, ...attachment }) => {
56 const uploadedAttachments = [{ ...attachment, id: "prepared-attachment", sizeBytes: 3 }];
57 const wire = serializeComposerMessageForServer(text, undefined, true);
58 const spec = {
59 projectId: ProjectId.make("project"),
60 projectCwd: "/workspace",
61 threadId: "image-thread",
62 commandId: "image-command",
63 messageId: "image-message",
64 createdAt: "2026-09-04T00:00:00Z",
65 ...wire,
66 titleText: text,
67 uploadedAttachments,
68 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.6-sol" },
69 runtimeMode: "full-access",
70 interactionMode: "default",
71 workspaceMode: "local",
72 branch: null,
73 worktreePath: null,
74 startFromOrigin: false,
75 worktreeBranchName: "unused",
76 } satisfies ProjectThreadStartTurnSpec;
77 const input = buildProjectThreadStartTurnInput(spec);
78
79 expect(input.titleSeed).toBe(title);
80 expect(input.bootstrap.createThread.title).toBe(input.titleSeed);
81 expect(input.message.attachments).toEqual(uploadedAttachments);
82 expect(input.message.text).toBe(wire.text);
83 const relaunched = buildProjectThreadStartTurnInput({
84 ...spec,
85 threadId: "local-relaunch",
86 text: input.message.text,
87 titleText: input.bootstrap.createThread.title,
88 });
89 expect(relaunched.titleSeed).toBe(title);
90 expect(relaunched.message.text).toBe(wire.text);
91 expect(relaunched.message.attachments).toEqual(uploadedAttachments);
92 });
93
94 it.each([
95 {
96 comment: undefined,
97 title: "Keep `cache[key]` & <parser> shared. Retry!",
98 },
99 {
100 comment: 'Why "shared"?',
101 title: "Keep `cache[key]` & <parser> shared. Retry! Commen...",
102 },
103 ])("uses readable titles and intact links with comment $comment", ({ comment, title }) => {
104 const quoteText = "Keep `cache[key]` & <parser> shared.\n Retry!";
105 const text = serializeAssistantCitation({
106 version: 1,
107 environmentId: EnvironmentId.make("source-environment"),
108 threadId: ThreadId.make("source-thread"),
109 messageId: MessageId.make("source-message"),
110 text: quoteText,
111 ...(comment === undefined ? {} : { comment }),
112 start: 0,
113 end: quoteText.length,
114 prefix: "",
115 suffix: "",
116 });
117 const input = buildProjectThreadStartTurnInput({
118 projectId: ProjectId.make("project"),
119 projectCwd: "/workspace",
120 threadId: "new-thread",
121 commandId: "command",
122 messageId: "message",
123 createdAt: "2026-09-01T00:00:00Z",
124 text,
125 uploadedAttachments: [],
126 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.6-sol" },
127 runtimeMode: "full-access",
128 interactionMode: "default",
129 workspaceMode: "local",
130 branch: null,
131 worktreePath: null,
132 startFromOrigin: false,
133 worktreeBranchName: "unused",
134 });
135
136 expect(input.titleSeed).toBe(title);
137 expect(input.bootstrap.createThread.title).toBe(input.titleSeed);
138 expect(input.message.text).toBe(text);
139 });
140});
141
142describe("new thread on an existing branch", () => {
143 it.each([null, "/worktrees/existing"])(
144 "reuses the selected workspace %s without preparing a new worktree",
145 (worktreePath) => {
146 const input = buildProjectThreadStartTurnInput({
147 projectId: ProjectId.make("project"),
148 projectCwd: "/workspace",
149 threadId: "new-thread",
150 commandId: "command",
151 messageId: "message",
152 createdAt: "2026-09-06T00:00:00Z",
153 text: "Start fresh",
154 uploadedAttachments: [],
155 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.6-sol" },
156 runtimeMode: "full-access",
157 interactionMode: "default",
158 workspaceMode: "local",
159 branch: "feature/existing",
160 worktreePath,
161 startFromOrigin: false,
162 worktreeBranchName: "unused",
163 });
164
165 expect(input.bootstrap.createThread).toMatchObject({
166 projectId: "project",
167 branch: "feature/existing",
168 worktreePath,
169 });
170 expect(input.bootstrap).not.toHaveProperty("prepareWorktree");
171 expect(input.bootstrap).not.toHaveProperty("runSetupScript");
172 expect(input.threadId).toBe("new-thread");
173 },
174 );
175});