Files
ComfyUI_frontend/tests-ui/tests/composables/useServerLogs.test.ts
Alexander Brown b264685052 lint: add tsconfig for browser_tests, fix existing violations (#5633)
## Summary

See https://typescript-eslint.io/blog/project-service/ for context.
Creates a browser_tests specific tsconfig so that they can be linted.

Does not add a package.json script to do the linting yet, but `pnpm exec
eslint browser_tests` should work for now.

┆Issue is synchronized with this [Notion
page](https://www.notion.so/PR-5633-lint-add-tsconfig-for-browser_tests-fix-existing-violations-2726d73d3650819d8ef2c4b0abc31e14)
by [Unito](https://www.unito.io)
2025-09-18 11:35:44 -07:00

122 lines
3.2 KiB
TypeScript

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: {
type: 'logs',
entries: [{ m: 'Log message 1' }, { m: 'Log message 2' }]
} as unknown as LogsWsMessage
}) 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: {
type: 'logs',
entries: [
{ m: 'Log message 1 dont remove me' },
{ m: 'remove me' },
{ m: '' }
]
} as unknown as LogsWsMessage
}) as CustomEvent<LogsWsMessage>
eventCallback(mockEvent)
await nextTick()
expect(logs.value).toEqual(['Log message 1 dont remove me', ''])
})
})