You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

104 lines
3.9 KiB

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { createTRPCClient } from '@trpc/client'
import { ipcLink } from 'electron-trpc/renderer'
import type { ElectronTRPCBridge } from 'electron-trpc/shared'
import type { AppRouter } from '../../src/main/trpc/app-router'
let callbacks: Set<Parameters<ElectronTRPCBridge['onMessage']>[0]>
let send: ReturnType<typeof vi.fn<ElectronTRPCBridge['sendMessage']>>
beforeEach(() => {
vi.useFakeTimers()
callbacks = new Set()
send = vi.fn<ElectronTRPCBridge['sendMessage']>()
const bridge: ElectronTRPCBridge = {
sendMessage: send,
onMessage: (callback) => {
callbacks.add(callback)
return () => {
callbacks.delete(callback)
}
}
}
vi.stubGlobal('electronTRPC', bridge)
})
afterEach(() => {
vi.useRealTimers()
vi.unstubAllGlobals()
})
function lastRequestId(): number {
const message = send.mock.calls.at(-1)![0]
if (message.method !== 'request') throw new Error('Expected a request')
return message.operation.id
}
const client = () =>
createTRPCClient<AppRouter>({
links: [
ipcLink({ requestTimeoutMs: 20, mutationTimeoutMs: 30, subscriptionStartTimeoutMs: 10 })
]
})
describe('IPC request lifetimes', () => {
it('times out a query, removes its observer and cancels delivery', async () => {
const pending = client()
.notes.list.query()
.catch((error: Error) => error.message)
await vi.advanceTimersByTimeAsync(21)
expect(await pending).toBe('Request timed out')
expect(callbacks.size).toBe(0)
expect(send.mock.calls.at(-1)?.[0]).toMatchObject({ method: 'request.cancel' })
expect(send).toHaveBeenCalledTimes(2)
})
it('does not repeat a mutation after timeout and explains ambiguous completion', async () => {
const pending = client()
.notes.add.mutate('once')
.catch((error: Error) => error.message)
await vi.advanceTimersByTimeAsync(31)
expect(await pending).toContain('may have completed')
expect(send.mock.calls.filter(([message]) => message.method === 'request')).toHaveLength(1)
})
it('honors AbortSignal and rejects already cancelled work before IPC', async () => {
const controller = new AbortController()
const pending = client()
.notes.list.query(undefined, { signal: controller.signal })
.catch((error: Error) => error.message)
controller.abort()
expect(await pending).toBe('Request cancelled')
expect(callbacks.size).toBe(0)
expect(vi.getTimerCount()).toBe(0)
send.mockClear()
await expect(
client().notes.list.query(undefined, { signal: controller.signal })
).rejects.toThrow('Request cancelled')
expect(send).not.toHaveBeenCalled()
})
it('bounds subscription startup but leaves an established stream alive', async () => {
const error = vi.fn()
const api = client()
api.theme.changed.subscribe(undefined, { onError: error })
await vi.advanceTimersByTimeAsync(11)
expect(error).toHaveBeenCalledOnce()
expect(callbacks.size).toBe(0)
const subscription = api.theme.changed.subscribe(undefined, { onError: error })
const id = lastRequestId()
for (const callback of callbacks) callback({ id, result: { type: 'started' } })
await vi.advanceTimersByTimeAsync(1000)
expect(error).toHaveBeenCalledOnce()
expect(callbacks.size).toBe(1)
subscription.unsubscribe()
expect(callbacks.size).toBe(0)
expect(vi.getTimerCount()).toBe(0)
})
it('allows deliberate long operations without a timeout', async () => {
const pending = client().notes.add.mutate('long', { context: { timeoutMs: 0 } })
const id = lastRequestId()
await vi.advanceTimersByTimeAsync(1000000)
for (const callback of callbacks)
callback({
id,
result: { type: 'data', data: { id: 'note', text: 'long', createdAt: 'now' } }
})
await expect(pending).resolves.toHaveProperty('text', 'long')
expect(send).toHaveBeenCalledOnce()
expect(callbacks.size).toBe(0)
})
})