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
| 1 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 2 | import { CommandId, ThreadId } from "@t3tools/contracts"; |
| 3 | import { expect } from "e2e"; |
| 4 | import * as SecureRandom from "effect/crypto/SecureRandom"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Schema from "effect/Schema"; |
| 7 | |
| 8 | import { T3_HOME } from "../e2e.config.ts"; |
| 9 | import { SECOND_HOME, startSecondServer } from "./second-server.ts"; |
| 10 | import { makeState, rpcClient, runPromise, test, WEB } from "./t3.ts"; |
| 11 | |
| 12 | // A shell keeps an environment's 25 newest settled threads; older ones come from search. |
| 13 | const NEWER_SETTLED = 25; |
| 14 | |
| 15 | const uuid = Effect.flatMap(SecureRandom.SecureRandom, (random) => random.uuidV4()); |
| 16 | const decodePairing = Schema.decodeEffect( |
| 17 | Schema.fromJsonString(Schema.Struct({ credential: Schema.String })), |
| 18 | ); |
| 19 | |
| 20 | const settle = Effect.fn("scenario.settle")( |
| 21 | function* (home: string, origin: string, threadIds: ReadonlyArray<string>) { |
| 22 | const client = yield* rpcClient(home, origin); |
| 23 | for (const threadId of threadIds) { |
| 24 | yield* client["orchestration.dispatchCommand"]({ |
| 25 | type: "thread.settle", |
| 26 | commandId: CommandId.make(yield* uuid), |
| 27 | threadId: ThreadId.make(threadId), |
| 28 | }); |
| 29 | } |
| 30 | }, |
| 31 | Effect.scoped, |
| 32 | Effect.provide(NodeCrypto.layer), |
| 33 | ); |
| 34 | |
| 35 | /** Turns the Linked environments beta on for both servers and links the first to the second. */ |
| 36 | const linkEnvironments = Effect.fn("scenario.linkEnvironments")(function* ( |
| 37 | primaryOrigin: string, |
| 38 | secondOrigin: string, |
| 39 | ) { |
| 40 | const primary = yield* rpcClient(T3_HOME, primaryOrigin); |
| 41 | const second = yield* rpcClient(SECOND_HOME, secondOrigin); |
| 42 | for (const client of [primary, second]) { |
| 43 | yield* client["server.updateSettings"]({ patch: { linkedEnvironmentsEnabled: true } }); |
| 44 | } |
| 45 | const pairing = yield* makeState(SECOND_HOME).cli(["auth", "pairing", "create", "--json"]); |
| 46 | const { credential } = yield* decodePairing(pairing.stdout); |
| 47 | yield* primary["peerLinks.link"]({ |
| 48 | url: secondOrigin, |
| 49 | pairingCode: credential, |
| 50 | access: "approval-required", |
| 51 | }); |
| 52 | }, Effect.scoped); |
| 53 | |
| 54 | test( |
| 55 | "The @ picker finds an older settled thread on a linked machine", |
| 56 | WEB, |
| 57 | async ({ t3, app, screen }) => { |
| 58 | const project = await t3.project("primary"); |
| 59 | const current = await t3.thread({ project, title: "Current work", messages: ["first"] }); |
| 60 | const server = startSecondServer(app.baseUrl ?? ""); |
| 61 | try { |
| 62 | const { host, token } = await server.ready; |
| 63 | await runPromise(linkEnvironments(app.baseUrl ?? "", host)); |
| 64 | const second = makeState(SECOND_HOME).state; |
| 65 | const linkedProject = await second.project("linked"); |
| 66 | // Settled first, so the newer ones push it out of the linked machine's shell. |
| 67 | const titles = [ |
| 68 | "Ancient design notes", |
| 69 | ...Array.from({ length: NEWER_SETTLED }, (_, index) => `Newer ${index + 1}`), |
| 70 | ]; |
| 71 | const threadIds: Array<string> = []; |
| 72 | for (const title of titles) { |
| 73 | threadIds.push( |
| 74 | (await second.thread({ project: linkedProject, title, messages: ["first"] })).threadId, |
| 75 | ); |
| 76 | } |
| 77 | await runPromise(settle(SECOND_HOME, host, threadIds)); |
| 78 | |
| 79 | await t3.open("/settings/connections"); |
| 80 | await screen.getByRole("button", "Add environment").tap(); |
| 81 | const dialog = screen.getByRole("dialog"); |
| 82 | await dialog.getByPlaceholder("backend.example.com").fill(host); |
| 83 | await dialog.getByPlaceholder("PAIRCODE").fill(token); |
| 84 | await dialog.getByRole("button", "Add environment").tap(); |
| 85 | await expect(screen.getByRole("button", "More actions for Scenario")).toBeVisible({ |
| 86 | timeout: 60_000, |
| 87 | }); |
| 88 | |
| 89 | await t3.open(current); |
| 90 | await screen.getByRole("textbox").fill("@Ancient"); |
| 91 | try { |
| 92 | await expect( |
| 93 | screen.getByRole("option", "Ancient design notes Thread on Scenario", { exact: false }), |
| 94 | ).toBeVisible(); |
| 95 | } finally { |
| 96 | await app.screenshot("ancient-mention"); |
| 97 | } |
| 98 | } finally { |
| 99 | await server.stop(); |
| 100 | } |
| 101 | }, |
| 102 | ); |