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, 04:45 UTC
| 1 | // @effect-diagnostics nodeBuiltinImport:off - CLI integration exercises the filesystem boundary. |
| 2 | import * as NodePath from "node:path"; |
| 3 | |
| 4 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 5 | import { NodeHttpServer } from "@effect/platform-node"; |
| 6 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 7 | import type { ExecutionEnvironmentDescriptor } from "@t3tools/contracts"; |
| 8 | import * as NetService from "@t3tools/shared/Net"; |
| 9 | import { expect, it } from "@effect/vitest"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import * as FileSystem from "effect/FileSystem"; |
| 12 | import * as Layer from "effect/Layer"; |
| 13 | import * as TestConsole from "effect/testing/TestConsole"; |
| 14 | import { Command } from "effect/cli"; |
| 15 | |
| 16 | import { cli } from "../binCli.ts"; |
| 17 | import * as ServerConfig from "../config.ts"; |
| 18 | import { descriptorOf, servePeer } from "../peer/PeerLinks.testkit.ts"; |
| 19 | |
| 20 | const layerCliRuntime = Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer); |
| 21 | |
| 22 | const runCli = (args: ReadonlyArray<string>) => Command.runWith(cli, { version: "0.0.0" })(args); |
| 23 | |
| 24 | /** The batch reports printed so far; the logger writes to the same console. */ |
| 25 | const reports = TestConsole.logLines.pipe( |
| 26 | Effect.map((lines) => |
| 27 | lines.filter((line): line is string => typeof line === "string" && line.includes(" linked.")), |
| 28 | ), |
| 29 | ); |
| 30 | |
| 31 | /** A machine of the fleet, on a socket of its own. */ |
| 32 | const serveMachine = (descriptor: ExecutionEnvironmentDescriptor) => |
| 33 | Layer.build(Layer.fresh(NodeHttpServer.layerTest)).pipe( |
| 34 | Effect.flatMap((server) => |
| 35 | servePeer(descriptor, Layer.empty).pipe(Effect.provideContext(server)), |
| 36 | ), |
| 37 | ); |
| 38 | |
| 39 | it.effect("links a file of environments, reports each line, and fails when one did not link", () => |
| 40 | Effect.scoped( |
| 41 | Effect.gen(function* () { |
| 42 | const fs = yield* FileSystem.FileSystem; |
| 43 | const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-environment-cli-" }); |
| 44 | const { settingsPath } = yield* ServerConfig.deriveServerPaths(baseDir, undefined); |
| 45 | yield* fs.makeDirectory(NodePath.dirname(settingsPath), { recursive: true }); |
| 46 | yield* fs.writeFileString(settingsPath, JSON.stringify({ linkedEnvironmentsEnabled: true })); |
| 47 | |
| 48 | const one = yield* serveMachine(descriptorOf("environment-vm-1", "VM 1")); |
| 49 | const two = yield* serveMachine(descriptorOf("environment-vm-2", "VM 2")); |
| 50 | const three = yield* serveMachine(descriptorOf("environment-vm-3", "VM 3")); |
| 51 | const codeFor = (machine: typeof one) => |
| 52 | machine.auth |
| 53 | .issuePairingCredential({ access: "auto", purpose: "peer-link" }) |
| 54 | .pipe(Effect.map((pairing) => pairing.credential)); |
| 55 | const firstCode = yield* codeFor(one); |
| 56 | const thirdCode = yield* codeFor(three); |
| 57 | |
| 58 | const file = NodePath.join(baseDir, "fleet.txt"); |
| 59 | yield* fs.writeFileString( |
| 60 | file, |
| 61 | `# fleet\n${one.url} ${firstCode}\n\n${two.url} not-a-code\n${three.url} ${thirdCode}\n`, |
| 62 | ); |
| 63 | const link = (from: string) => |
| 64 | runCli(["environment", "link", "--from", from, "--access", "auto", "--base-dir", baseDir]); |
| 65 | |
| 66 | const failed = yield* link(file).pipe(Effect.flip); |
| 67 | expect(failed).toMatchObject({ _tag: "EnvironmentLinkBatchError", failed: 1, total: 3 }); |
| 68 | const report = (yield* reports).at(-1) ?? ""; |
| 69 | expect(report).toContain(`${one.url}: linked VM 1 (environment-vm-1) with auto access`); |
| 70 | expect(report).toContain(`${two.url}: code refused: `); |
| 71 | expect(report).toContain(`${three.url}: linked VM 3 (environment-vm-3) with auto access`); |
| 72 | expect(report).toContain("2 of 3 linked."); |
| 73 | // A pairing code is never printed, whether it worked or not. |
| 74 | expect(report).not.toContain(firstCode); |
| 75 | expect(report).not.toContain("not-a-code"); |
| 76 | expect((yield* one.linkedSessions).length).toBe(1); |
| 77 | expect(yield* two.linkedSessions).toEqual([]); |
| 78 | expect((yield* three.linkedSessions).length).toBe(1); |
| 79 | |
| 80 | // Retrying the whole file links the failure and leaves the linked ones alone, |
| 81 | // their codes unused: the first is spent and the third was never valid. |
| 82 | const retry = NodePath.join(baseDir, "retry.txt"); |
| 83 | yield* fs.writeFileString( |
| 84 | retry, |
| 85 | `${one.url} ${firstCode}\n${two.url} ${yield* codeFor(two)}\n${three.url} never-valid\n`, |
| 86 | ); |
| 87 | yield* link(retry); |
| 88 | const retried = (yield* reports).at(-1) ?? ""; |
| 89 | expect(retried).toContain( |
| 90 | `${one.url}: already linked as VM 1 (environment-vm-1), left as it is`, |
| 91 | ); |
| 92 | expect(retried).toContain(`${two.url}: linked VM 2 (environment-vm-2) with auto access`); |
| 93 | expect(retried).toContain( |
| 94 | `${three.url}: already linked as VM 3 (environment-vm-3), left as it is`, |
| 95 | ); |
| 96 | expect(retried).toContain("3 of 3 linked."); |
| 97 | expect((yield* one.linkedSessions).length).toBe(1); |
| 98 | expect((yield* two.linkedSessions).length).toBe(1); |
| 99 | expect((yield* three.linkedSessions).length).toBe(1); |
| 100 | |
| 101 | // An address may carry credentials; a result line names only its origin. |
| 102 | const withSecrets = (url: string) => |
| 103 | Object.assign(new URL(url), { |
| 104 | username: "alice", |
| 105 | password: "hunter2", |
| 106 | search: "?token=s3cret", |
| 107 | }).href; |
| 108 | const secrets = NodePath.join(baseDir, "secrets.txt"); |
| 109 | yield* fs.writeFileString( |
| 110 | secrets, |
| 111 | [ |
| 112 | `${withSecrets(one.url)} ${firstCode}`, |
| 113 | `${withSecrets("http://192.168.1.20:3773")} some-code`, |
| 114 | `ftp://alice:hunter2@files.example/?token=s3cret some-code`, |
| 115 | ].join("\n"), |
| 116 | ); |
| 117 | yield* link(secrets).pipe(Effect.flip); |
| 118 | const redacted = (yield* reports).at(-1) ?? ""; |
| 119 | expect(redacted).toContain(`${new URL(one.url).origin}: already linked as VM 1`); |
| 120 | expect(redacted).toContain("http://192.168.1.20:3773: unreachable: "); |
| 121 | expect(redacted).toContain("line 3: unreachable: "); |
| 122 | expect(redacted).not.toMatch(/alice|hunter2|s3cret/); |
| 123 | }), |
| 124 | ).pipe(Effect.provide(Layer.mergeAll(layerCliRuntime, TestConsole.layer))), |
| 125 | ); |