The best way to control Claude Code, Codex and more from any computer or phone

main
ts 141 lines 5.03 KB
Raw
1 import { ApprovalRequestId, EnvironmentId, ThreadId } from "@t3tools/contracts";
2 import { describe, expect, it } from "@effect/vitest";
3 import { vi } from "vite-plus/test";
4 import {
5 questionAttachmentDraftKey,
6 questionAttachmentDraftPrefix,
7 } from "../state/question-attachments";
8 vi.mock("../state/atom-registry", () => ({ appAtomRegistry: {} }));
9
10 import {
11 composerAttachmentUploadBlockReason,
12 composerAttachmentsStillUploading,
13 composerAttachmentUploadKey,
14 composerDraftEnvironmentId,
15 type ComposerAttachmentUploadRequest,
16 } from "./composerAttachmentUploadQueue";
17
18 const environmentId = EnvironmentId.make("environment-1");
19 function request(id: string, environment = environmentId): ComposerAttachmentUploadRequest {
20 return {
21 environmentId: environment,
22 attachment: {
23 id,
24 type: "file",
25 name: `${id}.pdf`,
26 mimeType: "application/pdf",
27 sizeBytes: 42,
28 fileUri: `file:///documents/${id}.pdf`,
29 },
30 };
31 }
32
33 describe("draft upload scope and offline submission", () => {
34 it("uploads question drafts in their environment without matching other thread prefixes", () => {
35 const requestId = ApprovalRequestId.make("request:1");
36 for (const environment of [environmentId, EnvironmentId.make("remote:server")]) {
37 const threadId = ThreadId.make("thread:1");
38 const prefix = questionAttachmentDraftPrefix(environment, threadId);
39 const key = questionAttachmentDraftKey(environment, threadId, requestId, "question:1");
40 expect(composerDraftEnvironmentId(key, [environment], [])).toBe(environment);
41 expect(key.startsWith(prefix)).toBe(true);
42 for (const suffix of ["-extra", ":extra", "/extra", "%extra"]) {
43 const otherKey = questionAttachmentDraftKey(
44 environment,
45 ThreadId.make(`${threadId}${suffix}`),
46 requestId,
47 "question:1",
48 );
49 expect(otherKey.startsWith(prefix)).toBe(false);
50 expect(composerDraftEnvironmentId(otherKey, [environment], [])).toBe(environment);
51 }
52 }
53 });
54 it("resolves thread, new-task, and queued-task drafts without crossing environments", () => {
55 expect(composerDraftEnvironmentId("environment-1:thread", [environmentId], [])).toBe(
56 environmentId,
57 );
58 expect(composerDraftEnvironmentId("new-task:environment-1:project", [], [])).toBe(
59 environmentId,
60 );
61 expect(
62 composerDraftEnvironmentId(
63 "pending-task:message",
64 [],
65 [{ messageId: "message", environmentId }],
66 ),
67 ).toBe(environmentId);
68 expect(composerDraftEnvironmentId("pending-task:missing", [], [])).toBeNull();
69 const colonEnvironment = EnvironmentId.make("a:vcs-status:b");
70 expect(
71 composerDraftEnvironmentId(
72 `${colonEnvironment}:thread`,
73 [EnvironmentId.make("a"), colonEnvironment],
74 [],
75 ),
76 ).toBe(colonEnvironment);
77 expect(composerDraftEnvironmentId(`new-task:${colonEnvironment}:project`, [], [])).toBe(
78 colonEnvironment,
79 );
80 });
81
82 it("reads an id-keyed new-task draft's environment from its project stamp", () => {
83 const stamped = {
84 project: {
85 environmentId,
86 projectId: "project" as never,
87 createdAt: "2026-09-05T00:00:00.000Z",
88 },
89 };
90 expect(composerDraftEnvironmentId("new-task:abc123-def456", [], [], stamped)).toBe(
91 environmentId,
92 );
93 expect(composerDraftEnvironmentId("new-task:abc123-def456", [], [])).toBeNull();
94 expect(composerDraftEnvironmentId("new-task:environment-2:project", [], [], stamped)).toBe(
95 environmentId,
96 );
97 });
98
99 it("only a failed upload blocks sending; an in-flight one queues instead", () => {
100 const key = composerAttachmentUploadKey(environmentId, "file");
101 const input = {
102 environmentId,
103 attachments: [request("file").attachment],
104 connected: true,
105 serverConfig: {
106 environment: {
107 capabilities: { fileAttachments: { maxUploadBytes: 1024 } },
108 },
109 },
110 states: {},
111 };
112 expect(composerAttachmentUploadBlockReason(input)).toBeNull();
113 expect(composerAttachmentsStillUploading(input)).toBe(true);
114 expect(
115 composerAttachmentsStillUploading({
116 ...input,
117 states: { [key]: { status: "uploading", progress: 0.6 } },
118 }),
119 ).toBe(true);
120 expect(composerAttachmentUploadBlockReason({ ...input, connected: false })).toBeNull();
121 expect(
122 composerAttachmentUploadBlockReason({
123 ...input,
124 states: { [key]: { status: "failed", reason: "Offline" } },
125 }),
126 ).toBe("Retry or remove the failed attachment");
127 expect(
128 composerAttachmentsStillUploading({
129 ...input,
130 states: { [key]: { status: "failed", reason: "Offline" } },
131 }),
132 ).toBe(false);
133 expect(
134 composerAttachmentUploadBlockReason({ ...input, states: { [key]: { status: "ready" } } }),
135 ).toBeNull();
136 expect(
137 composerAttachmentsStillUploading({ ...input, states: { [key]: { status: "ready" } } }),
138 ).toBe(false);
139 expect(composerAttachmentsStillUploading({ ...input, serverConfig: null })).toBe(false);
140 });
141 });