Files
ComfyUI_frontend/src/scripts/metadata/gltf.test.ts
Alexander Brown 10feb1fd5b chore: migrate tests from tests-ui/ to colocate with source files (#7811)
## Summary

Migrates all unit tests from `tests-ui/` to colocate with their source
files in `src/`, improving discoverability and maintainability.

## Changes

- **What**: Relocated all unit tests to be adjacent to the code they
test, following the `<source>.test.ts` naming convention
- **Config**: Updated `vitest.config.ts` to remove `tests-ui` include
pattern and `@tests-ui` alias
- **Docs**: Moved testing documentation to `docs/testing/` with updated
paths and patterns

## Review Focus

- Migration patterns documented in
`temp/plans/migrate-tests-ui-to-src.md`
- Tests use `@/` path aliases instead of relative imports
- Shared fixtures placed in `__fixtures__/` directories

┆Issue is synchronized with this [Notion
page](https://www.notion.so/PR-7811-chore-migrate-tests-from-tests-ui-to-colocate-with-source-files-2da6d73d36508147a4cce85365dee614)
by [Unito](https://www.unito.io)

---------

Co-authored-by: Amp <amp@ampcode.com>
Co-authored-by: GitHub Action <action@github.com>
2026-01-05 16:32:24 -08:00

164 lines
4.5 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { ASCII, GltfSizeBytes } from '@/types/metadataTypes'
import { getGltfBinaryMetadata } from './gltf'
describe('GLTF binary metadata parser', () => {
const createGLTFFileStructure = () => {
const header = new ArrayBuffer(GltfSizeBytes.HEADER)
const headerView = new DataView(header)
return { header, headerView }
}
const jsonToBinary = (json: object) => {
const jsonString = JSON.stringify(json)
const jsonData = new TextEncoder().encode(jsonString)
return jsonData
}
const createJSONChunk = (jsonData: ArrayBuffer) => {
const chunkHeader = new ArrayBuffer(GltfSizeBytes.CHUNK_HEADER)
const chunkView = new DataView(chunkHeader)
chunkView.setUint32(0, jsonData.byteLength, true)
chunkView.setUint32(4, ASCII.JSON, true)
return chunkHeader
}
const setVersionHeader = (headerView: DataView, version: number) => {
headerView.setUint32(4, version, true)
}
const setTypeHeader = (headerView: DataView, type: number) => {
headerView.setUint32(0, type, true)
}
const setTotalLengthHeader = (headerView: DataView, length: number) => {
headerView.setUint32(8, length, true)
}
const setHeaders = (headerView: DataView, jsonData: ArrayBuffer) => {
setTypeHeader(headerView, ASCII.GLTF)
setVersionHeader(headerView, 2)
setTotalLengthHeader(
headerView,
GltfSizeBytes.HEADER + GltfSizeBytes.CHUNK_HEADER + jsonData.byteLength
)
}
function createMockGltfFile(jsonContent: object): File {
const jsonData = jsonToBinary(jsonContent)
const { header, headerView } = createGLTFFileStructure()
setHeaders(headerView, jsonData.buffer)
const chunkHeader = createJSONChunk(jsonData.buffer)
const fileContent = new Uint8Array(
header.byteLength + chunkHeader.byteLength + jsonData.byteLength
)
fileContent.set(new Uint8Array(header), 0)
fileContent.set(new Uint8Array(chunkHeader), header.byteLength)
fileContent.set(jsonData, header.byteLength + chunkHeader.byteLength)
return new File([fileContent], 'test.glb', { type: 'model/gltf-binary' })
}
it('should extract workflow metadata from GLTF binary file', async () => {
const testWorkflow = {
nodes: [
{
id: 1,
type: 'TestNode',
pos: [100, 100]
}
],
links: []
}
const mockFile = createMockGltfFile({
asset: {
version: '2.0',
generator: 'ComfyUI GLTF Test',
extras: {
workflow: testWorkflow
}
},
scenes: []
})
const metadata = await getGltfBinaryMetadata(mockFile)
expect(metadata).toBeDefined()
expect(metadata.workflow).toBeDefined()
const workflow = metadata.workflow as {
nodes: Array<{ id: number; type: string }>
}
expect(workflow.nodes[0].id).toBe(1)
expect(workflow.nodes[0].type).toBe('TestNode')
})
it('should extract prompt metadata from GLTF binary file', async () => {
const testPrompt = {
node1: {
class_type: 'TestNode',
inputs: {
seed: 123456
}
}
}
const mockFile = createMockGltfFile({
asset: {
version: '2.0',
generator: 'ComfyUI GLTF Test',
extras: {
prompt: testPrompt
}
},
scenes: []
})
const metadata = await getGltfBinaryMetadata(mockFile)
expect(metadata).toBeDefined()
expect(metadata.prompt).toBeDefined()
const prompt = metadata.prompt as Record<string, any>
expect(prompt.node1.class_type).toBe('TestNode')
expect(prompt.node1.inputs.seed).toBe(123456)
})
it('should handle string JSON content', async () => {
const workflowStr = JSON.stringify({
nodes: [{ id: 1, type: 'StringifiedNode' }],
links: []
})
const mockFile = createMockGltfFile({
asset: {
version: '2.0',
extras: {
workflow: workflowStr // As string instead of object
}
}
})
const metadata = await getGltfBinaryMetadata(mockFile)
expect(metadata).toBeDefined()
expect(metadata.workflow).toBeDefined()
const workflow = metadata.workflow as {
nodes: Array<{ id: number; type: string }>
}
expect(workflow.nodes[0].type).toBe('StringifiedNode')
})
it('should handle invalid GLTF binary files gracefully', async () => {
const invalidEmptyFile = new File([], 'invalid.glb')
const metadata = await getGltfBinaryMetadata(invalidEmptyFile)
expect(metadata).toEqual({})
})
})