1183 lines · 40.7 KB · TypeScript
RawDownload
import { beforeEach, vi } from "vite-plus/test";import { describe, expect, it } from "@effect/vitest";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";import { EnvironmentId } from "@t3tools/contracts";import { RelayMobileClientId } from "@t3tools/contracts/relay";import { ManagedRelay } from "@t3tools/client-runtime/relay";import { remoteHttpClientLayer } from "@t3tools/client-runtime/rpc";import { HttpClient } from "effect/unstable/http";import { MobilePreferencesStore } from "../../persistence/mobile-preferences";import { MobileStorage } from "../../persistence/mobile-storage"; import {  cloudEnvironmentsPendingStatus,  linkEnvironmentToCloud,  linkEnvironmentToCloudWithPreference,  connectCloudEnvironment,  listCloudEnvironments,  listCloudEnvironmentsWithStatus,  normalizeRelayBaseUrl,  refreshCloudEnvironmentConnection,} from "./linkEnvironment"; vi.mock("expo-constants", () => ({  default: {    expoConfig: {      extra: {        relay: {          url: "https://relay.example.test",        },      },    },  },})); vi.mock("react-native", () => ({  Platform: {    OS: "ios",  },})); vi.mock("expo-secure-store", () => ({  deleteItemAsync: vi.fn(),  getItemAsync: vi.fn(),  setItemAsync: vi.fn(),})); const loadPreferences = vi.fn(() => Effect.succeed({})); const savedConnection = {  environmentId: EnvironmentId.make("env-1"),  environmentLabel: "Desktop",  pairingUrl: "https://desktop.example.test/",  displayUrl: "https://desktop.example.test/",  httpBaseUrl: "https://desktop.example.test/",  wsBaseUrl: "wss://desktop.example.test/ws",  bearerToken: "local-bearer",}; const stableClerkToken = "eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJzdWIiOiJ1c2VyXzEyMyJ9.test"; const createProofMock = vi.fn(  (input: { readonly method: string; readonly url: string; readonly accessToken?: string }) =>    Effect.succeed(`dpop:${input.method}:${input.url}`),);const testDpopSignerLayer = Layer.succeed(  ManagedRelay.ManagedRelayDpopSigner,  ManagedRelay.ManagedRelayDpopSigner.of({    thumbprint: Effect.succeed("client-proof-key-thumbprint"),    createProof: (input) => createProofMock(input),  }),); function cloudClientLayer() {  const httpClientLayer = remoteHttpClientLayer((input, init) => globalThis.fetch(input, init));  return Layer.mergeAll(    httpClientLayer,    Layer.succeed(      MobilePreferencesStore,      MobilePreferencesStore.of({        load: loadPreferences(),        savePatch: (patch) => Effect.succeed(patch),        update: () => Effect.succeed({}),      }),    ),    Layer.succeed(      MobileStorage,      MobileStorage.of({        loadSavedConnections: Effect.succeed([]),        saveConnection: () => Effect.void,        clearSavedConnection: () => Effect.void,        loadOrCreateAgentAwarenessDeviceId: Effect.succeed("device-1"),        loadAgentAwarenessDeviceId: Effect.succeed("device-1"),        loadAgentAwarenessRegistrationRecord: Effect.succeed(null),        saveAgentAwarenessRegistrationRecord: () => Effect.void,        clearAgentAwarenessRegistrationRecord: Effect.void,        loadRecentThreadShortcuts: Effect.succeed([]),        saveRecentThreadShortcuts: () => Effect.void,      }),    ),    ManagedRelay.layer({      relayUrl: "https://relay.example.test",      clientId: RelayMobileClientId,    }).pipe(Layer.provideMerge(testDpopSignerLayer), Layer.provide(httpClientLayer)),  );} const withCloudServices = <A, E>(  effect: Effect.Effect<    A,    E,    | HttpClient.HttpClient    | ManagedRelay.ManagedRelayClient    | ManagedRelay.ManagedRelayDpopSigner    | MobilePreferencesStore    | MobileStorage  >,) => effect.pipe(Effect.provide(cloudClientLayer())); function validLinkProof() {  return "signed-environment-link-jwt";} function validLinkResponse(environmentId = "env-1") {  return {    ok: true,    environmentId,    endpoint: {      httpBaseUrl: "https://managed.example.test/",      wsBaseUrl: "wss://managed.example.test/ws",      providerKind: "cloudflare_tunnel",    },    endpointRuntime: {      providerKind: "cloudflare_tunnel",      connectorToken: "connector-token",    },    relayIssuer: "https://relay.example.test",    cloudUserId: "user_123",    environmentCredential: "environment-credential",    cloudMintPublicKey: "cloud-mint-public-key",  };} function validLinkChallengeResponse() {  return {    challenge: "link-challenge",    expiresAt: "2026-05-25T00:05:00.000Z",  };} function requestBodyText(body: BodyInit | null | undefined): string {  return body instanceof Uint8Array ? new TextDecoder().decode(body) : String(body ?? "");} function validDpopAccessTokenResponse(scope = "environment:status environment:connect") {  return {    access_token: "relay-dpop-token",    issued_token_type: "urn:ietf:params:oauth:token-type:access_token",    token_type: "DPoP",    expires_in: 300,    scope,  };} function listedEnvironment(environmentId: string) {  return {    environmentId: EnvironmentId.make(environmentId),    label: "Desktop",    endpoint: {      httpBaseUrl: `https://${environmentId}.example.test/`,      wsBaseUrl: `wss://${environmentId}.example.test/ws`,      providerKind: "cloudflare_tunnel" as const,    },    linkedAt: "2026-05-25T00:00:00.000Z",  };} describe("mobile cloud link environment client", () => {  beforeEach(() => {    vi.restoreAllMocks();    createProofMock.mockClear();    loadPreferences.mockClear();  });   it("normalizes configured relay base URLs before building DPoP-bound requests", () => {    expect(normalizeRelayBaseUrl(" https://relay.example.test/// ")).toBe(      "https://relay.example.test",    );    expect(normalizeRelayBaseUrl("   ")).toBeNull();  });   it("makes linked environments visible while their status is still loading", () => {    expect(cloudEnvironmentsPendingStatus([listedEnvironment("env-1")])).toMatchObject([      {        environment: { environmentId: "env-1", label: "Desktop" },        status: null,        statusError: "Checking status...",      },    ]);  });   it.effect("decodes relay environment list responses before returning records", () =>    Effect.gen(function* () {      vi.stubGlobal(        "fetch",        vi.fn(() =>          Promise.resolve(            Response.json({              environments: [                {                  environmentId: "env-1",                  label: "Desktop",                  endpoint: {                    httpBaseUrl: "https://desktop.example.test/",                    wsBaseUrl: "wss://desktop.example.test/ws",                    providerKind: "cloudflare_tunnel",                  },                  linkedAt: "2026-05-25T00:00:00.000Z",                },              ],            }),          ),        ),      );       const records = yield* withCloudServices(        listCloudEnvironments({ clerkToken: "clerk-token" }),      );      expect(records).toEqual([        {          environmentId: "env-1",          label: "Desktop",          endpoint: {            httpBaseUrl: "https://desktop.example.test/",            wsBaseUrl: "wss://desktop.example.test/ws",            providerKind: "cloudflare_tunnel",          },          linkedAt: "2026-05-25T00:00:00.000Z",        },      ]);    }),  );   it.effect("rejects malformed relay environment list responses", () =>    Effect.gen(function* () {      vi.stubGlobal(        "fetch",        vi.fn(() =>          Promise.resolve(            Response.json({              environments: [                {                  environmentId: "env-1",                  label: "Desktop",                  endpoint: {                    httpBaseUrl: "",                    wsBaseUrl: "wss://desktop.example.test/ws",                    providerKind: "cloudflare_tunnel",                  },                  linkedAt: "2026-05-25T00:00:00.000Z",                },              ],            }),          ),        ),      );       const error = yield* withCloudServices(        listCloudEnvironments({ clerkToken: "clerk-token" }),      ).pipe(Effect.flip);      expect(error).toMatchObject({        _tag: "CloudEnvironmentLinkError",        message: "https://relay.example.test/v1/environments failed",      });    }),  );   it.effect("loads signed status for each advertised cloud environment", () =>    Effect.gen(function* () {      const fetchMock = vi.fn((url: string | URL, _init?: RequestInit) => {        if (String(url) === "https://relay.example.test/v1/environments") {          return Promise.resolve(            Response.json({              environments: [                {                  environmentId: "env-1",                  label: "Desktop",                  endpoint: {                    httpBaseUrl: "https://desktop.example.test/",                    wsBaseUrl: "wss://desktop.example.test/ws",                    providerKind: "cloudflare_tunnel",                  },                  linkedAt: "2026-05-25T00:00:00.000Z",                },              ],            }),          );        }        if (String(url) === "https://relay.example.test/v1/client/dpop-token") {          return Promise.resolve(Response.json(validDpopAccessTokenResponse()));        }        expect(String(url)).toBe("https://relay.example.test/v1/environments/env-1/status");        return Promise.resolve(          Response.json({            environmentId: "env-1",            endpoint: {              httpBaseUrl: "https://desktop.example.test/",              wsBaseUrl: "wss://desktop.example.test/ws",              providerKind: "cloudflare_tunnel",            },            status: "online",            checkedAt: "2026-05-25T00:01:00.000Z",            descriptor: {              environmentId: "env-1",              label: "Desktop",              platform: { os: "darwin", arch: "arm64" },              serverVersion: "0.0.0-test",              capabilities: { repositoryIdentity: true },            },          }),        );      });      vi.stubGlobal("fetch", fetchMock);       const records = yield* withCloudServices(        listCloudEnvironmentsWithStatus({ clerkToken: "clerk-token" }),      );      expect(records).toMatchObject([        {          environment: {            environmentId: "env-1",            label: "Desktop",          },          status: {            environmentId: "env-1",            status: "online",            checkedAt: "2026-05-25T00:01:00.000Z",          },          statusError: null,        },      ]);      expect(String(fetchMock.mock.calls[2]?.[0])).toBe(        "https://relay.example.test/v1/environments/env-1/status",      );      expect(fetchMock.mock.calls[2]?.[1]?.method).toBe("POST");      const statusHeaders = new Headers(fetchMock.mock.calls[2]?.[1]?.headers);      expect(statusHeaders.get("authorization")).toBe("DPoP relay-dpop-token");      expect(statusHeaders.get("dpop")).toBe(        "dpop:POST:https://relay.example.test/v1/environments/env-1/status",      );      expect(createProofMock).toHaveBeenCalledWith({        method: "POST",        url: "https://relay.example.test/v1/environments/env-1/status",        accessToken: "relay-dpop-token",      });    }),  );   it.effect("reuses one valid DPoP access token while probing multiple environment statuses", () =>    Effect.gen(function* () {      const fetchMock = vi.fn((url: string | URL, _init?: RequestInit) => {        if (String(url).endsWith("/v1/environments")) {          return Promise.resolve(            Response.json({              environments: [listedEnvironment("env-1"), listedEnvironment("env-2")],            }),          );        }        if (String(url).endsWith("/v1/client/dpop-token")) {          return Promise.resolve(Response.json(validDpopAccessTokenResponse()));        }        const environmentId = String(url).includes("/env-1/") ? "env-1" : "env-2";        return Promise.resolve(          Response.json({            environmentId,            endpoint: listedEnvironment(environmentId).endpoint,            status: "online",            checkedAt: "2026-05-25T00:01:00.000Z",            descriptor: {              environmentId,              label: "Desktop",              platform: { os: "darwin", arch: "arm64" },              serverVersion: "0.0.0-test",              capabilities: { repositoryIdentity: true },            },          }),        );      });      vi.stubGlobal("fetch", fetchMock);       yield* withCloudServices(listCloudEnvironmentsWithStatus({ clerkToken: stableClerkToken }));       expect(        fetchMock.mock.calls.filter(([url]) => String(url).endsWith("/v1/client/dpop-token")),      ).toHaveLength(1);    }),  );   it.effect("reuses the status-and-connect token when connecting from the cloud list", () =>    Effect.gen(function* () {      const fetchMock = vi.fn((url: string | URL, _init?: RequestInit) => {        if (String(url).endsWith("/v1/environments")) {          return Promise.resolve(            Response.json({              environments: [listedEnvironment("env-1")],            }),          );        }        if (String(url).endsWith("/v1/client/dpop-token")) {          return Promise.resolve(Response.json(validDpopAccessTokenResponse()));        }        if (String(url).endsWith("/status")) {          return Promise.resolve(            Response.json({              environmentId: "env-1",              endpoint: listedEnvironment("env-1").endpoint,              status: "online",              checkedAt: "2026-05-25T00:01:00.000Z",              descriptor: {                environmentId: "env-1",                label: "Desktop",                platform: { os: "darwin", arch: "arm64" },                serverVersion: "0.0.0-test",                capabilities: { repositoryIdentity: true },              },            }),          );        }        if (String(url).endsWith("/connect")) {          return Promise.resolve(            Response.json({              environmentId: "env-1",              endpoint: listedEnvironment("env-1").endpoint,              credential: "one-time-cloud-credential",              expiresAt: "2026-05-25T00:05:00.000Z",            }),          );        }        if (String(url).endsWith("/.well-known/t3/environment")) {          return Promise.resolve(            Response.json({              environmentId: "env-1",              label: "Desktop",              platform: { os: "darwin", arch: "arm64" },              serverVersion: "0.0.0-test",              capabilities: { repositoryIdentity: true },            }),          );        }        return Promise.resolve(          Response.json({            access_token: "environment-dpop-token",            issued_token_type: "urn:ietf:params:oauth:token-type:access_token",            token_type: "DPoP",            expires_in: 3600,            scope: "orchestration:read orchestration:operate terminal:operate review:write",          }),        );      });      vi.stubGlobal("fetch", fetchMock);       yield* withCloudServices(        Effect.gen(function* () {          const records = yield* listCloudEnvironmentsWithStatus({            clerkToken: stableClerkToken,          });          yield* connectCloudEnvironment({            clerkToken: stableClerkToken,            environment: records[0]!.environment,          });        }),      );       expect(        fetchMock.mock.calls.filter(([url]) => String(url).endsWith("/v1/client/dpop-token")),      ).toHaveLength(1);      const exchangeRequest = fetchMock.mock.calls.find(([url]) =>        String(url).endsWith("/v1/client/dpop-token"),      )?.[1];      expect(new URLSearchParams(requestBodyText(exchangeRequest?.body)).get("scope")).toBe(        "environment:status environment:connect",      );      const environmentTokenRequest = fetchMock.mock.calls.find(([url]) =>        String(url).endsWith("/oauth/token"),      )?.[1];      const environmentTokenBody = new URLSearchParams(        requestBodyText(environmentTokenRequest?.body),      );      expect(environmentTokenBody.get("client_label")).toBe("T3 Code Mobile");      expect(environmentTokenBody.get("client_device_type")).toBe("mobile");      expect(environmentTokenBody.get("client_os")).toBe("iOS");    }),  );   it.effect("keeps advertised environments visible when status probing fails", () =>    Effect.gen(function* () {      vi.stubGlobal(        "fetch",        vi.fn((url: string | URL) => {          if (String(url) === "https://relay.example.test/v1/environments") {

Lines 1–500 of 1183.