@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, 04:45 UTC

main
ts 85 lines 3.16 KB
Raw
1 import type { DesktopBridge } from "@t3tools/contracts";
2 import { afterEach, describe, expect, it, vi } from "@effect/vitest";
3 import * as Effect from "effect/Effect";
4 import * as Fiber from "effect/Fiber";
5 import { HttpClient } from "effect/http";
6
7 import { __resetDesktopPrimaryAuthForTests } from "./desktopAuth";
8 import * as PrimaryEnvironmentHttpLayer from "./httpLayer";
9
10 function createSignal<A>() {
11 let resolve!: (value: A) => void;
12 const promise = new Promise<A>((complete) => {
13 resolve = complete;
14 });
15 return { promise, resolve };
16 }
17
18 describe("primary environment HTTP layer", { concurrent: false }, () => {
19 afterEach(() => {
20 __resetDesktopPrimaryAuthForTests();
21 Reflect.deleteProperty(globalThis, "window");
22 vi.unstubAllGlobals();
23 });
24
25 it.effect("uses cookie credentials for browser primary environments", () => {
26 const fetchMock = vi.fn().mockResolvedValue(new Response(null, { status: 200 }));
27 vi.stubGlobal("fetch", fetchMock);
28 Object.defineProperty(globalThis, "window", {
29 configurable: true,
30 value: {
31 location: {
32 href: "http://127.0.0.1:3773/settings",
33 origin: "http://127.0.0.1:3773",
34 },
35 },
36 });
37
38 return Effect.gen(function* () {
39 yield* HttpClient.get("http://127.0.0.1:3773/api/auth/session");
40
41 const request = new Request(fetchMock.mock.calls[0]?.[0], fetchMock.mock.calls[0]?.[1]);
42 expect(request.credentials).toBe("include");
43 expect(request.headers.get("authorization")).toBeNull();
44 }).pipe(Effect.provide(PrimaryEnvironmentHttpLayer.layerForCurrentOrigin()));
45 });
46
47 it.effect("uses bearer auth without cookies for desktop-managed primaries", () => {
48 const requested = createSignal<void>();
49 const bearerToken = createSignal<string>();
50 const fetchMock = vi.fn().mockResolvedValue(new Response(null, { status: 200 }));
51 vi.stubGlobal("fetch", fetchMock);
52 Object.defineProperty(globalThis, "window", {
53 configurable: true,
54 value: {
55 location: { origin: "t3code://app" },
56 desktopBridge: {
57 getLocalEnvironmentBootstrap: () => ({
58 label: "Local environment",
59 httpBaseUrl: "http://127.0.0.1:3773",
60 wsBaseUrl: "ws://127.0.0.1:3773",
61 bootstrapToken: "desktop-bootstrap-token",
62 }),
63 getLocalEnvironmentBearerToken: () => {
64 requested.resolve(undefined);
65 return bearerToken.promise;
66 },
67 } as unknown as DesktopBridge,
68 },
69 });
70
71 return Effect.gen(function* () {
72 const requestFiber = yield* HttpClient.get(
73 "http://127.0.0.1:3773/api/connect/link-state",
74 ).pipe(Effect.forkChild);
75 yield* Effect.promise(() => requested.promise);
76 expect(fetchMock).not.toHaveBeenCalled();
77 bearerToken.resolve("desktop-bearer-token");
78 yield* Fiber.join(requestFiber);
79
80 const request = new Request(fetchMock.mock.calls[0]?.[0], fetchMock.mock.calls[0]?.[1]);
81 expect(request.credentials).not.toBe("include");
82 expect(request.headers.get("authorization")).toBe("Bearer desktop-bearer-token");
83 }).pipe(Effect.provide(PrimaryEnvironmentHttpLayer.layerForCurrentOrigin()));
84 });
85 });