Files
clean-architecture-frontend…/tests/unit/auth-session-adapter.test.js
T

45 lines
1.6 KiB
JavaScript

import { describe, expect, it, vi } from "vitest";
import {
createAnonymousSessionAdapter,
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",
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",
attachCredential: async (request) => request,
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");
});
});