import { test } from 'node:test' import assert from 'node:assert/strict' import { PromiseTimeoutError, withTimeout } from '../src/shared/utils/promise-timeout.ts' /** * 登出 capture 的超时底座(CR-12):原生桥卡死时必须让调用方继续清 token/uid。 * 这里只验超时原语本身;capture 走超时后仍写快照/继续清理的分支在 secret-store.test.ts。 */ test('withTimeout 在期限内原样返回结果', async () => { await assert.doesNotReject(async () => { const value = await withTimeout(Promise.resolve('ok'), 50) assert.equal(value, 'ok') }) }) test('withTimeout 对永不返回的 promise 按时抛 PromiseTimeoutError', async () => { const start = Date.now() await assert.rejects( () => withTimeout(new Promise(() => {}), 20), (error: unknown) => error instanceof PromiseTimeoutError && /超过 20ms 未完成/.test(error.message), ) assert.ok(Date.now() - start < 1000, '超时必须及时触发,不能等底层 promise') }) test('withTimeout 透传底层拒绝(吞掉会伪造成功)', async () => { await assert.rejects( () => withTimeout(Promise.reject(new Error('bridge down')), 50), /bridge down/, ) })