|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +import { LazyStream } from "@/api/workspace"; |
| 4 | +import { type UnidirectionalStream } from "@/websocket/eventStreamConnection"; |
| 5 | + |
| 6 | +function mockStream(): UnidirectionalStream<unknown> { |
| 7 | + return { |
| 8 | + url: "ws://test", |
| 9 | + addEventListener: vi.fn(), |
| 10 | + removeEventListener: vi.fn(), |
| 11 | + close: vi.fn(), |
| 12 | + }; |
| 13 | +} |
| 14 | + |
| 15 | +type StreamFactory = () => Promise<UnidirectionalStream<unknown>>; |
| 16 | + |
| 17 | +/** Creates a factory whose promise can be resolved manually. */ |
| 18 | +function deferredFactory() { |
| 19 | + let resolve!: (s: UnidirectionalStream<unknown>) => void; |
| 20 | + const factory: StreamFactory = vi.fn().mockReturnValue( |
| 21 | + new Promise<UnidirectionalStream<unknown>>((r) => { |
| 22 | + resolve = r; |
| 23 | + }), |
| 24 | + ); |
| 25 | + return { |
| 26 | + factory, |
| 27 | + resolve: (s?: UnidirectionalStream<unknown>) => resolve(s ?? mockStream()), |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +describe("LazyStream", () => { |
| 32 | + it("opens once and ignores subsequent calls", async () => { |
| 33 | + const factory: StreamFactory = vi.fn().mockResolvedValue(mockStream()); |
| 34 | + const lazy = new LazyStream(); |
| 35 | + |
| 36 | + await lazy.open(factory); |
| 37 | + await lazy.open(factory); |
| 38 | + |
| 39 | + expect(factory).toHaveBeenCalledOnce(); |
| 40 | + }); |
| 41 | + |
| 42 | + it("can reopen after close", async () => { |
| 43 | + const factory: StreamFactory = vi.fn().mockResolvedValue(mockStream()); |
| 44 | + const lazy = new LazyStream(); |
| 45 | + |
| 46 | + await lazy.open(factory); |
| 47 | + lazy.close(); |
| 48 | + await lazy.open(factory); |
| 49 | + |
| 50 | + expect(factory).toHaveBeenCalledTimes(2); |
| 51 | + }); |
| 52 | + |
| 53 | + it("closes the underlying stream", async () => { |
| 54 | + const stream = mockStream(); |
| 55 | + const lazy = new LazyStream(); |
| 56 | + |
| 57 | + await lazy.open(() => Promise.resolve(stream)); |
| 58 | + lazy.close(); |
| 59 | + |
| 60 | + expect(stream.close).toHaveBeenCalledOnce(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("deduplicates concurrent opens", async () => { |
| 64 | + const { factory, resolve } = deferredFactory(); |
| 65 | + const lazy = new LazyStream(); |
| 66 | + |
| 67 | + const p1 = lazy.open(factory); |
| 68 | + const p2 = lazy.open(factory); |
| 69 | + resolve(); |
| 70 | + await Promise.all([p1, p2]); |
| 71 | + |
| 72 | + expect(factory).toHaveBeenCalledOnce(); |
| 73 | + }); |
| 74 | + |
| 75 | + it("allows reopening after close during pending open", async () => { |
| 76 | + const { factory, resolve } = deferredFactory(); |
| 77 | + const lazy = new LazyStream(); |
| 78 | + |
| 79 | + const p = lazy.open(factory); |
| 80 | + lazy.close(); |
| 81 | + resolve(); |
| 82 | + await p.catch(() => {}); |
| 83 | + |
| 84 | + const factory2: StreamFactory = vi.fn().mockResolvedValue(mockStream()); |
| 85 | + await lazy.open(factory2); |
| 86 | + expect(factory2).toHaveBeenCalledOnce(); |
| 87 | + }); |
| 88 | +}); |
0 commit comments