|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 2 | +import path from 'node:path'; |
| 3 | +import type { ChildProcess } from 'node:child_process'; |
| 4 | +import { WebSocket } from 'ws'; |
| 5 | +import { |
| 6 | + createTempStateDir, |
| 7 | + getTestPort, |
| 8 | + startDaemon, |
| 9 | + waitForDaemon, |
| 10 | + stopDaemon, |
| 11 | + sendIpcCommand, |
| 12 | + connectMockApp, |
| 13 | + sendOperations, |
| 14 | + buildOperations, |
| 15 | + rootOp, |
| 16 | + addOp, |
| 17 | + ELEMENT_TYPE_FUNCTION, |
| 18 | + sleep, |
| 19 | +} from './helpers.js'; |
| 20 | + |
| 21 | +// A component read with no app attached answers from an empty tree, which is |
| 22 | +// indistinguishable from "nothing matched": a check that was never performed |
| 23 | +// reads as a check that passed. Every read must refuse instead. |
| 24 | +const COMPONENT_READS = [ |
| 25 | + { label: 'get-tree', command: { type: 'get-tree' } }, |
| 26 | + { label: 'get-component', command: { type: 'get-component', id: 1 } }, |
| 27 | + { label: 'find', command: { type: 'find', name: 'App' } }, |
| 28 | + { label: 'count', command: { type: 'count' } }, |
| 29 | + { label: 'errors', command: { type: 'errors' } }, |
| 30 | +] as const; |
| 31 | + |
| 32 | +describe('Component reads require an attached app (e2e)', () => { |
| 33 | + let stateDir: string; |
| 34 | + let port: number; |
| 35 | + let daemon: ChildProcess | null = null; |
| 36 | + let socketPath: string; |
| 37 | + |
| 38 | + beforeEach(async () => { |
| 39 | + stateDir = createTempStateDir(); |
| 40 | + port = getTestPort(); |
| 41 | + daemon = startDaemon(port, stateDir); |
| 42 | + await waitForDaemon(stateDir); |
| 43 | + socketPath = path.join(stateDir, 'daemon.sock'); |
| 44 | + }); |
| 45 | + |
| 46 | + afterEach(async () => { |
| 47 | + await stopDaemon(daemon, stateDir); |
| 48 | + daemon = null; |
| 49 | + }); |
| 50 | + |
| 51 | + for (const { label, command } of COMPONENT_READS) { |
| 52 | + it(`should refuse ${label} when no app has ever connected`, async () => { |
| 53 | + const resp = await sendIpcCommand(socketPath, command as never); |
| 54 | + |
| 55 | + expect(resp.ok).toBe(false); |
| 56 | + expect(resp.code).toBe('NO_APP_CONNECTED'); |
| 57 | + expect(resp.error).toContain('No app is connected'); |
| 58 | + // Nothing has ever attached, so there is no disconnect to describe. |
| 59 | + expect(resp.error).not.toContain('disconnected'); |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + it('should refuse a read issued after the app disconnected, not answer from its stale tree', async () => { |
| 64 | + const ws = await connectMockApp(port); |
| 65 | + sendOperations( |
| 66 | + ws, |
| 67 | + buildOperations(1, 100, (s) => [ |
| 68 | + rootOp(100), |
| 69 | + addOp(1, ELEMENT_TYPE_FUNCTION, 100, s('App')), |
| 70 | + ]), |
| 71 | + ); |
| 72 | + await sleep(200); |
| 73 | + |
| 74 | + const attached = await sendIpcCommand(socketPath, { type: 'find', name: 'App' }); |
| 75 | + expect(attached.ok).toBe(true); |
| 76 | + expect(attached.data).toHaveLength(1); |
| 77 | + |
| 78 | + ws.close(); |
| 79 | + await sleep(300); |
| 80 | + |
| 81 | + const afterDisconnect = await sendIpcCommand(socketPath, { type: 'find', name: 'App' }); |
| 82 | + expect(afterDisconnect.ok).toBe(false); |
| 83 | + expect(afterDisconnect.code).toBe('NO_APP_CONNECTED'); |
| 84 | + expect(afterDisconnect.error).toContain('disconnected'); |
| 85 | + |
| 86 | + const errors = await sendIpcCommand(socketPath, { type: 'errors' }); |
| 87 | + expect(errors.ok).toBe(false); |
| 88 | + expect(errors.code).toBe('NO_APP_CONNECTED'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should answer component reads while an app is attached', async () => { |
| 92 | + const ws = await connectMockApp(port); |
| 93 | + sendOperations( |
| 94 | + ws, |
| 95 | + buildOperations(1, 100, (s) => [ |
| 96 | + rootOp(100), |
| 97 | + addOp(1, ELEMENT_TYPE_FUNCTION, 100, s('App')), |
| 98 | + ]), |
| 99 | + ); |
| 100 | + await sleep(200); |
| 101 | + |
| 102 | + for (const { command } of COMPONENT_READS) { |
| 103 | + // `get-component` resolves a real id here; the others take no argument. |
| 104 | + const resp = await sendIpcCommand(socketPath, command as never); |
| 105 | + expect(resp.code).toBeUndefined(); |
| 106 | + } |
| 107 | + |
| 108 | + const errors = await sendIpcCommand(socketPath, { type: 'errors' }); |
| 109 | + expect(errors.ok).toBe(true); |
| 110 | + expect(errors.data).toEqual([]); |
| 111 | + |
| 112 | + ws.close(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should keep status and wait answerable with nothing attached', async () => { |
| 116 | + const status = await sendIpcCommand(socketPath, { type: 'status' }); |
| 117 | + |
| 118 | + expect(status.ok).toBe(true); |
| 119 | + expect((status.data as { connectedApps: number }).connectedApps).toBe(0); |
| 120 | + }); |
| 121 | +}); |
0 commit comments