The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | import * as Exit from "effect/Exit"; |
| 3 | import * as Schema from "effect/Schema"; |
| 4 | |
| 5 | import { AssetCreateUrlResult, AttachmentCreateUploadUrlResult } from "./assets.ts"; |
| 6 | import { ORCHESTRATION_V2_WS_METHODS } from "./orchestrationV2.ts"; |
| 7 | import { WS_METHODS, WsRpcGroup } from "./rpc.ts"; |
| 8 | |
| 9 | describe("WebSocket RPC contracts", () => { |
| 10 | it("only accepts asset and upload URLs on their own server routes", () => { |
| 11 | const decodeAsset = Schema.decodeUnknownExit(AssetCreateUrlResult); |
| 12 | const decodeUpload = Schema.decodeUnknownExit(AttachmentCreateUploadUrlResult); |
| 13 | const expiresAt = 10_000; |
| 14 | |
| 15 | expect( |
| 16 | Exit.isSuccess( |
| 17 | decodeAsset({ relativeUrl: "/api/assets/token.sig/my%20file.png", expiresAt }), |
| 18 | ), |
| 19 | ).toBe(true); |
| 20 | expect( |
| 21 | Exit.isSuccess( |
| 22 | decodeUpload({ |
| 23 | relativeUrl: "/api/attachments/upload/token.sig", |
| 24 | expiresAt, |
| 25 | attachmentId: "attachment", |
| 26 | }), |
| 27 | ), |
| 28 | ).toBe(true); |
| 29 | |
| 30 | for (const relativeUrl of [ |
| 31 | "data:text/plain,hello", |
| 32 | "file:///tmp/document", |
| 33 | "https://other.example/document", |
| 34 | "//other.example/document", |
| 35 | "/api/assets/token/../../../other", |
| 36 | ]) { |
| 37 | expect(Exit.isFailure(decodeAsset({ relativeUrl, expiresAt }))).toBe(true); |
| 38 | expect( |
| 39 | Exit.isFailure(decodeUpload({ relativeUrl, expiresAt, attachmentId: "attachment" })), |
| 40 | ).toBe(true); |
| 41 | } |
| 42 | }); |
| 43 | |
| 44 | it("rejects server-internal commands sent to dispatchCommand", () => { |
| 45 | const dispatchCommand = WsRpcGroup.requests.get(ORCHESTRATION_V2_WS_METHODS.dispatchCommand); |
| 46 | if (dispatchCommand === undefined) throw new Error("dispatchCommand is not registered"); |
| 47 | const decode = Schema.decodeUnknownExit(dispatchCommand.payloadSchema); |
| 48 | |
| 49 | expect( |
| 50 | Exit.isFailure( |
| 51 | decode({ |
| 52 | type: "checkpoint.rollback.fail", |
| 53 | commandId: "forged-rollback-failure", |
| 54 | threadId: "thread-1", |
| 55 | requestId: "rollback-1", |
| 56 | message: "Forged failure.", |
| 57 | }), |
| 58 | ), |
| 59 | ).toBe(true); |
| 60 | expect( |
| 61 | Exit.isSuccess( |
| 62 | decode({ |
| 63 | type: "checkpoint.rollback", |
| 64 | commandId: "rollback-1", |
| 65 | threadId: "thread-1", |
| 66 | scopeId: "scope-1", |
| 67 | checkpointId: "checkpoint-1", |
| 68 | }), |
| 69 | ), |
| 70 | ).toBe(true); |
| 71 | }); |
| 72 | |
| 73 | it("lets the server pick a new worktree's folder", () => { |
| 74 | const createWorktree = WsRpcGroup.requests.get(WS_METHODS.vcsCreateWorktree); |
| 75 | if (createWorktree === undefined) throw new Error("vcs.createWorktree is not registered"); |
| 76 | const decode = Schema.decodeUnknownExit(createWorktree.payloadSchema); |
| 77 | const request = { cwd: "/repo", refName: "main", newRefName: "feature/x" }; |
| 78 | |
| 79 | expect(Exit.isSuccess(decode({ ...request, path: null }))).toBe(true); |
| 80 | expect(Exit.isFailure(decode({ ...request, path: "/repo/.git/hooks" }))).toBe(true); |
| 81 | }); |
| 82 | }); |