|
| 1 | +import { describe, expect, it } from "@effect/vitest"; |
| 2 | +import { Effect, Layer } from "effect"; |
| 3 | + |
| 4 | +import { v2ImportAnswer, type V2ImportSource } from "./v2-import"; |
| 5 | +import { |
| 6 | + liveV2MovedSource, |
| 7 | + parseV2Moved, |
| 8 | + v2Moved, |
| 9 | + V2_MOVED_AT_KEY, |
| 10 | + V2_MOVED_SLUG_KEY, |
| 11 | + type V2MovedSource, |
| 12 | +} from "./v2-moved"; |
| 13 | +import { WorkOSClient, type WorkOSClientService } from "./workos"; |
| 14 | +import { WorkOSError } from "./errors"; |
| 15 | + |
| 16 | +const USER = "user_signed_in"; |
| 17 | +const ORG = "org_example"; |
| 18 | +const V2_ORIGIN = new URL("https://app.example.test"); |
| 19 | +const MOVED_AT = "2026-10-09T17:30:00.000Z"; |
| 20 | + |
| 21 | +const movedMetadata = (overrides: Record<string, unknown> = {}) => ({ |
| 22 | + [V2_MOVED_SLUG_KEY]: "example-team", |
| 23 | + [V2_MOVED_AT_KEY]: MOVED_AT, |
| 24 | + ...overrides, |
| 25 | +}); |
| 26 | + |
| 27 | +describe("v2 moved metadata", () => { |
| 28 | + it("reads the v2 slug and move time into the org's v2 page", () => { |
| 29 | + expect(parseV2Moved(movedMetadata(), V2_ORIGIN)).toEqual({ |
| 30 | + v2Url: "https://app.example.test/org/example-team", |
| 31 | + v2Slug: "example-team", |
| 32 | + movedAt: MOVED_AT, |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + it("ignores unrelated metadata keys", () => { |
| 37 | + expect(parseV2Moved(movedMetadata({ plan_note: "anything" }), V2_ORIGIN)?.v2Slug).toBe( |
| 38 | + "example-team", |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + it("normalizes a timestamp with an offset to UTC", () => { |
| 43 | + expect( |
| 44 | + parseV2Moved(movedMetadata({ [V2_MOVED_AT_KEY]: "2026-10-09T10:30:00-07:00" }), V2_ORIGIN) |
| 45 | + ?.movedAt, |
| 46 | + ).toBe(MOVED_AT); |
| 47 | + }); |
| 48 | + |
| 49 | + it("is not moved without both keys", () => { |
| 50 | + expect(parseV2Moved({}, V2_ORIGIN)).toBeNull(); |
| 51 | + expect(parseV2Moved({ [V2_MOVED_SLUG_KEY]: "example-team" }, V2_ORIGIN)).toBeNull(); |
| 52 | + expect(parseV2Moved({ [V2_MOVED_AT_KEY]: MOVED_AT }, V2_ORIGIN)).toBeNull(); |
| 53 | + }); |
| 54 | + |
| 55 | + it("is not moved when the slug is not a v2 slug", () => { |
| 56 | + for (const slug of [ |
| 57 | + "", |
| 58 | + "Example-Team", |
| 59 | + "example_team", |
| 60 | + "-example", |
| 61 | + "example-", |
| 62 | + "example--team", |
| 63 | + "example/team", |
| 64 | + "../admin", |
| 65 | + "example team", |
| 66 | + "a".repeat(46), |
| 67 | + 42, |
| 68 | + null, |
| 69 | + ]) { |
| 70 | + expect( |
| 71 | + parseV2Moved(movedMetadata({ [V2_MOVED_SLUG_KEY]: slug }), V2_ORIGIN), |
| 72 | + String(slug), |
| 73 | + ).toBeNull(); |
| 74 | + } |
| 75 | + expect( |
| 76 | + parseV2Moved(movedMetadata({ [V2_MOVED_SLUG_KEY]: "a".repeat(45) }), V2_ORIGIN), |
| 77 | + "45 characters is v2's longest slug", |
| 78 | + ).not.toBeNull(); |
| 79 | + }); |
| 80 | + |
| 81 | + it("is not moved when the move time is not an ISO timestamp", () => { |
| 82 | + for (const movedAt of [ |
| 83 | + "", |
| 84 | + "2026-10-09", |
| 85 | + "2026-10-09T17:30:00", |
| 86 | + "October 9, 2026", |
| 87 | + "2026-13-40T99:99:00Z", |
| 88 | + "1791000000000", |
| 89 | + 1791000000000, |
| 90 | + null, |
| 91 | + ]) { |
| 92 | + expect( |
| 93 | + parseV2Moved(movedMetadata({ [V2_MOVED_AT_KEY]: movedAt }), V2_ORIGIN), |
| 94 | + String(movedAt), |
| 95 | + ).toBeNull(); |
| 96 | + } |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +/** A metadata read answering `metadata`, or failing; records each read. */ |
| 101 | +const movedSourceOf = (metadata: Record<string, unknown> | "unavailable") => { |
| 102 | + const reads: string[] = []; |
| 103 | + const source: V2MovedSource = { |
| 104 | + organizationMetadata: (organizationId) => |
| 105 | + Effect.suspend(() => { |
| 106 | + reads.push(organizationId); |
| 107 | + return metadata === "unavailable" ? Effect.fail("unavailable") : Effect.succeed(metadata); |
| 108 | + }), |
| 109 | + }; |
| 110 | + return { source, reads }; |
| 111 | +}; |
| 112 | + |
| 113 | +/** An import source that admits the user, recording whether it was asked. */ |
| 114 | +const importSourceOf = (eligible: boolean) => { |
| 115 | + const reads: string[] = []; |
| 116 | + const source: V2ImportSource = { |
| 117 | + emailVerified: () => Effect.sync(() => (reads.push("user"), true)), |
| 118 | + activeMemberIds: () => Effect.sync(() => (reads.push("members"), eligible ? [USER] : [])), |
| 119 | + subscriptions: () => Effect.sync(() => (reads.push("billing"), [])), |
| 120 | + }; |
| 121 | + return { source, reads }; |
| 122 | +}; |
| 123 | + |
| 124 | +describe("v2 moved read", () => { |
| 125 | + it.effect("reads the selected org's metadata", () => |
| 126 | + Effect.gen(function* () { |
| 127 | + const { source, reads } = movedSourceOf(movedMetadata()); |
| 128 | + expect((yield* v2Moved(source, ORG, V2_ORIGIN))?.v2Slug).toBe("example-team"); |
| 129 | + expect(reads).toEqual([ORG]); |
| 130 | + }), |
| 131 | + ); |
| 132 | + |
| 133 | + it.effect("is not moved when the read fails", () => |
| 134 | + Effect.gen(function* () { |
| 135 | + const { source } = movedSourceOf("unavailable"); |
| 136 | + expect(yield* v2Moved(source, ORG, V2_ORIGIN)).toBeNull(); |
| 137 | + }), |
| 138 | + ); |
| 139 | +}); |
| 140 | + |
| 141 | +describe("v2 moved live read", () => { |
| 142 | + /** WorkOS answering `getOrganization` with `answer`; every other call dies. */ |
| 143 | + const workosAnswering = (answer: (organizationId: string) => Effect.Effect<unknown, unknown>) => |
| 144 | + Layer.succeed( |
| 145 | + WorkOSClient, |
| 146 | + new Proxy({} as WorkOSClientService, { |
| 147 | + get: (_target, prop) => |
| 148 | + prop === "getOrganization" |
| 149 | + ? answer |
| 150 | + : () => Effect.die(`unexpected WorkOSClient.${String(prop)} call`), |
| 151 | + }), |
| 152 | + ); |
| 153 | + |
| 154 | + const organization = (metadata: Record<string, string>) => ({ |
| 155 | + object: "organization", |
| 156 | + id: ORG, |
| 157 | + name: "Example Org", |
| 158 | + allowProfilesOutsideOrganization: false, |
| 159 | + domains: [], |
| 160 | + createdAt: "2026-01-01T00:00:00.000Z", |
| 161 | + updatedAt: "2026-01-01T00:00:00.000Z", |
| 162 | + externalId: null, |
| 163 | + metadata, |
| 164 | + }); |
| 165 | + |
| 166 | + const movedLive = (layer: Layer.Layer<WorkOSClient>) => |
| 167 | + Effect.gen(function* () { |
| 168 | + return yield* v2Moved(yield* liveV2MovedSource, ORG, V2_ORIGIN); |
| 169 | + }).pipe(Effect.provide(layer)); |
| 170 | + |
| 171 | + it.effect("reads the move from the WorkOS organization's metadata", () => |
| 172 | + Effect.gen(function* () { |
| 173 | + const asked: string[] = []; |
| 174 | + const moved = yield* movedLive( |
| 175 | + workosAnswering((organizationId) => |
| 176 | + Effect.sync(() => (asked.push(organizationId), organization(movedMetadata()))), |
| 177 | + ), |
| 178 | + ); |
| 179 | + expect(moved?.v2Url).toBe("https://app.example.test/org/example-team"); |
| 180 | + expect(asked).toEqual([ORG]); |
| 181 | + }), |
| 182 | + ); |
| 183 | + |
| 184 | + it.effect("is not moved for an organization without the keys", () => |
| 185 | + Effect.gen(function* () { |
| 186 | + expect(yield* movedLive(workosAnswering(() => Effect.succeed(organization({}))))).toBeNull(); |
| 187 | + }), |
| 188 | + ); |
| 189 | + |
| 190 | + it.effect("is not moved when WorkOS fails", () => |
| 191 | + Effect.gen(function* () { |
| 192 | + const failure = new WorkOSError({ status: 503 }); |
| 193 | + expect(yield* movedLive(workosAnswering(() => Effect.fail(failure)))).toBeNull(); |
| 194 | + }), |
| 195 | + ); |
| 196 | +}); |
| 197 | + |
| 198 | +describe("v2 import answer", () => { |
| 199 | + const configured = { |
| 200 | + V2: { fetch: () => Promise.resolve(new Response(null)) }, |
| 201 | + V2_SIGN_UP_URL: "https://app.example.test/login?mode=signup", |
| 202 | + V2_OAUTH_STATE_PREFIX: "x2.", |
| 203 | + V2_ANALYTICS_PROXY_PATH: "/api/0123456789abcdef", |
| 204 | + V2_ERROR_TUNNEL_PATH: "/api/fedcba9876543210/submit", |
| 205 | + }; |
| 206 | + |
| 207 | + const answer = ( |
| 208 | + env: typeof configured | Record<string, never>, |
| 209 | + moved: ReturnType<typeof movedSourceOf>, |
| 210 | + importing: ReturnType<typeof importSourceOf>, |
| 211 | + ) => |
| 212 | + v2ImportAnswer({ |
| 213 | + env, |
| 214 | + importSource: Effect.succeed(importing.source), |
| 215 | + movedSource: Effect.succeed(moved.source), |
| 216 | + userId: USER, |
| 217 | + organizationId: ORG, |
| 218 | + }); |
| 219 | + |
| 220 | + it.effect("answers where a moved org lives, without offering the import again", () => |
| 221 | + Effect.gen(function* () { |
| 222 | + const moved = movedSourceOf(movedMetadata()); |
| 223 | + const importing = importSourceOf(true); |
| 224 | + expect(yield* answer(configured, moved, importing)).toEqual({ |
| 225 | + url: null, |
| 226 | + moved: { |
| 227 | + v2Url: "https://app.example.test/org/example-team", |
| 228 | + v2Slug: "example-team", |
| 229 | + movedAt: MOVED_AT, |
| 230 | + }, |
| 231 | + }); |
| 232 | + expect(importing.reads, "a moved org's eligibility is never read").toEqual([]); |
| 233 | + }), |
| 234 | + ); |
| 235 | + |
| 236 | + it.effect("offers the import to an eligible org that has not moved", () => |
| 237 | + Effect.gen(function* () { |
| 238 | + expect(yield* answer(configured, movedSourceOf({}), importSourceOf(true))).toEqual({ |
| 239 | + url: "https://app.example.test/v1-import", |
| 240 | + moved: null, |
| 241 | + }); |
| 242 | + expect(yield* answer(configured, movedSourceOf({}), importSourceOf(false))).toEqual({ |
| 243 | + url: null, |
| 244 | + moved: null, |
| 245 | + }); |
| 246 | + }), |
| 247 | + ); |
| 248 | + |
| 249 | + it.effect("treats malformed or unreadable metadata as not moved", () => |
| 250 | + Effect.gen(function* () { |
| 251 | + for (const moved of [ |
| 252 | + movedSourceOf(movedMetadata({ [V2_MOVED_SLUG_KEY]: "Not A Slug" })), |
| 253 | + movedSourceOf(movedMetadata({ [V2_MOVED_AT_KEY]: "yesterday" })), |
| 254 | + movedSourceOf("unavailable"), |
| 255 | + ]) { |
| 256 | + expect(yield* answer(configured, moved, importSourceOf(true))).toEqual({ |
| 257 | + url: "https://app.example.test/v1-import", |
| 258 | + moved: null, |
| 259 | + }); |
| 260 | + } |
| 261 | + }), |
| 262 | + ); |
| 263 | + |
| 264 | + it.effect("answers nothing without a v2 host, without reading anything", () => |
| 265 | + Effect.gen(function* () { |
| 266 | + const moved = movedSourceOf(movedMetadata()); |
| 267 | + const importing = importSourceOf(true); |
| 268 | + expect(yield* answer({}, moved, importing)).toEqual({ url: null, moved: null }); |
| 269 | + expect(moved.reads).toEqual([]); |
| 270 | + expect(importing.reads).toEqual([]); |
| 271 | + }), |
| 272 | + ); |
| 273 | +}); |
0 commit comments