The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { |
| 4 | type CloudAccountSource, |
| 5 | createCloudAccountService, |
| 6 | planCloudAccountStep, |
| 7 | } from "./cloudAccountService.ts"; |
| 8 | |
| 9 | type Emit = (user: { readonly id: string } | null | undefined, clientId?: string) => void; |
| 10 | |
| 11 | function makeSource() { |
| 12 | let listener: Parameters<CloudAccountSource["addListener"]>[0] | null = null; |
| 13 | const source: CloudAccountSource = { |
| 14 | addListener: (next) => { |
| 15 | listener = next; |
| 16 | return () => { |
| 17 | listener = null; |
| 18 | }; |
| 19 | }, |
| 20 | }; |
| 21 | const emit: Emit = (user, clientId = "client_1") => |
| 22 | listener?.({ user, client: { id: clientId } }); |
| 23 | return { source, emit, isSubscribed: () => listener !== null }; |
| 24 | } |
| 25 | |
| 26 | function makeService() { |
| 27 | const log: string[] = []; |
| 28 | const activated: Array<PromiseWithResolvers<void>> = []; |
| 29 | const cleanedUp: Array<PromiseWithResolvers<void>> = []; |
| 30 | const gates = new Map< |
| 31 | string, |
| 32 | { readonly started: PromiseWithResolvers<void>; readonly release: PromiseWithResolvers<void> } |
| 33 | >(); |
| 34 | const service = createCloudAccountService<CloudAccountSource>({ |
| 35 | deactivate: () => log.push("deactivate"), |
| 36 | detach: () => log.push("detach"), |
| 37 | onSignOutTransition: () => log.push("signOutTransition"), |
| 38 | cleanUp: async ({ accountId, wasActive }) => { |
| 39 | log.push(`cleanUp:${accountId}:${wasActive}`); |
| 40 | cleanedUp.shift()?.resolve(); |
| 41 | }, |
| 42 | prepare: async ({ accountId }) => { |
| 43 | log.push(`prepare:${accountId}`); |
| 44 | const gate = gates.get(accountId); |
| 45 | gate?.started.resolve(); |
| 46 | await gate?.release.promise; |
| 47 | }, |
| 48 | activate: ({ accountId, isAccountTransition }) => { |
| 49 | log.push(`activate:${accountId}:${isAccountTransition}`); |
| 50 | activated.shift()?.resolve(); |
| 51 | }, |
| 52 | }); |
| 53 | const nextActivation = () => { |
| 54 | const deferred = Promise.withResolvers<void>(); |
| 55 | activated.push(deferred); |
| 56 | return deferred.promise; |
| 57 | }; |
| 58 | const nextCleanUp = () => { |
| 59 | const deferred = Promise.withResolvers<void>(); |
| 60 | cleanedUp.push(deferred); |
| 61 | return deferred.promise; |
| 62 | }; |
| 63 | const gate = (accountId: string) => { |
| 64 | const deferred = { |
| 65 | started: Promise.withResolvers<void>(), |
| 66 | release: Promise.withResolvers<void>(), |
| 67 | }; |
| 68 | gates.set(accountId, deferred); |
| 69 | return deferred; |
| 70 | }; |
| 71 | return { service, log, nextActivation, nextCleanUp, gate }; |
| 72 | } |
| 73 | |
| 74 | describe("planCloudAccountStep", () => { |
| 75 | it("treats the first observation as a non-transition", () => { |
| 76 | expect(planCloudAccountStep(undefined, "a")).toEqual({ |
| 77 | _tag: "SignIn", |
| 78 | accountId: "a", |
| 79 | switchFromAccountId: null, |
| 80 | isAccountTransition: false, |
| 81 | }); |
| 82 | expect(planCloudAccountStep(undefined, null)).toEqual({ |
| 83 | _tag: "SignOut", |
| 84 | previousAccountId: null, |
| 85 | isAccountTransition: false, |
| 86 | }); |
| 87 | }); |
| 88 | |
| 89 | it("ignores repeats and switches from the previous account", () => { |
| 90 | expect(planCloudAccountStep("a", "a")).toEqual({ _tag: "None" }); |
| 91 | expect(planCloudAccountStep(null, null)).toEqual({ _tag: "None" }); |
| 92 | expect(planCloudAccountStep("a", "b")).toEqual({ |
| 93 | _tag: "SignIn", |
| 94 | accountId: "b", |
| 95 | switchFromAccountId: "a", |
| 96 | isAccountTransition: true, |
| 97 | }); |
| 98 | }); |
| 99 | }); |
| 100 | |
| 101 | describe("createCloudAccountService", () => { |
| 102 | it("activates once per account however often Clerk emits", async () => { |
| 103 | const { service, log, nextActivation } = makeService(); |
| 104 | const { source, emit } = makeSource(); |
| 105 | service.attach(source); |
| 106 | |
| 107 | emit(undefined); |
| 108 | expect(service.getState()).toEqual({ _tag: "Unobserved" }); |
| 109 | const activation = nextActivation(); |
| 110 | emit({ id: "a" }); |
| 111 | emit({ id: "a" }); |
| 112 | expect(service.getState()).toEqual({ _tag: "Activating", accountId: "a" }); |
| 113 | await activation; |
| 114 | |
| 115 | expect(service.getState()).toEqual({ _tag: "Active", accountId: "a" }); |
| 116 | expect(log).toEqual(["prepare:a", "activate:a:false"]); |
| 117 | }); |
| 118 | |
| 119 | it("drops credentials synchronously and cleans up before activating the next account", async () => { |
| 120 | const { service, log, nextActivation } = makeService(); |
| 121 | const { source, emit } = makeSource(); |
| 122 | service.attach(source); |
| 123 | const first = nextActivation(); |
| 124 | emit({ id: "a" }); |
| 125 | await first; |
| 126 | log.length = 0; |
| 127 | |
| 128 | const second = nextActivation(); |
| 129 | emit({ id: "b" }); |
| 130 | expect(log).toEqual(["deactivate"]); |
| 131 | await second; |
| 132 | |
| 133 | expect(log).toEqual(["deactivate", "cleanUp:a:true", "prepare:b", "activate:b:true"]); |
| 134 | }); |
| 135 | |
| 136 | it("cleans up on sign-out, including a signed-out first observation", async () => { |
| 137 | const { service, log, nextActivation, nextCleanUp } = makeService(); |
| 138 | const { source, emit } = makeSource(); |
| 139 | service.attach(source); |
| 140 | |
| 141 | const initialCleanUp = nextCleanUp(); |
| 142 | emit(null); |
| 143 | await initialCleanUp; |
| 144 | expect(log).toEqual(["deactivate", "cleanUp:null:false"]); |
| 145 | |
| 146 | const activation = nextActivation(); |
| 147 | emit({ id: "a" }); |
| 148 | await activation; |
| 149 | log.length = 0; |
| 150 | |
| 151 | const cleanUp = nextCleanUp(); |
| 152 | emit(null); |
| 153 | await cleanUp; |
| 154 | expect(service.getState()).toEqual({ _tag: "SignedOut" }); |
| 155 | expect(log).toEqual(["signOutTransition", "deactivate", "cleanUp:a:true"]); |
| 156 | }); |
| 157 | |
| 158 | it("waits for Clerk's API to confirm a signed-out launch before cleaning up", async () => { |
| 159 | const { service, log, nextActivation, nextCleanUp } = makeService(); |
| 160 | const { source, emit } = makeSource(); |
| 161 | service.attach(source); |
| 162 | |
| 163 | // Offline: an id-less client, or the placeholder built from the session cookie. |
| 164 | emit(null, ""); |
| 165 | emit(null, "client_init"); |
| 166 | expect(service.getState()).toEqual({ _tag: "Unobserved" }); |
| 167 | expect(log).toEqual([]); |
| 168 | |
| 169 | const cleanUp = nextCleanUp(); |
| 170 | emit(null); |
| 171 | await cleanUp; |
| 172 | expect(log).toEqual(["deactivate", "cleanUp:null:false"]); |
| 173 | |
| 174 | const activation = nextActivation(); |
| 175 | emit({ id: "a" }); |
| 176 | await activation; |
| 177 | log.length = 0; |
| 178 | const signOut = nextCleanUp(); |
| 179 | emit(null, "client_init"); |
| 180 | await signOut; |
| 181 | expect(log).toEqual(["signOutTransition", "deactivate", "cleanUp:a:true"]); |
| 182 | }); |
| 183 | |
| 184 | it("never activates an account that signed out while it was activating", async () => { |
| 185 | const { service, log, nextCleanUp, gate } = makeService(); |
| 186 | const { source, emit } = makeSource(); |
| 187 | service.attach(source); |
| 188 | const prepared = gate("a"); |
| 189 | emit({ id: "a" }); |
| 190 | await prepared.started.promise; |
| 191 | const cleanUp = nextCleanUp(); |
| 192 | emit(null); |
| 193 | prepared.release.resolve(); |
| 194 | await cleanUp; |
| 195 | |
| 196 | expect(service.getState()).toEqual({ _tag: "SignedOut" }); |
| 197 | expect(log).toEqual(["prepare:a", "signOutTransition", "deactivate", "cleanUp:a:false"]); |
| 198 | }); |
| 199 | |
| 200 | it("retries a rejected activation on the next emission for the same account", async () => { |
| 201 | const log: string[] = []; |
| 202 | const failed = Promise.withResolvers<void>(); |
| 203 | const activated = Promise.withResolvers<void>(); |
| 204 | let attempts = 0; |
| 205 | const service = createCloudAccountService<CloudAccountSource>({ |
| 206 | deactivate: () => log.push("deactivate"), |
| 207 | detach: () => log.push("detach"), |
| 208 | cleanUp: async ({ accountId, wasActive }) => { |
| 209 | log.push(`cleanUp:${accountId}:${wasActive}`); |
| 210 | }, |
| 211 | prepare: async ({ accountId }) => { |
| 212 | attempts += 1; |
| 213 | log.push(`prepare:${accountId}`); |
| 214 | if (attempts === 1) { |
| 215 | // Queued before the rejection, so the service's catch runs before |
| 216 | // the test's `await failed.promise` continuation. |
| 217 | queueMicrotask(failed.resolve); |
| 218 | throw new Error("restore failed"); |
| 219 | } |
| 220 | }, |
| 221 | activate: ({ accountId, isAccountTransition }) => { |
| 222 | log.push(`activate:${accountId}:${isAccountTransition}`); |
| 223 | activated.resolve(); |
| 224 | }, |
| 225 | }); |
| 226 | const { source, emit } = makeSource(); |
| 227 | service.attach(source); |
| 228 | emit(null); |
| 229 | emit({ id: "a" }); |
| 230 | await failed.promise; |
| 231 | expect(service.getState()).toMatchObject({ _tag: "Failed", accountId: "a" }); |
| 232 | |
| 233 | emit({ id: "a" }); |
| 234 | await activated.promise; |
| 235 | |
| 236 | expect(service.getState()).toEqual({ _tag: "Active", accountId: "a" }); |
| 237 | expect(log).toEqual([ |
| 238 | "deactivate", |
| 239 | "cleanUp:null:false", |
| 240 | "prepare:a", |
| 241 | "prepare:a", |
| 242 | "activate:a:true", |
| 243 | ]); |
| 244 | }); |
| 245 | |
| 246 | it("detaches from a replaced Clerk instance", () => { |
| 247 | const { service, log } = makeService(); |
| 248 | const first = makeSource(); |
| 249 | const second = makeSource(); |
| 250 | service.attach(first.source); |
| 251 | service.attach(second.source); |
| 252 | |
| 253 | expect(first.isSubscribed()).toBe(false); |
| 254 | expect(second.isSubscribed()).toBe(true); |
| 255 | expect(log).toEqual(["detach"]); |
| 256 | }); |
| 257 | |
| 258 | it("releases credentials and stops observing when detached", async () => { |
| 259 | const { service, log, nextActivation } = makeService(); |
| 260 | const { source, emit, isSubscribed } = makeSource(); |
| 261 | const detach = service.attach(source); |
| 262 | const activation = nextActivation(); |
| 263 | emit({ id: "a" }); |
| 264 | await activation; |
| 265 | |
| 266 | detach(); |
| 267 | expect(isSubscribed()).toBe(false); |
| 268 | expect(service.getState()).toEqual({ _tag: "Unobserved" }); |
| 269 | expect(log).toEqual(["prepare:a", "activate:a:false", "detach"]); |
| 270 | }); |
| 271 | }); |