import { describe, expect, it, vi } from "vitest"; import { createAnonymousSessionAdapter, createDemoSessionAdapter, createExternalAuthSessionAdapter, } from "../../src/adapters/auth/external-session-adapter.js"; describe("external AuthSessionPort adapter", () => { it("attaches opaque credentials without exposing a token-shaped session", async () => { const adapter = createExternalAuthSessionAdapter({ readState: () => "authenticated", subscribe: () => () => {}, beginSignIn: async () => {}, signOut: async () => {}, attachCredential: async (request) => { const headers = new Headers(request.headers); headers.set("X-Session-Attached", "true"); return new Request(request, { headers }); }, recoverSession: async () => "restored", notifyUnauthenticated: vi.fn(), }); const request = await adapter.attach(new Request("https://api.test/resource")); expect(request.headers.get("X-Session-Attached")).toBe("true"); expect(adapter.getState()).toBe("authenticated"); expect(adapter).not.toHaveProperty("accessToken"); expect(adapter).not.toHaveProperty("refreshToken"); }); it("fails invalid recovery states closed", async () => { const adapter = createExternalAuthSessionAdapter({ readState: () => "authenticated", subscribe: () => () => {}, beginSignIn: async () => {}, signOut: async () => {}, attachCredential: async (request) => request, // @ts-expect-error Deliberately violates the external-owner contract. recoverSession: async () => "unexpected", notifyUnauthenticated: vi.fn(), }); await expect(adapter.recover()).rejects.toThrow("invalid recovery state"); }); it("provides a safe anonymous adapter", async () => { const adapter = createAnonymousSessionAdapter(); expect(adapter.getState()).toBe("unauthenticated"); await expect(adapter.recover()).resolves.toBe("no-session"); }); it("provides a reactive credential-free demo seam", async () => { const adapter = createDemoSessionAdapter(); let notifications = 0; const unsubscribe = adapter.subscribe(() => { notifications += 1; }); expect(adapter.getState()).toBe("unauthenticated"); await adapter.beginSignIn(); expect(adapter.getState()).toBe("authenticated"); await adapter.signOut(); expect(adapter.getState()).toBe("unauthenticated"); expect(notifications).toBe(2); unsubscribe(); }); });