Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import { ProviderInstanceId, ProviderSetupError, type ProviderAuthState } from "@t3tools/contracts";
4import * as Deferred from "effect/Deferred";
5import * as Effect from "effect/Effect";
6import * as Exit from "effect/Exit";
7import * as Fiber from "effect/Fiber";
8import * as Option from "effect/Option";
9import * as Scope from "effect/Scope";
10import * as Stream from "effect/Stream";
11import * as TestClock from "effect/testing/TestClock";
12import * as AcpErrors from "effect-acp/errors";
13import type * as AcpSchema from "effect-acp/compat";
14
15import {
16 makeAntigravityAuth,
17 type AntigravityAuth,
18 type AntigravityAuthRuntime,
19} from "./AntigravityAuth.ts";
20import type * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime";
21
22const instanceId = ProviderInstanceId.make("antigravity-auth-test");
23const owner = "t3-auth-session-owner";
24const otherOwner = "t3-auth-session-other";
25const authorizationUrl =
26 "https://accounts.google.com/o/oauth2/v2/auth?response_type=code&redirect_uri=http%3A%2F%2F127.0.0.1%3A51234%2F&state=test-state";
27const callbackUrl = "http://127.0.0.1:51234/?state=test-state&code=test-code";
28
29const initialized = {
30 protocolVersion: 1,
31 authMethods: [{ id: "oauth-personal", name: "Log in with Google" }],
32 agentCapabilities: { auth: { logout: {} } },
33} satisfies AcpSchema.InitializeResponse;
34const started: AcpSessionRuntime.AcpSessionRuntimeStartResult = {
35 sessionId: "native-session",
36 initializeResult: initialized,
37 sessionSetupResult: {
38 sessionId: "native-session",
39 models: {
40 currentModelId: "gemini-test",
41 availableModels: [{ modelId: "gemini-test", name: "Gemini test" }],
42 },
43 },
44 modelConfigId: "model",
45};
46
47const phase = (auth: AntigravityAuth, value: ProviderAuthState["phase"], sessionId = owner) =>
48 auth.controller.subscribe(sessionId).pipe(
49 Stream.filter((state) => state.phase === value),
50 Stream.runHead,
51 Effect.map(Option.getOrThrow),
52 );
53
54const makeHarness = Effect.fn("makeAuthTestHarness")(function* (
55 options: {
56 readonly interactive?: boolean;
57 readonly authorizationUrls?: ReadonlyArray<string>;
58 readonly supportsLogout?: boolean;
59 readonly beforeInitialize?: Effect.Effect<void>;
60 readonly forwardCallback?: Effect.Effect<void, ProviderSetupError>;
61 } = {},
62) {
63 const authenticated = yield* Deferred.make<void, AcpErrors.AcpError>();
64 const discovered = yield* Deferred.make<void, AcpErrors.AcpError>();
65 const closed = yield* Deferred.make<void>();
66 const events: string[] = [];
67 let receiveAuthorizationUrl:
68 | ((url: string) => Effect.Effect<void, AcpErrors.AcpError>)
69 | undefined;
70 let forwarded = 0;
71 let catalog = ["previous-account-model"];
72 const auth = yield* makeAntigravityAuth({
73 instanceId,
74 makeRuntime: (input) =>
75 Effect.gen(function* () {
76 receiveAuthorizationUrl = input.onAuthorizationUrl;
77 events.push("process-open");
78 yield* Effect.addFinalizer(() =>
79 Effect.gen(function* () {
80 events.push("process-close");
81 yield* Deferred.succeed(closed, undefined);
82 }),
83 );
84 return {
85 initialize: () =>
86 Effect.gen(function* () {
87 events.push("initialize");
88 yield* options.beforeInitialize ?? Effect.void;
89 return options.supportsLogout === false
90 ? { ...initialized, agentCapabilities: {} }
91 : initialized;
92 }),
93 start: () =>
94 Effect.gen(function* () {
95 events.push("authenticate");
96 if (options.interactive !== false && input.onAuthorizationUrl) {
97 for (const url of options.authorizationUrls ?? [authorizationUrl]) {
98 yield* input.onAuthorizationUrl(url);
99 }
100 }
101 yield* Deferred.await(authenticated);
102 events.push("session-new");
103 yield* Deferred.await(discovered);
104 return started;
105 }),
106 request: (method) =>
107 Effect.sync(() => {
108 events.push(method);
109 return {};
110 }),
111 } satisfies AntigravityAuthRuntime;
112 }),
113 onAuthenticated: () =>
114 Effect.sync(() => {
115 catalog = ["gemini-test"];
116 events.push("catalog-published");
117 }),
118 onSignedOut: Effect.sync(() => {
119 catalog = [];
120 events.push("catalog-cleared");
121 }),
122 forwardCallback: () =>
123 options.forwardCallback ??
124 Effect.sync(() => {
125 forwarded += 1;
126 }),
127 });
128 return {
129 auth,
130 authenticated,
131 discovered,
132 closed,
133 events,
134 catalog: () => catalog,
135 forwarded: () => forwarded,
136 receiveAuthorizationUrl: (url: string) =>
137 Effect.suspend(() =>
138 receiveAuthorizationUrl
139 ? receiveAuthorizationUrl(url)
140 : Effect.die("Authorization URL receiver is not ready."),
141 ),
142 };
143});
144
145it.layer(NodeServices.layer)("AntigravityAuth", (it) => {
146 it.effect("accepts the same authorization URL from stderr and stdout", () =>
147 Effect.gen(function* () {
148 const harness = yield* makeHarness({
149 authorizationUrls: [authorizationUrl, authorizationUrl],
150 });
151 yield* harness.auth.controller.start(owner);
152 const waiting = yield* phase(harness.auth, "waiting");
153 assert.equal(waiting.authorizationUrl, authorizationUrl);
154
155 yield* Deferred.succeed(harness.authenticated, undefined);
156 yield* Deferred.succeed(harness.discovered, undefined);
157 yield* phase(harness.auth, "succeeded");
158 }),
159 );
160
161 it.effect("accepts a delayed duplicate after callback completion starts", () =>
162 Effect.gen(function* () {
163 const harness = yield* makeHarness();
164 const state = yield* harness.auth.controller.start(owner);
165 yield* phase(harness.auth, "waiting");
166 yield* harness.auth.controller.complete(owner, {
167 flowId: state.flowId!,
168 callbackUrl,
169 });
170
171 yield* harness.receiveAuthorizationUrl(authorizationUrl);
172
173 yield* Deferred.succeed(harness.authenticated, undefined);
174 yield* Deferred.succeed(harness.discovered, undefined);
175 yield* phase(harness.auth, "succeeded");
176 }),
177 );
178
179 it.effect("rejects a different second authorization URL", () =>
180 Effect.gen(function* () {
181 const harness = yield* makeHarness({
182 authorizationUrls: [authorizationUrl, `${authorizationUrl}&scope=another-request`],
183 });
184 yield* harness.auth.controller.start(owner);
185
186 const failed = yield* phase(harness.auth, "failed");
187 assert.isNull(failed.authorizationUrl);
188 assert.deepEqual(harness.catalog(), ["previous-account-model"]);
189 }),
190 );
191
192 it.effect("keeps a remote flow private and waits for native auth and catalog discovery", () =>
193 Effect.gen(function* () {
194 const harness = yield* makeHarness();
195 const state = yield* harness.auth.controller.start(owner);
196 assert.isNotNull(state.flowId);
197 const waiting = yield* phase(harness.auth, "waiting");
198 assert.equal(waiting.authorizationUrl, authorizationUrl);
199
200 const other = yield* phase(harness.auth, "waiting", otherOwner);
201 assert.isNull(other.authorizationUrl);
202 assert.isNull(other.flowId);
203 const stolen = yield* harness.auth.controller
204 .complete(otherOwner, { flowId: state.flowId!, callbackUrl })
205 .pipe(Effect.exit);
206 assert.isTrue(Exit.isFailure(stolen));
207 assert.equal(harness.forwarded(), 0);
208
209 const verifying = yield* harness.auth.controller.complete(owner, {
210 flowId: state.flowId!,
211 callbackUrl,
212 });
213 assert.equal(verifying.phase, "verifying");
214 assert.equal(harness.forwarded(), 1);
215 assert.deepEqual(harness.catalog(), ["previous-account-model"]);
216
217 yield* Deferred.succeed(harness.authenticated, undefined);
218 yield* Deferred.succeed(harness.discovered, undefined);
219 const succeeded = yield* phase(harness.auth, "succeeded");
220 assert.deepEqual(harness.catalog(), ["gemini-test"]);
221 assert.isNull(succeeded.authorizationUrl);
222 assert.isNull(succeeded.expiresAt);
223 assert.equal(harness.events.at(-1), "process-close");
224 }),
225 );
226
227 it.effect(
228 "distinguishes a post-authentication session failure without exposing its payload",
229 () =>
230 Effect.gen(function* () {
231 const harness = yield* makeHarness();
232 yield* harness.auth.controller.start(owner);
233 yield* phase(harness.auth, "waiting");
234 yield* Deferred.succeed(harness.authenticated, undefined);
235 yield* Deferred.fail(
236 harness.discovered,
237 new AcpErrors.AcpRequestError({
238 code: -32603,
239 errorMessage: `Internal error ${callbackUrl}`,
240 method: "session/new",
241 }),
242 );
243 const failed = yield* phase(harness.auth, "failed");
244 assert.equal(
245 failed.message,
246 "Antigravity authenticated, but could not initialize a session or load models.",
247 );
248 assert.isNull(failed.authorizationUrl);
249 assert.deepEqual(harness.catalog(), ["previous-account-model"]);
250 yield* Deferred.await(harness.closed);
251 }),
252 );
253
254 it.effect("does not call callback HTTP success a successful Google sign-in", () =>
255 Effect.gen(function* () {
256 const harness = yield* makeHarness();
257 const state = yield* harness.auth.controller.start(owner);
258 yield* phase(harness.auth, "waiting");
259 yield* harness.auth.controller.complete(owner, { flowId: state.flowId!, callbackUrl });
260 yield* Deferred.fail(
261 harness.authenticated,
262 AcpErrors.AcpRequestError.internalError(`access_denied ${callbackUrl}`),
263 );
264 const failed = yield* phase(harness.auth, "failed");
265 assert.include(failed.message ?? "", "not approved");
266 assert.notInclude(failed.message ?? "", "test-code");
267 assert.isNull(failed.authorizationUrl);
268 assert.deepEqual(harness.catalog(), ["previous-account-model"]);
269 yield* Deferred.await(harness.closed);
270 }),
271 );
272
273 it.effect("accepts direct local or cached completion without a callback RPC", () =>
274 Effect.gen(function* () {
275 const harness = yield* makeHarness({ interactive: false });
276 yield* harness.auth.controller.start(owner);
277 yield* Deferred.succeed(harness.authenticated, undefined);
278 yield* Deferred.succeed(harness.discovered, undefined);
279 yield* phase(harness.auth, "succeeded");
280 assert.equal(harness.forwarded(), 0);
281 assert.deepEqual(harness.catalog(), ["gemini-test"]);
282 }),
283 );
284
285 it.effect("rejects mismatched callbacks without sending any HTTP request", () =>
286 Effect.gen(function* () {
287 const harness = yield* makeHarness();
288 const state = yield* harness.auth.controller.start(owner);
289 yield* phase(harness.auth, "waiting");
290 for (const invalidUrl of [
291 callbackUrl.replace("51234", "51235"),
292 callbackUrl.replace("test-state", "wrong-state"),
293 callbackUrl.replace("/?", "/other?"),
294 `${callbackUrl}&state=test-state`,
295 ]) {
296 const result = yield* harness.auth.controller
297 .complete(owner, { flowId: state.flowId!, callbackUrl: invalidUrl })
298 .pipe(Effect.exit);
299 assert.isTrue(Exit.isFailure(result));
300 }
301 assert.equal(harness.forwarded(), 0);
302 assert.equal((yield* phase(harness.auth, "waiting")).authorizationUrl, authorizationUrl);
303 yield* harness.auth.controller.cancel(owner, state.flowId!);
304 }),
305 );
306
307 it.effect("fails the flow when delivery fails after the requesting client disconnects", () =>
308 Effect.gen(function* () {
309 const deliveryGate = yield* Deferred.make<void>();
310 const harness = yield* makeHarness({
311 forwardCallback: Deferred.await(deliveryGate).pipe(
312 Effect.andThen(
313 Effect.fail(
314 new ProviderSetupError({
315 instanceId,
316 operation: "complete",
317 detail: "loopback refused",
318 }),
319 ),
320 ),
321 ),
322 });
323 const state = yield* harness.auth.controller.start(owner);
324 yield* phase(harness.auth, "waiting");
325 const request = yield* harness.auth.controller
326 .complete(owner, { flowId: state.flowId!, callbackUrl })
327 .pipe(Effect.forkScoped);
328 yield* phase(harness.auth, "verifying");
329 yield* Fiber.interrupt(request);
330 yield* Deferred.succeed(deliveryGate, undefined);
331 const failed = yield* phase(harness.auth, "failed");
332 assert.include(failed.message ?? "", "Could not deliver");
333 assert.isTrue(harness.events.includes("process-close"));
334 }),
335 );
336
337 it.effect("cancel closes the owned process without forwarding a denial", () =>
338 Effect.gen(function* () {
339 const harness = yield* makeHarness();
340 const state = yield* harness.auth.controller.start(owner);
341 yield* phase(harness.auth, "waiting");
342 const wrongOwner = yield* harness.auth.controller
343 .cancel(otherOwner, state.flowId!)
344 .pipe(Effect.exit);
345 assert.isTrue(Exit.isFailure(wrongOwner));
346 const cancelled = yield* harness.auth.controller.cancel(owner, state.flowId!);
347 assert.equal(cancelled.phase, "cancelled");
348 assert.isNull(cancelled.authorizationUrl);
349 assert.equal(harness.forwarded(), 0);
350 yield* Deferred.await(harness.closed);
351 assert.deepEqual(harness.catalog(), ["previous-account-model"]);
352 }),
353 );
354
355 it.effect("expires the flow at the official deadline and removes its URL", () =>
356 Effect.gen(function* () {
357 const harness = yield* makeHarness();
358 const state = yield* harness.auth.controller.start(owner);
359 yield* phase(harness.auth, "waiting");
360 yield* TestClock.adjust("300 seconds");
361 const failed = yield* phase(harness.auth, "failed");
362 assert.include(failed.message ?? "", "expired");
363 assert.isNull(failed.authorizationUrl);
364 yield* Deferred.await(harness.closed);
365 const late = yield* harness.auth.controller
366 .complete(owner, { flowId: state.flowId!, callbackUrl })
367 .pipe(Effect.exit);
368 assert.isTrue(Exit.isFailure(late));
369 assert.equal(harness.forwarded(), 0);
370 }),
371 );
372
373 it.effect("survives subscriber disconnect and does not replace a competing client's flow", () =>
374 Effect.gen(function* () {
375 const harness = yield* makeHarness();
376 const first = yield* harness.auth.controller.start(owner);
377 yield* phase(harness.auth, "waiting");
378 const second = yield* harness.auth.controller.start(owner);
379 assert.equal(first.flowId, second.flowId);
380 const competing = yield* harness.auth.controller.start(otherOwner).pipe(Effect.exit);
381 assert.isTrue(Exit.isFailure(competing));
382 const resumed = yield* phase(harness.auth, "waiting");
383 assert.equal(resumed.flowId, first.flowId);
384 assert.deepEqual(harness.events, ["process-open", "authenticate"]);
385 yield* harness.auth.controller.cancel(owner, first.flowId!);
386 }),
387 );
388
389 it.effect("sign-out closes admission and every process before fresh native logout", () =>
390 Effect.gen(function* () {
391 const harness = yield* makeHarness();
392 const processScope = yield* Scope.make();
393 yield* Effect.addFinalizer(() => Scope.close(processScope, Exit.void));
394 const stop = Effect.gen(function* () {
395 harness.events.push("chat-close");
396 yield* Scope.close(processScope, Exit.void);
397 });
398 yield* harness.auth.withProcess(stop, Effect.void).pipe(Scope.provide(processScope));
399 const stopSessions = Effect.gen(function* () {
400 harness.events.push("sessions-stop");
401 const denied = yield* harness.auth
402 .withProcess(
403 Effect.void,
404 Effect.sync(() => harness.events.push("late-process")),
405 )
406 .pipe(Effect.scoped, Effect.exit);
407 assert.isTrue(Exit.isFailure(denied));
408 });
409 const result = yield* harness.auth.controller.logout(stopSessions);
410 assert.equal(result.phase, "idle");
411 assert.deepEqual(harness.events, [
412 "sessions-stop",
413 "chat-close",
414 "process-open",
415 "initialize",
416 "logout",
417 "catalog-cleared",
418 "process-close",
419 ]);
420 assert.deepEqual(harness.catalog(), []);
421 yield* harness.auth.withProcess(Effect.void, Effect.void).pipe(Effect.scoped);
422 }),
423 );
424
425 it.effect("signs out after a slow packaged runtime starts", () =>
426 Effect.gen(function* () {
427 const entered = yield* Deferred.make<void>();
428 const initialized = yield* Deferred.make<void>();
429 const harness = yield* makeHarness({
430 beforeInitialize: Deferred.succeed(entered, undefined).pipe(
431 Effect.andThen(Deferred.await(initialized)),
432 ),
433 });
434 const logout = yield* harness.auth.controller.logout(Effect.void).pipe(Effect.forkScoped);
435 yield* Deferred.await(entered);
436 yield* TestClock.adjust("47 seconds");
437 yield* Deferred.succeed(initialized, undefined);
438 assert.equal((yield* Fiber.join(logout)).phase, "idle");
439 assert.include(harness.events, "logout");
440 assert.deepEqual(harness.catalog(), []);
441 yield* Deferred.await(harness.closed);
442 }),
443 );
444
445 it.effect("closes a stalled sign-out process without clearing its account catalog", () =>
446 Effect.gen(function* () {
447 const entered = yield* Deferred.make<void>();
448 const harness = yield* makeHarness({
449 beforeInitialize: Deferred.succeed(entered, undefined).pipe(Effect.andThen(Effect.never)),
450 });
451 const logout = yield* harness.auth.controller
452 .logout(Effect.void)
453 .pipe(Effect.exit, Effect.forkScoped);
454 yield* Deferred.await(entered);
455 yield* TestClock.adjust("90 seconds");
456 assert.isTrue(Exit.isFailure(yield* Fiber.join(logout)));
457 yield* Deferred.await(harness.closed);
458 assert.notInclude(harness.events, "logout");
459 assert.deepEqual(harness.catalog(), ["previous-account-model"]);
460 yield* harness.auth.withProcess(Effect.void, Effect.void).pipe(Effect.scoped);
461 }),
462 );
463
464 it.effect(
465 "sign-out interrupts startup without interrupting its caller after startup returns",
466 () =>
467 Effect.gen(function* () {
468 const harness = yield* makeHarness();
469 const entering = yield* Deferred.make<void>();
470 const continueStartup = yield* Deferred.make<void>();
471 const processScope = yield* Scope.make();
472 yield* Effect.addFinalizer(() => Scope.close(processScope, Exit.void));
473 const task = Effect.gen(function* () {
474 yield* Deferred.succeed(entering, undefined);
475 yield* Deferred.await(continueStartup);
476 harness.events.push("late-spawn");
477 });
478 const startup = yield* harness.auth
479 .withProcess(Scope.close(processScope, Exit.void), task)
480 .pipe(Scope.provide(processScope), Effect.forkScoped);
481 yield* Deferred.await(entering);
482 yield* harness.auth.controller.logout(Effect.void);
483 yield* Deferred.succeed(continueStartup, undefined);
484 assert.isTrue(Exit.isFailure(yield* Fiber.await(startup)));
485 assert.notInclude(harness.events, "late-spawn");
486 assert.include(harness.events, "logout");
487 }),
488 );
489
490 it.effect("failed session stopping still closes owned processes and skips native logout", () =>
491 Effect.gen(function* () {
492 const harness = yield* makeHarness();
493 const processScope = yield* Scope.make();
494 yield* Effect.addFinalizer(() => Scope.close(processScope, Exit.void));
495 const stop = Effect.gen(function* () {
496 harness.events.push("chat-close");
497 yield* Scope.close(processScope, Exit.void);
498 });
499 yield* harness.auth.withProcess(stop, Effect.void).pipe(Scope.provide(processScope));
500 const result = yield* harness.auth.controller
501 .logout(
502 Effect.fail(
503 new ProviderSetupError({
504 instanceId,
505 operation: "stopSessions",
506 detail: "Stop failed.",
507 }),
508 ),
509 )
510 .pipe(Effect.exit);
511 assert.isTrue(Exit.isFailure(result));
512 assert.deepEqual(harness.events, ["chat-close"]);
513 assert.deepEqual(harness.catalog(), ["previous-account-model"]);
514 }),
515 );
516
517 it.effect("finishes sign-out when the requesting client disconnects", () =>
518 Effect.gen(function* () {
519 const harness = yield* makeHarness();
520 const stopping = yield* Deferred.make<void>();
521 const continueStop = yield* Deferred.make<void>();
522 const request = yield* harness.auth.controller
523 .logout(
524 Effect.gen(function* () {
525 yield* Deferred.succeed(stopping, undefined);
526 yield* Deferred.await(continueStop);
527 }),
528 )
529 .pipe(Effect.forkScoped);
530 yield* Deferred.await(stopping);
531 yield* Fiber.interrupt(request);
532 yield* Deferred.succeed(continueStop, undefined);
533 const result = yield* harness.auth.controller.subscribe(owner).pipe(
534 Stream.filter((state) => state.message === "Signed out of Google."),
535 Stream.runHead,
536 Effect.map(Option.getOrThrow),
537 );
538 assert.equal(result.phase, "idle");
539 assert.deepEqual(harness.catalog(), []);
540 assert.include(harness.events, "logout");
541 }),
542 );
543
544 it.effect("does not call logout unless the official process advertises it", () =>
545 Effect.gen(function* () {
546 const harness = yield* makeHarness({ supportsLogout: false });
547 const result = yield* harness.auth.controller.logout(Effect.void).pipe(Effect.exit);
548 assert.isTrue(Exit.isFailure(result));
549 assert.deepEqual(harness.events, ["process-open", "initialize", "process-close"]);
550 assert.deepEqual(harness.catalog(), ["previous-account-model"]);
551 }),
552 );
553});