|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +import { |
| 4 | + encodeRealtimeClientRequest, |
| 5 | + decodeRealtimeClientBinaryMessage, |
| 6 | + RealtimeCommandType, |
| 7 | +} from "@deepnotes/realtime-wire"; |
| 8 | + |
| 9 | +import { |
| 10 | + canRealtimeHashAccess, |
| 11 | + executeRealtimeWsBatch, |
| 12 | + realtimeFullKey, |
| 13 | + type RealtimeHashPort, |
| 14 | +} from "./realtime-ws-batch.js"; |
| 15 | + |
| 16 | +function createMemoryHashPort( |
| 17 | + initial: Record<string, Record<string, unknown>> = {}, |
| 18 | +): { port: RealtimeHashPort; snapshot: () => Record<string, Record<string, unknown>> } { |
| 19 | + const store = new Map<string, Record<string, unknown>>(); |
| 20 | + for (const [k, row] of Object.entries(initial)) { |
| 21 | + store.set(k, { ...row }); |
| 22 | + } |
| 23 | + const port: RealtimeHashPort = { |
| 24 | + async hmget(key, fields) { |
| 25 | + const row = store.get(key) ?? {}; |
| 26 | + return fields.map((f) => { |
| 27 | + if (!Object.prototype.hasOwnProperty.call(row, f)) { |
| 28 | + return null; |
| 29 | + } |
| 30 | + return row[f] ?? null; |
| 31 | + }); |
| 32 | + }, |
| 33 | + async hset(key, entries) { |
| 34 | + let row = store.get(key); |
| 35 | + if (row == null) { |
| 36 | + row = {}; |
| 37 | + store.set(key, row); |
| 38 | + } |
| 39 | + Object.assign(row, entries); |
| 40 | + }, |
| 41 | + }; |
| 42 | + return { |
| 43 | + port, |
| 44 | + snapshot: () => { |
| 45 | + const o: Record<string, Record<string, unknown>> = {}; |
| 46 | + for (const [k, v] of store) { |
| 47 | + o[k] = { ...v }; |
| 48 | + } |
| 49 | + return o; |
| 50 | + }, |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +describe("executeRealtimeWsBatch", () => { |
| 55 | + it("denies group/page hash access (user-only until Postgres ACL)", () => { |
| 56 | + expect(canRealtimeHashAccess("u1", "group", "g1")).toBe(false); |
| 57 | + expect(canRealtimeHashAccess("u1", "page", "p1")).toBe(false); |
| 58 | + expect(canRealtimeHashAccess("u1", "user", "u1")).toBe(true); |
| 59 | + expect(canRealtimeHashAccess("u1", "user", "u2")).toBe(false); |
| 60 | + }); |
| 61 | + |
| 62 | + it("HGET returns batched hmget values for own user hash", async () => { |
| 63 | + const { port } = createMemoryHashPort({ |
| 64 | + "user:u1": { email: "a@b.c", plan: "pro" }, |
| 65 | + }); |
| 66 | + const req = encodeRealtimeClientRequest({ |
| 67 | + firstCommandId: 1, |
| 68 | + commands: [ |
| 69 | + { |
| 70 | + type: RealtimeCommandType.HGET, |
| 71 | + args: ["user", "u1", "email"], |
| 72 | + }, |
| 73 | + { |
| 74 | + type: RealtimeCommandType.HGET, |
| 75 | + args: ["user", "u1", "plan"], |
| 76 | + }, |
| 77 | + ], |
| 78 | + }); |
| 79 | + const decoded = decodeRealtimeClientBinaryMessage(req); |
| 80 | + if (decoded == null) { |
| 81 | + throw new Error("decode"); |
| 82 | + } |
| 83 | + const hooks = { subscribeField: () => {}, unsubscribeField: () => {} }; |
| 84 | + const out = await executeRealtimeWsBatch({ |
| 85 | + userId: "u1", |
| 86 | + decoded, |
| 87 | + redis: port, |
| 88 | + hooks, |
| 89 | + }); |
| 90 | + expect(out.responseBytes).not.toBeNull(); |
| 91 | + expect(out.subscribeNotifyBytes).toBeNull(); |
| 92 | + expect(out.hsetBroadcastItems).toEqual([]); |
| 93 | + }); |
| 94 | + |
| 95 | + it("SUBSCRIBE registers hook and sends initial DATA_NOTIFICATION", async () => { |
| 96 | + const { port } = createMemoryHashPort({ |
| 97 | + "user:u1": { email: "x@y.z" }, |
| 98 | + }); |
| 99 | + const subs: string[] = []; |
| 100 | + const req = encodeRealtimeClientRequest({ |
| 101 | + firstCommandId: 0, |
| 102 | + commands: [ |
| 103 | + { |
| 104 | + type: RealtimeCommandType.SUBSCRIBE, |
| 105 | + args: ["user", "u1", "email"], |
| 106 | + }, |
| 107 | + ], |
| 108 | + }); |
| 109 | + const decoded = decodeRealtimeClientBinaryMessage(req); |
| 110 | + if (decoded == null) { |
| 111 | + throw new Error("decode"); |
| 112 | + } |
| 113 | + const out = await executeRealtimeWsBatch({ |
| 114 | + userId: "u1", |
| 115 | + decoded, |
| 116 | + redis: port, |
| 117 | + hooks: { |
| 118 | + subscribeField: (fk) => { |
| 119 | + subs.push(fk); |
| 120 | + }, |
| 121 | + unsubscribeField: () => {}, |
| 122 | + }, |
| 123 | + }); |
| 124 | + expect(subs).toEqual([realtimeFullKey("user", "u1", "email")]); |
| 125 | + expect(out.subscribeNotifyBytes).not.toBeNull(); |
| 126 | + }); |
| 127 | + |
| 128 | + it("HSET notifies broadcast list items for subscribed fan-out handled by DO", async () => { |
| 129 | + const { port } = createMemoryHashPort(); |
| 130 | + const req = encodeRealtimeClientRequest({ |
| 131 | + firstCommandId: 10, |
| 132 | + commands: [ |
| 133 | + { |
| 134 | + type: RealtimeCommandType.HSET, |
| 135 | + args: ["user", "u1", "email", "new@mail"], |
| 136 | + }, |
| 137 | + ], |
| 138 | + }); |
| 139 | + const decoded = decodeRealtimeClientBinaryMessage(req); |
| 140 | + if (decoded == null) { |
| 141 | + throw new Error("decode"); |
| 142 | + } |
| 143 | + const out = await executeRealtimeWsBatch({ |
| 144 | + userId: "u1", |
| 145 | + decoded, |
| 146 | + redis: port, |
| 147 | + hooks: { subscribeField: () => {}, unsubscribeField: () => {} }, |
| 148 | + }); |
| 149 | + expect(out.hsetBroadcastItems).toHaveLength(1); |
| 150 | + expect(out.hsetBroadcastItems[0]?.fullKey).toBe( |
| 151 | + realtimeFullKey("user", "u1", "email"), |
| 152 | + ); |
| 153 | + }); |
| 154 | +}); |
0 commit comments