Files
ComfyUI_frontend/src/lib/litegraph/src/subgraph/svgBitmapCache.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

95 lines
2.7 KiB
TypeScript

import { fromPartial } from '@total-typescript/shoehorn'
import { describe, expect, it, vi } from 'vitest'
import { createBitmapCache } from './svgBitmapCache'
function mockSvg(
overrides: Partial<{ complete: boolean; naturalWidth: number }> = {}
) {
const img = new Image()
Object.defineProperty(img, 'complete', {
value: overrides.complete ?? true
})
Object.defineProperty(img, 'naturalWidth', {
value: overrides.naturalWidth ?? 16
})
return img
}
describe('createBitmapCache', () => {
const BITMAP_SIZE = 16
function mockGetContext(returnValue: CanvasRenderingContext2D | null) {
return vi
.spyOn(HTMLCanvasElement.prototype, 'getContext')
.mockImplementation(
(() => returnValue) as typeof HTMLCanvasElement.prototype.getContext
)
}
const stubContext = fromPartial<CanvasRenderingContext2D>({
drawImage: vi.fn()
})
it('returns the SVG when image is not yet complete', () => {
const svg = mockSvg({ complete: false, naturalWidth: 0 })
const cache = createBitmapCache(svg, BITMAP_SIZE)
expect(cache.get()).toBe(svg)
})
it('returns the SVG when naturalWidth is 0', () => {
const svg = mockSvg({ complete: true, naturalWidth: 0 })
const cache = createBitmapCache(svg, BITMAP_SIZE)
expect(cache.get()).toBe(svg)
})
it('creates a bitmap canvas once the SVG is loaded', () => {
const svg = mockSvg()
const cache = createBitmapCache(svg, BITMAP_SIZE)
mockGetContext(stubContext)
const result = cache.get()
expect(result).toBeInstanceOf(HTMLCanvasElement)
expect((result as HTMLCanvasElement).width).toBe(BITMAP_SIZE)
expect((result as HTMLCanvasElement).height).toBe(BITMAP_SIZE)
vi.restoreAllMocks()
})
it('returns the cached bitmap on subsequent calls', () => {
const svg = mockSvg()
const cache = createBitmapCache(svg, BITMAP_SIZE)
mockGetContext(stubContext)
const first = cache.get()
const second = cache.get()
expect(first).toBe(second)
vi.restoreAllMocks()
})
it('does not re-create the canvas on subsequent calls', () => {
const svg = mockSvg()
const cache = createBitmapCache(svg, BITMAP_SIZE)
mockGetContext(stubContext)
const createElementSpy = vi.spyOn(document, 'createElement')
cache.get()
const callCount = createElementSpy.mock.calls.length
cache.get()
expect(createElementSpy).toHaveBeenCalledTimes(callCount)
vi.restoreAllMocks()
})
it('falls back to SVG when canvas context is unavailable', () => {
const svg = mockSvg()
const cache = createBitmapCache(svg, BITMAP_SIZE)
mockGetContext(null)
expect(cache.get()).toBe(svg)
vi.restoreAllMocks()
})
})