|
| 1 | +/** |
| 2 | + * Tests for getPatchInstructions when the compiled writer is unavailable |
| 3 | + * (CSP/eval-restricted environments). vi.mock is hoisted by vitest so this |
| 4 | + * file's entire module graph sees getCompiledWriter returning undefined, |
| 5 | + * forcing the typed-binary fallback path in partialIO.ts. |
| 6 | + */ |
| 7 | +import { describe, expect, vi } from 'vitest'; |
| 8 | +import { it } from 'typegpu-testing-utility'; |
| 9 | +import type { TypedArray } from '../src/shared/utilityTypes.ts'; |
| 10 | +import type { WriteInstruction } from '../src/data/partialIO.ts'; |
| 11 | + |
| 12 | +vi.mock('../src/data/compiledIO.ts', () => ({ |
| 13 | + EVAL_ALLOWED_IN_ENV: false, |
| 14 | + getCompiledWriter: () => undefined, |
| 15 | + buildWriter: () => '', |
| 16 | +})); |
| 17 | + |
| 18 | +// Dynamic imports are required AFTER vi.mock to get the mocked versions. |
| 19 | +const { getPatchInstructions } = await import('../src/data/partialIO.ts'); |
| 20 | +const d = await import('../src/data/index.ts'); |
| 21 | + |
| 22 | +function expectInstruction( |
| 23 | + instruction: WriteInstruction, |
| 24 | + { |
| 25 | + start, |
| 26 | + length, |
| 27 | + expectedData, |
| 28 | + }: { |
| 29 | + start: number; |
| 30 | + length: number; |
| 31 | + expectedData: TypedArray | TypedArray[]; |
| 32 | + }, |
| 33 | +): void { |
| 34 | + expect(instruction.gpuOffset).toBe(start); |
| 35 | + expect(instruction.data.byteLength).toBe(length); |
| 36 | + |
| 37 | + const dataArrays = Array.isArray(expectedData) ? expectedData : [expectedData]; |
| 38 | + const totalByteLength = dataArrays.reduce((acc, arr) => acc + arr.byteLength, 0); |
| 39 | + const mergedExpected = new Uint8Array(totalByteLength); |
| 40 | + let offset = 0; |
| 41 | + for (const arr of dataArrays) { |
| 42 | + mergedExpected.set(new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength), offset); |
| 43 | + offset += arr.byteLength; |
| 44 | + } |
| 45 | + |
| 46 | + expect(instruction.data).toHaveLength(totalByteLength); |
| 47 | + expect(new Uint8Array(instruction.data)).toStrictEqual(mergedExpected); |
| 48 | +} |
| 49 | + |
| 50 | +describe('getPatchInstructions (no-eval / typed-binary fallback)', () => { |
| 51 | + it('should produce correct instructions for a scalar', () => { |
| 52 | + const instructions = getPatchInstructions(d.u32, 3) as [WriteInstruction]; |
| 53 | + expect(instructions).toHaveLength(1); |
| 54 | + expectInstruction(instructions[0], { |
| 55 | + start: 0, |
| 56 | + length: 4, |
| 57 | + expectedData: new Uint32Array([3]), |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should produce correct instructions for a struct', () => { |
| 62 | + const struct = d.struct({ |
| 63 | + a: d.u32, |
| 64 | + b: d.vec3f, |
| 65 | + c: d.struct({ d: d.u32 }), |
| 66 | + }); |
| 67 | + |
| 68 | + const instructions = getPatchInstructions(struct, { |
| 69 | + a: 3, |
| 70 | + b: d.vec3f(1, 2, 3), |
| 71 | + c: { d: 4 }, |
| 72 | + }) as [WriteInstruction]; |
| 73 | + |
| 74 | + expect(instructions).toHaveLength(1); |
| 75 | + expectInstruction(instructions[0], { |
| 76 | + start: 0, |
| 77 | + length: 32, |
| 78 | + expectedData: [ |
| 79 | + new Uint32Array([3, 0, 0, 0]), |
| 80 | + new Float32Array([1, 2, 3]), |
| 81 | + new Uint32Array([4]), |
| 82 | + ], |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should handle sparse array updates', () => { |
| 87 | + const struct = d.struct({ |
| 88 | + a: d.u32, |
| 89 | + b: d.arrayOf(d.vec3f, 4), |
| 90 | + }); |
| 91 | + |
| 92 | + const instructions = getPatchInstructions(struct, { |
| 93 | + b: { 1: d.vec3f(4, 5, 6) }, |
| 94 | + }) as [WriteInstruction]; |
| 95 | + |
| 96 | + expect(instructions).toHaveLength(1); |
| 97 | + expectInstruction(instructions[0], { |
| 98 | + start: 32, // offset of b[1]: 16 (b start) + 1 * 16 (element stride) |
| 99 | + length: 12, |
| 100 | + expectedData: new Float32Array([4, 5, 6]), |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should split instructions at non-contiguous gaps', () => { |
| 105 | + const struct = d.struct({ |
| 106 | + a: d.u32, |
| 107 | + b: d.vec3f, |
| 108 | + }); |
| 109 | + |
| 110 | + // Only patch b, leaving a out — creates a gap after start |
| 111 | + const instructions = getPatchInstructions(struct, { b: d.vec3f(1, 2, 3) }) as [ |
| 112 | + WriteInstruction, |
| 113 | + ]; |
| 114 | + |
| 115 | + expect(instructions).toHaveLength(1); |
| 116 | + expectInstruction(instructions[0], { |
| 117 | + start: 16, |
| 118 | + length: 12, |
| 119 | + expectedData: new Float32Array([1, 2, 3]), |
| 120 | + }); |
| 121 | + }); |
| 122 | +}); |
0 commit comments