import type { ToolcraftModelFormatAdapter, ToolcraftModelSourceBundle, } from "./model-import-types"; import { createToolcraftModelFormatAdapterRegistry, type ToolcraftModelFormatAdapterRegistry, } from "./formats/model-format-adapter"; import { createToolcraftModelSourceBundle } from "./model-source-bundle"; export function modelSourceTestAdapter( format: ToolcraftModelFormatAdapter["format"], extension: string, ): ToolcraftModelFormatAdapter { return { adapterVersion: `${format}-test-1`, async decode() { throw new Error("decode is not used by source bundle tests"); }, format, rootExtensions: [extension], workerCapable: true, }; } function modelSourceTestRegistry(): ToolcraftModelFormatAdapterRegistry { return createToolcraftModelFormatAdapterRegistry([ modelSourceTestAdapter("glb", ".glb"), modelSourceTestAdapter("gltf", ".gltf"), modelSourceTestAdapter("obj", ".obj"), ]); } export function modelSourceTestBinaryFile( name: string, bytes: BlobPart | number[], options: { path?: string; type?: string } = {}, ): File { const file = new File( [Array.isArray(bytes) ? new Uint8Array(bytes) : bytes], name, { type: options.type ?? "application/octet-stream" }, ); if (options.path !== undefined) { Object.defineProperty(file, "webkitRelativePath", { configurable: true, value: options.path, }); } return file; } export function modelSourceTestTextFile( name: string, text: string, options: { path?: string; type?: string } = {}, ): File { return modelSourceTestBinaryFile(name, text, { ...options, type: options.type ?? "model/gltf+json", }); } export function modelSourceTestGltfWithBuffers( uris: readonly string[], extra: Record = {}, ): string { return JSON.stringify({ asset: { version: "2.0" }, buffers: uris.map((uri) => ({ byteLength: 3, uri })), ...extra, }); } export async function createModelSourceTestBundle( files: readonly File[], limits?: Parameters[1]["limits"], ): Promise { return createToolcraftModelSourceBundle(files, { limits, registry: modelSourceTestRegistry(), }); }