1import { AuthSessionId } from "@t3tools/contracts";
2import { describe, expect, it } from "@effect/vitest";
3import * as DateTime from "effect/DateTime";
4
5import { applyAuthAccessStreamEvent, EMPTY_AUTH_ACCESS_SNAPSHOT } from "./auth.ts";
6
7describe("applyAuthAccessStreamEvent", () => {
8 it("accumulates rapid pairing-link and client updates into one snapshot", () => {
9 const pairingLink = {
10 id: "pairing-link",
11 scopes: ["orchestration:read"],
12 subject: "subject",
13 label: "Phone",
14 createdAt: DateTime.makeUnsafe("2036-04-07T00:00:00.000Z"),
15 expiresAt: DateTime.makeUnsafe("2036-04-07T00:05:00.000Z"),
16 } as const;
17 const clientSession = {
18 sessionId: AuthSessionId.make("session-client"),
19 subject: "subject",
20 scopes: ["orchestration:read"],
21 method: "browser-session-cookie",
22 client: {
23 label: "Phone",
24 deviceType: "mobile",
25 },
26 issuedAt: DateTime.makeUnsafe("2036-04-07T00:00:00.000Z"),
27 expiresAt: DateTime.makeUnsafe("2036-05-07T00:00:00.000Z"),
28 lastConnectedAt: null,
29 connected: true,
30 current: false,
31 } as const;
32
33 const withPairingLink = applyAuthAccessStreamEvent(EMPTY_AUTH_ACCESS_SNAPSHOT, {
34 version: 1,
35 revision: 1,
36 type: "pairingLinkUpserted",
37 payload: pairingLink,
38 });
39 const withClient = applyAuthAccessStreamEvent(withPairingLink, {
40 version: 1,
41 revision: 2,
42 type: "clientUpserted",
43 payload: clientSession,
44 });
45
46 expect(withClient).toEqual({
47 pairingLinks: [pairingLink],
48 clientSessions: [clientSession],
49 });
50 });
51
52 it("applies removals without disturbing unrelated access state", () => {
53 const snapshot = applyAuthAccessStreamEvent(
54 {
55 pairingLinks: [
56 {
57 id: "pairing-link",
58 scopes: ["orchestration:read"],
59 subject: "subject",
60 label: "Phone",
61 createdAt: DateTime.makeUnsafe("2036-04-07T00:00:00.000Z"),
62 expiresAt: DateTime.makeUnsafe("2036-04-07T00:05:00.000Z"),
63 },
64 ],
65 clientSessions: [],
66 },
67 {
68 version: 1,
69 revision: 2,
70 type: "pairingLinkRemoved",
71 payload: { id: "pairing-link" },
72 },
73 );
74
75 expect(snapshot).toEqual(EMPTY_AUTH_ACCESS_SNAPSHOT);
76 });
77});