Skip to content

Commit afdb292

Browse files
committed
cover more defaults
1 parent f448275 commit afdb292

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

src/structs_ffi.test.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ describe("Structs FFI", () => {
639639
expect(entriesView.getUint32(entryBaseOffset + textureOffset + 8, true)).toBe(0); // default multisampled = false
640640
});
641641

642-
it("should handle enum defaults in empty sub-structs (reproducing GPUDevice issue)", () => {
642+
it("should handle enum defaults in empty sub-structs (reproducing GPUDevice issue)", () => {
643643
// Create enums exactly like the real ones
644644
const SampleTypeEnum = defineEnum({
645645
"binding-not-used": 0,
@@ -708,4 +708,48 @@ describe("Structs FFI", () => {
708708
expect(multisampled).toBe(0); // false
709709
});
710710
});
711+
712+
describe("empty object defaults", () => {
713+
it("should apply defaults when packing empty objects", () => {
714+
const SamplerStruct = defineStruct([
715+
['type', 'u32', { default: 2 }] // filtering = 2
716+
] as const);
717+
718+
// Test packing with empty object vs undefined
719+
const emptyObjectPacked = SamplerStruct.pack({});
720+
721+
const emptyView = new DataView(emptyObjectPacked);
722+
723+
console.log('Empty object packed type value:', emptyView.getUint32(0, true));
724+
725+
// Empty object should apply the default value of 2
726+
expect(emptyView.getUint32(0, true)).toBe(2);
727+
});
728+
729+
it("should handle nested struct with empty object", () => {
730+
const SamplerStruct = defineStruct([
731+
['type', 'u32', { default: 2 }]
732+
] as const);
733+
734+
const EntryStruct = defineStruct([
735+
['binding', 'u32'],
736+
['sampler', SamplerStruct, { optional: true }]
737+
] as const);
738+
739+
// This mimics the GPUDevice scenario: sampler: {}
740+
const packed = EntryStruct.pack({
741+
binding: 1,
742+
sampler: {} // Empty object - should get defaults
743+
});
744+
745+
const view = new DataView(packed);
746+
const binding = view.getUint32(0, true);
747+
const samplerType = view.getUint32(4, true); // sampler.type after binding field
748+
749+
console.log('Nested - binding:', binding, 'samplerType:', samplerType);
750+
751+
expect(binding).toBe(1);
752+
expect(samplerType).toBe(2); // Should have default applied
753+
});
754+
});
711755
});

0 commit comments

Comments
 (0)