Files
ComfyUI_frontend/src/composables/useServerLogs.test.ts
Alexander Brown 661e3d7949 test: migrate as unknown as to @total-typescript/shoehorn (#10761)
*PR Created by the Glary-Bot Agent*

---

## Summary

- Replace all `as unknown as Type` assertions in 59 unit test files with
type-safe `@total-typescript/shoehorn` functions
- Use `fromPartial<Type>()` for partial mock objects where deep-partial
type-checks (21 files)
- Use `fromAny<Type>()` for fundamentally incompatible types: null,
undefined, primitives, variables, class expressions, and mocks with
test-specific extra properties that `PartialDeepObject` rejects
(remaining files)
- All explicit type parameters preserved so TypeScript return types are
correct
- Browser test `.spec.ts` files excluded (shoehorn unavailable in
`page.evaluate` browser context)

## Verification

- `pnpm typecheck` 
- `pnpm lint` 
- `pnpm format` 
- Pre-commit hooks passed (format + oxlint + eslint + typecheck)
- Migrated test files verified passing (ran representative subset)
- No test behavior changes — only type assertion syntax changed
- No UI changes — screenshots not applicable

┆Issue is synchronized with this [Notion
page](https://www.notion.so/PR-10761-test-migrate-as-unknown-as-to-total-typescript-shoehorn-3336d73d365081f6b8adc44db5dcc380)
by [Unito](https://www.unito.io)

---------

Co-authored-by: Glary-Bot <glary-bot@users.noreply.github.com>
Co-authored-by: Amp <amp@ampcode.com>
2026-03-30 19:20:18 -07:00

123 lines
3.2 KiB
TypeScript

import { fromAny } from '@total-typescript/shoehorn'
import { useEventListener } from '@vueuse/core'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { nextTick } from 'vue'
import { useServerLogs } from '@/composables/useServerLogs'
import type { LogsWsMessage } from '@/schemas/apiSchema'
import { api } from '@/scripts/api'
vi.mock('@/scripts/api', () => ({
api: {
subscribeLogs: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn()
}
}))
vi.mock('@vueuse/core', () => ({
useEventListener: vi.fn().mockReturnValue(vi.fn())
}))
describe('useServerLogs', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('should initialize with empty logs array', () => {
const { logs } = useServerLogs()
expect(logs.value).toEqual([])
})
it('should not subscribe to logs by default', () => {
useServerLogs()
expect(api.subscribeLogs).not.toHaveBeenCalled()
})
it('should subscribe to logs when immediate is true', () => {
useServerLogs({ immediate: true })
expect(api.subscribeLogs).toHaveBeenCalledWith(true)
})
it('should start listening when startListening is called', async () => {
const { startListening } = useServerLogs()
await startListening()
expect(api.subscribeLogs).toHaveBeenCalledWith(true)
})
it('should stop listening when stopListening is called', async () => {
const { startListening, stopListening } = useServerLogs()
await startListening()
await stopListening()
expect(api.subscribeLogs).toHaveBeenCalledWith(false)
})
it('should register event listener when starting', async () => {
const { startListening } = useServerLogs()
await startListening()
expect(vi.mocked(useEventListener)).toHaveBeenCalledWith(
api,
'logs',
expect.any(Function)
)
})
it('should handle log messages correctly', async () => {
const { logs, startListening } = useServerLogs()
await startListening()
// Get the callback that was registered with useEventListener
const eventCallback = vi.mocked(useEventListener).mock.calls[0][2] as (
event: CustomEvent<LogsWsMessage>
) => void
// Simulate receiving a log event
const mockEvent = new CustomEvent('logs', {
detail: fromAny<LogsWsMessage, unknown>({
type: 'logs',
entries: [{ m: 'Log message 1' }, { m: 'Log message 2' }]
})
}) as CustomEvent<LogsWsMessage>
eventCallback(mockEvent)
await nextTick()
expect(logs.value).toEqual(['Log message 1', 'Log message 2'])
})
it('should use the message filter if provided', async () => {
const { logs, startListening } = useServerLogs({
messageFilter: (msg) => msg !== 'remove me'
})
await startListening()
const eventCallback = vi.mocked(useEventListener).mock.calls[0][2] as (
event: CustomEvent<LogsWsMessage>
) => void
const mockEvent = new CustomEvent('logs', {
detail: fromAny<LogsWsMessage, unknown>({
type: 'logs',
entries: [
{ m: 'Log message 1 dont remove me' },
{ m: 'remove me' },
{ m: '' }
]
})
}) as CustomEvent<LogsWsMessage>
eventCallback(mockEvent)
await nextTick()
expect(logs.value).toEqual(['Log message 1 dont remove me', ''])
})
})