Open History to see the last commit
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 credential: "credential",
12 scopes: ["orchestration:read"],
13 subject: "subject",
14 label: "Phone",
15 createdAt: DateTime.makeUnsafe("2036-04-07T00:00:00.000Z"),
16 expiresAt: DateTime.makeUnsafe("2036-04-07T00:05:00.000Z"),
17 } as const;
18 const clientSession = {
19 sessionId: AuthSessionId.make("session-client"),
20 subject: "subject",
21 scopes: ["orchestration:read"],
22 method: "browser-session-cookie",
23 client: {
24 label: "Phone",
25 deviceType: "mobile",
26 },
27 issuedAt: DateTime.makeUnsafe("2036-04-07T00:00:00.000Z"),
28 expiresAt: DateTime.makeUnsafe("2036-05-07T00:00:00.000Z"),
29 lastConnectedAt: null,
30 connected: true,
31 current: false,
32 } as const;
33
34 const withPairingLink = applyAuthAccessStreamEvent(EMPTY_AUTH_ACCESS_SNAPSHOT, {
35 version: 1,
36 revision: 1,
37 type: "pairingLinkUpserted",
38 payload: pairingLink,
39 });
40 const withClient = applyAuthAccessStreamEvent(withPairingLink, {
41 version: 1,
42 revision: 2,
43 type: "clientUpserted",
44 payload: clientSession,
45 });
46
47 expect(withClient).toEqual({
48 pairingLinks: [pairingLink],
49 clientSessions: [clientSession],
50 });
51 });
52
53 it("applies removals without disturbing unrelated access state", () => {
54 const snapshot = applyAuthAccessStreamEvent(
55 {
56 pairingLinks: [
57 {
58 id: "pairing-link",
59 credential: "credential",
60 scopes: ["orchestration:read"],
61 subject: "subject",
62 label: "Phone",
63 createdAt: DateTime.makeUnsafe("2036-04-07T00:00:00.000Z"),
64 expiresAt: DateTime.makeUnsafe("2036-04-07T00:05:00.000Z"),
65 },
66 ],
67 clientSessions: [],
68 },
69 {
70 version: 1,
71 revision: 2,
72 type: "pairingLinkRemoved",
73 payload: { id: "pairing-link" },
74 },
75 );
76
77 expect(snapshot).toEqual(EMPTY_AUTH_ACCESS_SNAPSHOT);
78 });
79});