| 1 | import { describe, expect, it } from "@effect/vitest"; |
| 2 | import { EnvironmentId } from "@t3tools/contracts"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | |
| 5 | import { migrateLegacyConnectionCatalog } from "./migration"; |
| 6 | |
| 7 | describe("migrateLegacyConnectionCatalog", () => { |
| 8 | it.effect("migrates bearer and relay-managed connections into the new catalog", () => |
| 9 | Effect.gen(function* () { |
| 10 | const bearerEnvironmentId = EnvironmentId.make("bearer-environment"); |
| 11 | const relayEnvironmentId = EnvironmentId.make("relay-environment"); |
| 12 | const catalog = yield* migrateLegacyConnectionCatalog( |
| 13 | JSON.stringify({ |
| 14 | connections: [ |
| 15 | { |
| 16 | environmentId: bearerEnvironmentId, |
| 17 | environmentLabel: "Local Mac", |
| 18 | pairingUrl: "https://local.example.test/pair", |
| 19 | displayUrl: "https://local.example.test", |
| 20 | httpBaseUrl: "https://local.example.test", |
| 21 | wsBaseUrl: "wss://local.example.test", |
| 22 | bearerToken: "bearer-token", |
| 23 | authenticationMethod: "bearer", |
| 24 | }, |
| 25 | { |
| 26 | environmentId: relayEnvironmentId, |
| 27 | environmentLabel: "Cloud Mac", |
| 28 | pairingUrl: "https://relay.example.test", |
| 29 | displayUrl: "https://relay.example.test", |
| 30 | httpBaseUrl: "https://relay.example.test", |
| 31 | wsBaseUrl: "wss://relay.example.test", |
| 32 | bearerToken: null, |
| 33 | authenticationMethod: "dpop", |
| 34 | relayManaged: true, |
| 35 | }, |
| 36 | ], |
| 37 | }), |
| 38 | ); |
| 39 | |
| 40 | expect(catalog.targets).toHaveLength(2); |
| 41 | expect( |
| 42 | catalog.targets.find((target) => target.environmentId === bearerEnvironmentId)?._tag, |
| 43 | ).toBe("BearerConnectionTarget"); |
| 44 | expect( |
| 45 | catalog.targets.find((target) => target.environmentId === relayEnvironmentId)?._tag, |
| 46 | ).toBe("RelayConnectionTarget"); |
| 47 | expect(catalog.profiles).toHaveLength(1); |
| 48 | expect(catalog.credentials).toHaveLength(1); |
| 49 | expect(catalog.credentials[0]?.credential).toMatchObject({ |
| 50 | _tag: "BearerConnectionCredential", |
| 51 | token: "bearer-token", |
| 52 | }); |
| 53 | }), |
| 54 | ); |
| 55 | |
| 56 | it.effect("drops invalid legacy bearer entries without credentials", () => |
| 57 | Effect.gen(function* () { |
| 58 | const catalog = yield* migrateLegacyConnectionCatalog( |
| 59 | JSON.stringify({ |
| 60 | connections: [ |
| 61 | { |
| 62 | environmentId: EnvironmentId.make("invalid-bearer"), |
| 63 | environmentLabel: "Invalid", |
| 64 | pairingUrl: "https://invalid.example.test/pair", |
| 65 | displayUrl: "https://invalid.example.test", |
| 66 | httpBaseUrl: "https://invalid.example.test", |
| 67 | wsBaseUrl: "wss://invalid.example.test", |
| 68 | bearerToken: null, |
| 69 | authenticationMethod: "bearer", |
| 70 | }, |
| 71 | ], |
| 72 | }), |
| 73 | ); |
| 74 | |
| 75 | expect(catalog.targets).toEqual([]); |
| 76 | }), |
| 77 | ); |
| 78 | }); |