@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 112 lines 3.86 KB
Raw
1 import { expect } from "e2e";
2 import * as Effect from "effect/Effect";
3 import * as FileSystem from "effect/FileSystem";
4 import * as Path from "effect/Path";
5 import * as Schema from "effect/Schema";
6 import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/http";
7
8 import { T3_HOME } from "../e2e.config.ts";
9 import { ScenarioError } from "./shell.ts";
10 import { makeState, runPromise, test, WEB } from "./t3.ts";
11
12 const SettingsJson = Schema.fromJsonString(Schema.Record(Schema.String, Schema.Unknown));
13 const decodeSettings = Schema.decodeEffect(SettingsJson);
14 const encodeSettings = Schema.encodeEffect(SettingsJson);
15
16 // A token from `auth session issue` acts at most in approval-required mode, so the thread must too.
17 const approvalRequiredThreads = Effect.gen(function* () {
18 const fs = yield* FileSystem.FileSystem;
19 const settingsPath = (yield* Path.Path).join(T3_HOME, "userdata/settings.json");
20 const original = yield* fs.readFileString(settingsPath);
21 const settings = yield* decodeSettings(original);
22 return {
23 apply: fs.writeFileString(
24 settingsPath,
25 yield* encodeSettings({
26 ...settings,
27 defaultRuntimeMode: "approval-required",
28 }),
29 ),
30 restore: fs.writeFileString(settingsPath, original),
31 };
32 });
33
34 /** Archives a thread as an outside agent does: `t3_thread_organize` on the server's MCP endpoint. */
35 const archiveOverMcp = Effect.fn("scenario.archiveOverMcp")(function* (
36 baseUrl: string,
37 threadId: string,
38 ) {
39 const { cli } = makeState(T3_HOME);
40 const { stdout } = yield* cli([
41 "auth",
42 "session",
43 "issue",
44 "--subject",
45 "mcp-client",
46 "--scope",
47 "orchestration:read",
48 "--scope",
49 "orchestration:operate",
50 "--token-only",
51 ]);
52 const client = yield* HttpClient.HttpClient;
53 let sessionId: string | null = null;
54 const send = Effect.fnUntraced(function* (message: object) {
55 const response = yield* client.execute(
56 HttpClientRequest.post(`${baseUrl}/mcp`).pipe(
57 HttpClientRequest.setHeaders({
58 accept: "application/json, text/event-stream",
59 authorization: `Bearer ${stdout.trim()}`,
60 ...(sessionId === null ? {} : { "mcp-session-id": sessionId }),
61 }),
62 HttpClientRequest.bodyJsonUnsafe({ jsonrpc: "2.0", ...message }),
63 ),
64 );
65 sessionId = response.headers["mcp-session-id"] ?? sessionId;
66 const body = yield* response.text;
67 if (response.status >= 300) {
68 return yield* new ScenarioError({ message: `MCP ${response.status}: ${body}` });
69 }
70 return body;
71 });
72 yield* send({
73 id: 1,
74 method: "initialize",
75 params: {
76 protocolVersion: "2025-06-18",
77 capabilities: {},
78 clientInfo: { name: "scenario", version: "1" },
79 },
80 });
81 yield* send({ method: "notifications/initialized" });
82 const result = yield* send({
83 id: 2,
84 method: "tools/call",
85 params: { name: "t3_thread_organize", arguments: { threadId, action: "archive" } },
86 });
87 if (result.includes('"isError":true')) {
88 return yield* new ScenarioError({ message: `Archive failed: ${result}` });
89 }
90 }, Effect.provide(FetchHttpClient.layer));
91
92 test(
93 "Settings Archived lists a thread archived elsewhere while it is open",
94 WEB,
95 async ({ t3, app, screen }) => {
96 const settings = await runPromise(approvalRequiredThreads);
97 try {
98 await runPromise(settings.apply);
99 const project = await t3.project("demo");
100 const thread = await t3.thread({ project, title: "Archived elsewhere", messages: ["first"] });
101 await t3.open("/settings/archived");
102 await expect(screen.getByText("No archived threads")).toBeVisible();
103
104 await runPromise(archiveOverMcp(app.baseUrl ?? "", thread.threadId));
105
106 await expect(screen.getByRole("button", "Unarchive Archived elsewhere")).toBeVisible();
107 await app.screenshot("archived-list");
108 } finally {
109 await runPromise(settings.restore);
110 }
111 },
112 );