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 | import { assert, describe, it } from "@effect/vitest"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 4 | import * as FileSystem from "effect/FileSystem"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import { beforeEach, vi } from "vite-plus/test"; |
| 7 | |
| 8 | const { handleMock, netFetchMock, unhandleMock } = vi.hoisted(() => ({ |
| 9 | handleMock: vi.fn(), |
| 10 | netFetchMock: vi.fn(), |
| 11 | unhandleMock: vi.fn(), |
| 12 | })); |
| 13 | |
| 14 | vi.mock("electron", () => ({ |
| 15 | net: { fetch: netFetchMock }, |
| 16 | protocol: { handle: handleMock, unhandle: unhandleMock }, |
| 17 | })); |
| 18 | |
| 19 | import * as ElectronProtocol from "./ElectronProtocol.ts"; |
| 20 | |
| 21 | const layerProtocol = ElectronProtocol.layer.pipe(Layer.provide(NodeServices.layer)); |
| 22 | |
| 23 | describe("ElectronProtocol", () => { |
| 24 | beforeEach(() => { |
| 25 | handleMock.mockReset(); |
| 26 | netFetchMock.mockReset(); |
| 27 | unhandleMock.mockReset(); |
| 28 | }); |
| 29 | |
| 30 | it.effect("serves the bundled client from disk without a backend", () => |
| 31 | Effect.gen(function* () { |
| 32 | const fileSystem = yield* FileSystem.FileSystem; |
| 33 | const directory = yield* fileSystem.makeTempDirectoryScoped(); |
| 34 | yield* fileSystem.writeFileString(`${directory}/index.html`, "<html>app</html>"); |
| 35 | yield* fileSystem.writeFileString(`${directory}/app.js`, "export default 1;"); |
| 36 | let handler: ((request: Request) => Promise<Response>) | undefined; |
| 37 | handleMock.mockImplementation((_scheme, nextHandler) => { |
| 38 | handler = nextHandler; |
| 39 | }); |
| 40 | const protocol = yield* ElectronProtocol.ElectronProtocol; |
| 41 | yield* protocol.registerDesktopProtocol({ |
| 42 | scheme: "t3code", |
| 43 | assetDirectory: directory, |
| 44 | clerkFrontendApiHostname: undefined, |
| 45 | }); |
| 46 | const request = (pathname: string, init?: RequestInit) => |
| 47 | Effect.promise(() => handler!(new Request(`t3code://app${pathname}`, init))); |
| 48 | |
| 49 | const page = yield* request("/settings/connections"); |
| 50 | assert.equal(yield* Effect.promise(() => page.text()), "<html>app</html>"); |
| 51 | assert.include(page.headers.get("content-security-policy") ?? "", "default-src 'self'"); |
| 52 | const dottedRoute = yield* request("/environment/thread.with.dots", { |
| 53 | headers: { accept: "text/html" }, |
| 54 | }); |
| 55 | assert.equal(yield* Effect.promise(() => dottedRoute.text()), "<html>app</html>"); |
| 56 | |
| 57 | const script = yield* request("/app.js?v=1"); |
| 58 | assert.equal(yield* Effect.promise(() => script.text()), "export default 1;"); |
| 59 | assert.include(script.headers.get("content-type") ?? "", "javascript"); |
| 60 | |
| 61 | assert.equal((yield* request("/missing.js")).status, 404); |
| 62 | assert.equal((yield* request("/%2e%2e%2fsecret.txt")).status, 404); |
| 63 | assert.equal((yield* request("/%invalid")).status, 400); |
| 64 | assert.equal((yield* request("/", { method: "POST" })).status, 405); |
| 65 | assert.equal(netFetchMock.mock.calls.length, 0); |
| 66 | }).pipe(Effect.provide(Layer.merge(layerProtocol, NodeServices.layer))), |
| 67 | ); |
| 68 | |
| 69 | it.effect("proxies the stable renderer origin to the current app server", () => |
| 70 | Effect.gen(function* () { |
| 71 | let handler: ((request: Request) => Promise<Response>) | undefined; |
| 72 | handleMock.mockImplementation((_scheme, nextHandler) => { |
| 73 | handler = nextHandler; |
| 74 | }); |
| 75 | netFetchMock.mockResolvedValue(new Response("ok")); |
| 76 | |
| 77 | yield* Effect.scoped( |
| 78 | Effect.gen(function* () { |
| 79 | const protocol = yield* ElectronProtocol.ElectronProtocol; |
| 80 | yield* protocol.registerDesktopProtocol({ |
| 81 | scheme: "t3code-dev", |
| 82 | targetOrigin: new URL("http://127.0.0.1:3773/"), |
| 83 | clerkFrontendApiHostname: "clerk.t3.codes", |
| 84 | }); |
| 85 | assert.isDefined(handler); |
| 86 | |
| 87 | const response = yield* Effect.promise(() => |
| 88 | handler!( |
| 89 | new Request("t3code-dev://app/api/health?verbose=1", { |
| 90 | headers: { |
| 91 | accept: "application/json", |
| 92 | origin: "t3code-dev://app", |
| 93 | referer: "t3code-dev://app/", |
| 94 | "sec-fetch-site": "same-origin", |
| 95 | }, |
| 96 | }), |
| 97 | ), |
| 98 | ); |
| 99 | assert.equal(yield* Effect.promise(() => response.text()), "ok"); |
| 100 | assert.include( |
| 101 | response.headers.get("content-security-policy") ?? "", |
| 102 | "script-src 'self' 'unsafe-inline' 'wasm-unsafe-eval' https://clerk.t3.codes https://challenges.cloudflare.com", |
| 103 | ); |
| 104 | assert.include( |
| 105 | response.headers.get("content-security-policy") ?? "", |
| 106 | "connect-src 'self' blob: http: https: ws: wss:", |
| 107 | ); |
| 108 | assert.include( |
| 109 | response.headers.get("content-security-policy") ?? "", |
| 110 | "img-src 'self' t3code-dev: blob: data: http: https:", |
| 111 | ); |
| 112 | assert.include( |
| 113 | response.headers.get("content-security-policy") ?? "", |
| 114 | "font-src 'self' t3code-dev: data:", |
| 115 | ); |
| 116 | }), |
| 117 | ); |
| 118 | |
| 119 | assert.deepEqual( |
| 120 | handleMock.mock.calls.map((call) => call[0]), |
| 121 | ["t3code-dev"], |
| 122 | ); |
| 123 | assert.equal(netFetchMock.mock.calls[0]?.[0], "http://127.0.0.1:3773/api/health?verbose=1"); |
| 124 | const forwardedHeaders = new Headers(netFetchMock.mock.calls[0]?.[1]?.headers); |
| 125 | assert.equal(forwardedHeaders.get("accept"), "application/json"); |
| 126 | assert.isNull(forwardedHeaders.get("origin")); |
| 127 | assert.isNull(forwardedHeaders.get("referer")); |
| 128 | assert.isNull(forwardedHeaders.get("sec-fetch-site")); |
| 129 | assert.deepEqual(unhandleMock.mock.calls, [["t3code-dev"]]); |
| 130 | }).pipe(Effect.provide(layerProtocol)), |
| 131 | ); |
| 132 | |
| 133 | it.effect("keeps proxied paths on the configured server", () => |
| 134 | Effect.gen(function* () { |
| 135 | let handler: ((request: Request) => Promise<Response>) | undefined; |
| 136 | handleMock.mockImplementation((_scheme, nextHandler) => { |
| 137 | handler = nextHandler; |
| 138 | }); |
| 139 | netFetchMock.mockImplementation(async () => new Response("ok")); |
| 140 | |
| 141 | yield* Effect.scoped( |
| 142 | Effect.gen(function* () { |
| 143 | const protocol = yield* ElectronProtocol.ElectronProtocol; |
| 144 | yield* protocol.registerDesktopProtocol({ |
| 145 | scheme: "t3code-dev", |
| 146 | targetOrigin: new URL("http://127.0.0.1:5733/"), |
| 147 | clerkFrontendApiHostname: undefined, |
| 148 | }); |
| 149 | yield* Effect.promise(() => |
| 150 | handler!(new Request("t3code-dev://app//example.com/src/main.tsx?t=1")), |
| 151 | ); |
| 152 | yield* Effect.promise(() => handler!(new Request("t3code-dev://app/\\example.com/"))); |
| 153 | }), |
| 154 | ); |
| 155 | |
| 156 | assert.deepEqual( |
| 157 | netFetchMock.mock.calls.map((call) => call[0]), |
| 158 | [ |
| 159 | "http://127.0.0.1:5733//example.com/src/main.tsx?t=1", |
| 160 | "http://127.0.0.1:5733//example.com/", |
| 161 | ], |
| 162 | ); |
| 163 | }).pipe(Effect.provide(layerProtocol)), |
| 164 | ); |
| 165 | |
| 166 | it.effect("rejects custom protocol requests for another host", () => |
| 167 | Effect.gen(function* () { |
| 168 | let handler: ((request: Request) => Promise<Response>) | undefined; |
| 169 | handleMock.mockImplementation((_scheme, nextHandler) => { |
| 170 | handler = nextHandler; |
| 171 | }); |
| 172 | |
| 173 | const response = yield* Effect.scoped( |
| 174 | Effect.gen(function* () { |
| 175 | const protocol = yield* ElectronProtocol.ElectronProtocol; |
| 176 | yield* protocol.registerDesktopProtocol({ |
| 177 | scheme: "t3code", |
| 178 | targetOrigin: new URL("http://127.0.0.1:3773/"), |
| 179 | clerkFrontendApiHostname: undefined, |
| 180 | }); |
| 181 | return yield* Effect.promise(() => handler!(new Request("t3code://other/"))); |
| 182 | }), |
| 183 | ); |
| 184 | |
| 185 | assert.equal(response.status, 404); |
| 186 | assert.equal(netFetchMock.mock.calls.length, 0); |
| 187 | }).pipe(Effect.provide(layerProtocol)), |
| 188 | ); |
| 189 | }); |