|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { |
| 3 | + broadcastTypingThrottled, |
| 4 | + clearSessionTypingState, |
| 5 | + updateTypingConnections, |
| 6 | +} from "./session-typing-state.js"; |
| 7 | + |
| 8 | +beforeEach(() => { |
| 9 | + vi.useFakeTimers(); |
| 10 | + vi.setSystemTime(10_000); |
| 11 | + clearSessionTypingState(); |
| 12 | +}); |
| 13 | + |
| 14 | +afterEach(() => { |
| 15 | + clearSessionTypingState(); |
| 16 | + vi.useRealTimers(); |
| 17 | +}); |
| 18 | + |
| 19 | +describe("session typing connection state", () => { |
| 20 | + it("retains the newest live preview across an actor's connections", () => { |
| 21 | + const key = "shared-preview"; |
| 22 | + |
| 23 | + expect( |
| 24 | + updateTypingConnections({ |
| 25 | + key, |
| 26 | + connectionId: "preview-tab", |
| 27 | + typing: true, |
| 28 | + preview: "first draft", |
| 29 | + now: 10_000, |
| 30 | + }), |
| 31 | + ).toEqual({ typing: true, preview: "first draft" }); |
| 32 | + expect( |
| 33 | + updateTypingConnections({ |
| 34 | + key, |
| 35 | + connectionId: "presence-tab", |
| 36 | + typing: true, |
| 37 | + now: 10_100, |
| 38 | + }), |
| 39 | + ).toEqual({ typing: true, preview: "first draft" }); |
| 40 | + expect( |
| 41 | + updateTypingConnections({ |
| 42 | + key, |
| 43 | + connectionId: "presence-tab", |
| 44 | + typing: true, |
| 45 | + preview: "newer draft", |
| 46 | + now: 10_200, |
| 47 | + }), |
| 48 | + ).toEqual({ typing: true, preview: "newer draft" }); |
| 49 | + }); |
| 50 | + |
| 51 | + it("removes a stopped connection's preview while preserving another connection's liveness", () => { |
| 52 | + const key = "stopped-preview"; |
| 53 | + |
| 54 | + updateTypingConnections({ |
| 55 | + key, |
| 56 | + connectionId: "preview-tab", |
| 57 | + typing: true, |
| 58 | + preview: "draft", |
| 59 | + now: 10_000, |
| 60 | + }); |
| 61 | + updateTypingConnections({ |
| 62 | + key, |
| 63 | + connectionId: "presence-tab", |
| 64 | + typing: true, |
| 65 | + now: 10_100, |
| 66 | + }); |
| 67 | + |
| 68 | + expect( |
| 69 | + updateTypingConnections({ |
| 70 | + key, |
| 71 | + connectionId: "preview-tab", |
| 72 | + typing: false, |
| 73 | + now: 10_200, |
| 74 | + }), |
| 75 | + ).toEqual({ typing: true }); |
| 76 | + expect( |
| 77 | + updateTypingConnections({ |
| 78 | + key, |
| 79 | + connectionId: "presence-tab", |
| 80 | + typing: false, |
| 81 | + now: 10_300, |
| 82 | + }), |
| 83 | + ).toEqual({ typing: false }); |
| 84 | + expect( |
| 85 | + updateTypingConnections({ |
| 86 | + key, |
| 87 | + connectionId: "missing-tab", |
| 88 | + typing: false, |
| 89 | + now: 10_400, |
| 90 | + }), |
| 91 | + ).toEqual({ typing: false }); |
| 92 | + }); |
| 93 | + |
| 94 | + it("expires stale previews while a refreshed boolean-only connection remains live", () => { |
| 95 | + const key = "expired-preview"; |
| 96 | + |
| 97 | + updateTypingConnections({ |
| 98 | + key, |
| 99 | + connectionId: "preview-tab", |
| 100 | + typing: true, |
| 101 | + preview: "expired draft", |
| 102 | + now: 10_000, |
| 103 | + }); |
| 104 | + updateTypingConnections({ |
| 105 | + key, |
| 106 | + connectionId: "presence-tab", |
| 107 | + typing: true, |
| 108 | + now: 11_000, |
| 109 | + }); |
| 110 | + |
| 111 | + expect( |
| 112 | + updateTypingConnections({ |
| 113 | + key, |
| 114 | + connectionId: "presence-tab", |
| 115 | + typing: true, |
| 116 | + now: 12_500, |
| 117 | + }), |
| 118 | + ).toEqual({ typing: true }); |
| 119 | + }); |
| 120 | +}); |
| 121 | + |
| 122 | +describe("session typing broadcast throttle", () => { |
| 123 | + it("broadcasts a changed preview while the actor remains typing", () => { |
| 124 | + const emit = vi.fn(() => true); |
| 125 | + const broadcast = (preview: string) => |
| 126 | + broadcastTypingThrottled({ |
| 127 | + key: "preview-change", |
| 128 | + typing: true, |
| 129 | + signature: `true\0${preview}`, |
| 130 | + intervalMs: 250, |
| 131 | + now: Date.now(), |
| 132 | + emit, |
| 133 | + }); |
| 134 | + |
| 135 | + expect(broadcast("first")).toBe(true); |
| 136 | + vi.advanceTimersByTime(100); |
| 137 | + expect(broadcast("second")).toBe(false); |
| 138 | + vi.advanceTimersByTime(150); |
| 139 | + |
| 140 | + expect(emit).toHaveBeenCalledTimes(2); |
| 141 | + }); |
| 142 | + |
| 143 | + it.each([ |
| 144 | + { label: "draft previews", intervalMs: 250, signature: "true\0draft" }, |
| 145 | + { label: "boolean-only presence", intervalMs: 1_000, signature: "true\0" }, |
| 146 | + ])("throttles $label at $intervalMs ms", ({ intervalMs, signature }) => { |
| 147 | + const emit = vi.fn(() => true); |
| 148 | + const broadcast = () => |
| 149 | + broadcastTypingThrottled({ |
| 150 | + key: signature, |
| 151 | + typing: true, |
| 152 | + signature, |
| 153 | + intervalMs, |
| 154 | + now: Date.now(), |
| 155 | + emit, |
| 156 | + }); |
| 157 | + |
| 158 | + broadcast(); |
| 159 | + vi.advanceTimersByTime(25); |
| 160 | + broadcast(); |
| 161 | + vi.advanceTimersByTime(intervalMs - 26); |
| 162 | + expect(emit).toHaveBeenCalledTimes(1); |
| 163 | + vi.advanceTimersByTime(1); |
| 164 | + expect(emit).toHaveBeenCalledTimes(2); |
| 165 | + }); |
| 166 | + |
| 167 | + it("emits only the latest draft at the trailing edge of a burst", () => { |
| 168 | + const previews: string[] = []; |
| 169 | + const broadcast = (preview: string) => |
| 170 | + broadcastTypingThrottled({ |
| 171 | + key: "preview-burst", |
| 172 | + typing: true, |
| 173 | + signature: `true\0${preview}`, |
| 174 | + intervalMs: 250, |
| 175 | + now: Date.now(), |
| 176 | + emit: () => { |
| 177 | + previews.push(preview); |
| 178 | + return true; |
| 179 | + }, |
| 180 | + }); |
| 181 | + |
| 182 | + broadcast("first"); |
| 183 | + vi.advanceTimersByTime(50); |
| 184 | + broadcast("second"); |
| 185 | + vi.advanceTimersByTime(50); |
| 186 | + broadcast("latest"); |
| 187 | + vi.advanceTimersByTime(150); |
| 188 | + |
| 189 | + expect(previews).toEqual(["first", "latest"]); |
| 190 | + }); |
| 191 | + |
| 192 | + it("preserves boolean-only cancellation and trailing stop behavior", () => { |
| 193 | + const updates: boolean[] = []; |
| 194 | + const broadcast = (typing: boolean) => |
| 195 | + broadcastTypingThrottled({ |
| 196 | + key: "boolean-only", |
| 197 | + typing, |
| 198 | + signature: `${typing}\0`, |
| 199 | + intervalMs: 1_000, |
| 200 | + now: Date.now(), |
| 201 | + emit: () => { |
| 202 | + updates.push(typing); |
| 203 | + return true; |
| 204 | + }, |
| 205 | + }); |
| 206 | + |
| 207 | + broadcast(true); |
| 208 | + vi.advanceTimersByTime(100); |
| 209 | + broadcast(false); |
| 210 | + vi.advanceTimersByTime(100); |
| 211 | + broadcast(true); |
| 212 | + vi.advanceTimersByTime(800); |
| 213 | + expect(updates).toEqual([true, true]); |
| 214 | + |
| 215 | + vi.advanceTimersByTime(100); |
| 216 | + broadcast(false); |
| 217 | + vi.advanceTimersByTime(900); |
| 218 | + expect(updates).toEqual([true, true, false]); |
| 219 | + }); |
| 220 | +}); |
0 commit comments