|
| 1 | +import test from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import http from 'node:http'; |
| 4 | +import { |
| 5 | + closeLoopbackServer, |
| 6 | + listenOnLoopback, |
| 7 | + skipWhenLoopbackUnavailable, |
| 8 | +} from '../../src/__tests__/test-utils/loopback.ts'; |
| 9 | + |
| 10 | +type FixtureCommand = { |
| 11 | + command: 'status' | 'tap' | 'type'; |
| 12 | + commandId?: string; |
| 13 | + statusCommandId?: string; |
| 14 | + delayMs?: number; |
| 15 | +}; |
| 16 | + |
| 17 | +type FixtureJournalEntry = { |
| 18 | + command: string; |
| 19 | + state: 'accepted' | 'started' | 'completed' | 'failed'; |
| 20 | +}; |
| 21 | + |
| 22 | +type FixtureResponse = { |
| 23 | + ok: boolean; |
| 24 | + data?: { |
| 25 | + commandId?: string; |
| 26 | + lifecycleState?: string; |
| 27 | + lifecycleCommand?: string; |
| 28 | + message?: string; |
| 29 | + }; |
| 30 | +}; |
| 31 | + |
| 32 | +test('iOS runner status transport stays visible while command execution remains serial', async (t) => { |
| 33 | + if (await skipWhenLoopbackUnavailable(t, 'iOS runner status-visible transport fixture')) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + const fixture = new StatusVisibleRunnerFixture(); |
| 38 | + const server = http.createServer((req, res) => { |
| 39 | + void fixture.handle(req, res); |
| 40 | + }); |
| 41 | + const port = await listenOnLoopback(server); |
| 42 | + t.after(async () => { |
| 43 | + await closeLoopbackServer(server); |
| 44 | + }); |
| 45 | + |
| 46 | + let longCommandCompleted = false; |
| 47 | + const longCommand = postCommand(port, { command: 'tap', commandId: 'long', delayMs: 300 }).then( |
| 48 | + (response) => { |
| 49 | + longCommandCompleted = true; |
| 50 | + return response; |
| 51 | + }, |
| 52 | + ); |
| 53 | + |
| 54 | + const visibleStatus = await pollStatus(port, 'long', (state) => state !== 'notAccepted'); |
| 55 | + assert.match(visibleStatus.data?.lifecycleState ?? '', /^(accepted|started)$/); |
| 56 | + assert.equal(longCommandCompleted, false, 'status returned before long command completed'); |
| 57 | + |
| 58 | + const secondCommand = postCommand(port, { command: 'type', commandId: 'second' }); |
| 59 | + const secondStatus = await pollStatus(port, 'second', (state) => state === 'accepted'); |
| 60 | + assert.equal(secondStatus.data?.lifecycleState, 'accepted'); |
| 61 | + |
| 62 | + assert.deepEqual(await longCommand, { ok: true, data: { message: 'tap completed' } }); |
| 63 | + assert.deepEqual(await secondCommand, { ok: true, data: { message: 'type completed' } }); |
| 64 | + assert.deepEqual(fixture.events, [ |
| 65 | + 'long:accepted', |
| 66 | + 'long:started', |
| 67 | + 'second:accepted', |
| 68 | + 'long:completed', |
| 69 | + 'second:started', |
| 70 | + 'second:completed', |
| 71 | + ]); |
| 72 | +}); |
| 73 | + |
| 74 | +class StatusVisibleRunnerFixture { |
| 75 | + public readonly events: string[] = []; |
| 76 | + private readonly journal = new Map<string, FixtureJournalEntry>(); |
| 77 | + private commandQueue: Promise<void> = Promise.resolve(); |
| 78 | + |
| 79 | + async handle(req: http.IncomingMessage, res: http.ServerResponse): Promise<void> { |
| 80 | + if (req.method !== 'POST') { |
| 81 | + writeJson(res, 404, { ok: false }); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + const command = await readJsonBody(req); |
| 86 | + if (command.command === 'status') { |
| 87 | + writeJson(res, 200, this.status(command.statusCommandId)); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + this.accept(command); |
| 92 | + const response = this.enqueue(command); |
| 93 | + writeJson(res, 200, await response); |
| 94 | + } |
| 95 | + |
| 96 | + private accept(command: FixtureCommand): void { |
| 97 | + if (!command.commandId) return; |
| 98 | + this.journal.set(command.commandId, { command: command.command, state: 'accepted' }); |
| 99 | + this.events.push(`${command.commandId}:accepted`); |
| 100 | + } |
| 101 | + |
| 102 | + private enqueue(command: FixtureCommand): Promise<FixtureResponse> { |
| 103 | + const response = this.commandQueue.then(() => this.execute(command)); |
| 104 | + this.commandQueue = response.then( |
| 105 | + () => {}, |
| 106 | + () => {}, |
| 107 | + ); |
| 108 | + return response; |
| 109 | + } |
| 110 | + |
| 111 | + private async execute(command: FixtureCommand): Promise<FixtureResponse> { |
| 112 | + this.update(command, 'started'); |
| 113 | + if (command.delayMs) { |
| 114 | + await delay(command.delayMs); |
| 115 | + } |
| 116 | + this.update(command, 'completed'); |
| 117 | + return { ok: true, data: { message: `${command.command} completed` } }; |
| 118 | + } |
| 119 | + |
| 120 | + private status(commandId: string | undefined): FixtureResponse { |
| 121 | + if (!commandId) return { ok: true, data: { lifecycleState: 'notAccepted' } }; |
| 122 | + const entry = this.journal.get(commandId); |
| 123 | + return { |
| 124 | + ok: true, |
| 125 | + data: { |
| 126 | + commandId, |
| 127 | + lifecycleState: entry?.state ?? 'notAccepted', |
| 128 | + lifecycleCommand: entry?.command, |
| 129 | + }, |
| 130 | + }; |
| 131 | + } |
| 132 | + |
| 133 | + private update(command: FixtureCommand, state: FixtureJournalEntry['state']): void { |
| 134 | + if (!command.commandId) return; |
| 135 | + this.journal.set(command.commandId, { command: command.command, state }); |
| 136 | + this.events.push(`${command.commandId}:${state}`); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +async function pollStatus( |
| 141 | + port: number, |
| 142 | + statusCommandId: string, |
| 143 | + predicate: (state: string) => boolean, |
| 144 | +): Promise<FixtureResponse> { |
| 145 | + const deadline = Date.now() + 1_000; |
| 146 | + while (Date.now() < deadline) { |
| 147 | + const response = await postCommand(port, { command: 'status', statusCommandId }); |
| 148 | + const state = response.data?.lifecycleState ?? 'notAccepted'; |
| 149 | + if (predicate(state)) return response; |
| 150 | + await delay(10); |
| 151 | + } |
| 152 | + throw new Error(`status for ${statusCommandId} did not reach expected state`); |
| 153 | +} |
| 154 | + |
| 155 | +async function postCommand(port: number, command: FixtureCommand): Promise<FixtureResponse> { |
| 156 | + const response = await fetch(`http://127.0.0.1:${port}`, { |
| 157 | + method: 'POST', |
| 158 | + headers: { 'content-type': 'application/json' }, |
| 159 | + body: JSON.stringify(command), |
| 160 | + }); |
| 161 | + return (await response.json()) as FixtureResponse; |
| 162 | +} |
| 163 | + |
| 164 | +async function readJsonBody(req: http.IncomingMessage): Promise<FixtureCommand> { |
| 165 | + const chunks: Buffer[] = []; |
| 166 | + for await (const chunk of req) { |
| 167 | + chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)); |
| 168 | + } |
| 169 | + return JSON.parse(Buffer.concat(chunks).toString('utf8')) as FixtureCommand; |
| 170 | +} |
| 171 | + |
| 172 | +function writeJson(res: http.ServerResponse, status: number, body: FixtureResponse): void { |
| 173 | + res.writeHead(status, { |
| 174 | + 'content-type': 'application/json', |
| 175 | + connection: 'close', |
| 176 | + }); |
| 177 | + res.end(JSON.stringify(body)); |
| 178 | +} |
| 179 | + |
| 180 | +function delay(ms: number): Promise<void> { |
| 181 | + return new Promise((resolve) => { |
| 182 | + setTimeout(resolve, ms); |
| 183 | + }); |
| 184 | +} |
0 commit comments