|
| 1 | +import { expect, describe, it } from "bun:test" |
| 2 | +import { defineStruct } from "../structs_ffi" |
| 3 | + |
| 4 | +describe("char* automatic unpacking", () => { |
| 5 | + it("should automatically unpack char* to string when lengthOf field exists", () => { |
| 6 | + const StringStruct = defineStruct([ |
| 7 | + ["data", "char*"], |
| 8 | + ["length", "u64", { lengthOf: "data" }], |
| 9 | + ] as const) |
| 10 | + |
| 11 | + const testString = "Hello, World!" |
| 12 | + const packed = StringStruct.pack({ data: testString }) |
| 13 | + const unpacked = StringStruct.unpack(packed) |
| 14 | + |
| 15 | + expect(typeof unpacked.data).toBe("string") |
| 16 | + expect(unpacked.data).toBe(testString) |
| 17 | + expect(unpacked.length).toBe(BigInt(Buffer.byteLength(testString))) |
| 18 | + }) |
| 19 | + |
| 20 | + it("should handle emoji strings correctly", () => { |
| 21 | + const StringStruct = defineStruct([ |
| 22 | + ["data", "char*"], |
| 23 | + ["length", "u32", { lengthOf: "data" }], |
| 24 | + ] as const) |
| 25 | + |
| 26 | + const testString = "Hello 🌍🎉✨" |
| 27 | + const packed = StringStruct.pack({ data: testString }) |
| 28 | + const unpacked = StringStruct.unpack(packed) |
| 29 | + |
| 30 | + expect(unpacked.data).toBe(testString) |
| 31 | + }) |
| 32 | + |
| 33 | + it("should handle complex unicode grapheme clusters", () => { |
| 34 | + const StringStruct = defineStruct([ |
| 35 | + ["data", "char*"], |
| 36 | + ["length", "u64", { lengthOf: "data" }], |
| 37 | + ] as const) |
| 38 | + |
| 39 | + const testString = "👨👩👧👦🏳️🌈🇺🇸" |
| 40 | + const packed = StringStruct.pack({ data: testString }) |
| 41 | + const unpacked = StringStruct.unpack(packed) |
| 42 | + |
| 43 | + expect(unpacked.data).toBe(testString) |
| 44 | + }) |
| 45 | + |
| 46 | + it("should handle null char* pointers", () => { |
| 47 | + const StringStruct = defineStruct([ |
| 48 | + ["data", "char*", { optional: true }], |
| 49 | + ["length", "u64", { lengthOf: "data" }], |
| 50 | + ] as const) |
| 51 | + |
| 52 | + const packed = StringStruct.pack({ data: null }) |
| 53 | + const unpacked = StringStruct.unpack(packed) |
| 54 | + |
| 55 | + expect(unpacked.data).toBeNull() |
| 56 | + expect(unpacked.length).toBe(0n) |
| 57 | + }) |
| 58 | + |
| 59 | + it("should handle empty strings", () => { |
| 60 | + const StringStruct = defineStruct([ |
| 61 | + ["data", "char*"], |
| 62 | + ["length", "u64", { lengthOf: "data" }], |
| 63 | + ] as const) |
| 64 | + |
| 65 | + const packed = StringStruct.pack({ data: "" }) |
| 66 | + const unpacked = StringStruct.unpack(packed) |
| 67 | + |
| 68 | + expect(unpacked.data).toBeNull() |
| 69 | + expect(unpacked.length).toBe(0n) |
| 70 | + }) |
| 71 | + |
| 72 | + it("should handle multiple char* fields in one struct", () => { |
| 73 | + const MultiStringStruct = defineStruct([ |
| 74 | + ["field1", "char*"], |
| 75 | + ["length1", "u32", { lengthOf: "field1" }], |
| 76 | + ["field2", "char*"], |
| 77 | + ["length2", "u32", { lengthOf: "field2" }], |
| 78 | + ["field3", "char*"], |
| 79 | + ["length3", "u32", { lengthOf: "field3" }], |
| 80 | + ] as const) |
| 81 | + |
| 82 | + const packed = MultiStringStruct.pack({ |
| 83 | + field1: "ASCII text", |
| 84 | + field2: "Emoji 🎉", |
| 85 | + field3: "Combined é", |
| 86 | + }) |
| 87 | + const unpacked = MultiStringStruct.unpack(packed) |
| 88 | + |
| 89 | + expect(unpacked.field1).toBe("ASCII text") |
| 90 | + expect(unpacked.field2).toBe("Emoji 🎉") |
| 91 | + expect(unpacked.field3).toBe("Combined é") |
| 92 | + }) |
| 93 | + |
| 94 | + it("should handle char* in nested structs", () => { |
| 95 | + const InnerStruct = defineStruct([ |
| 96 | + ["content", "char*"], |
| 97 | + ["contentLength", "u64", { lengthOf: "content" }], |
| 98 | + ] as const) |
| 99 | + |
| 100 | + const OuterStruct = defineStruct([ |
| 101 | + ["id", "u32"], |
| 102 | + ["message", InnerStruct], |
| 103 | + ["timestamp", "u64"], |
| 104 | + ] as const) |
| 105 | + |
| 106 | + const packed = OuterStruct.pack({ |
| 107 | + id: 42, |
| 108 | + message: { |
| 109 | + content: "Hello! 👋 How are you? 😊", |
| 110 | + }, |
| 111 | + timestamp: 1234567890n, |
| 112 | + }) |
| 113 | + const unpacked = OuterStruct.unpack(packed) |
| 114 | + |
| 115 | + expect(unpacked.message.content).toBe("Hello! 👋 How are you? 😊") |
| 116 | + expect(unpacked.id).toBe(42) |
| 117 | + expect(unpacked.timestamp).toBe(1234567890n) |
| 118 | + }) |
| 119 | + |
| 120 | + it("should handle char* with various length field types", () => { |
| 121 | + const testString = "Test string 🎉" |
| 122 | + |
| 123 | + // Test with u32 |
| 124 | + const Struct32 = defineStruct([ |
| 125 | + ["data", "char*"], |
| 126 | + ["length", "u32", { lengthOf: "data" }], |
| 127 | + ] as const) |
| 128 | + |
| 129 | + const packed32 = Struct32.pack({ data: testString }) |
| 130 | + const unpacked32 = Struct32.unpack(packed32) |
| 131 | + expect(unpacked32.data).toBe(testString) |
| 132 | + |
| 133 | + // Test with u64 |
| 134 | + const Struct64 = defineStruct([ |
| 135 | + ["data", "char*"], |
| 136 | + ["length", "u64", { lengthOf: "data" }], |
| 137 | + ] as const) |
| 138 | + |
| 139 | + const packed64 = Struct64.pack({ data: testString }) |
| 140 | + const unpacked64 = Struct64.unpack(packed64) |
| 141 | + expect(unpacked64.data).toBe(testString) |
| 142 | + |
| 143 | + // Test with u16 |
| 144 | + const Struct16 = defineStruct([ |
| 145 | + ["data", "char*"], |
| 146 | + ["length", "u16", { lengthOf: "data" }], |
| 147 | + ] as const) |
| 148 | + |
| 149 | + const packed16 = Struct16.pack({ data: testString }) |
| 150 | + const unpacked16 = Struct16.unpack(packed16) |
| 151 | + expect(unpacked16.data).toBe(testString) |
| 152 | + }) |
| 153 | + |
| 154 | + it("should return pointer for char* without lengthOf field", () => { |
| 155 | + const PointerStruct = defineStruct([ |
| 156 | + ["data", "char*"], |
| 157 | + ["someOtherField", "u32"], |
| 158 | + ] as const) |
| 159 | + |
| 160 | + const packed = PointerStruct.pack({ |
| 161 | + data: "test", |
| 162 | + someOtherField: 42, |
| 163 | + }) |
| 164 | + const unpacked = PointerStruct.unpack(packed) |
| 165 | + |
| 166 | + // Without lengthOf, should return the pointer value (number) |
| 167 | + expect(typeof unpacked.data).toBe("number") |
| 168 | + expect(unpacked.data).toBeGreaterThan(0) |
| 169 | + expect(unpacked.someOtherField).toBe(42) |
| 170 | + }) |
| 171 | + |
| 172 | + it("should handle very long strings", () => { |
| 173 | + const StringStruct = defineStruct([ |
| 174 | + ["data", "char*"], |
| 175 | + ["length", "u32", { lengthOf: "data" }], |
| 176 | + ] as const) |
| 177 | + |
| 178 | + const testString = "a".repeat(10000) + "🎉".repeat(1000) |
| 179 | + const packed = StringStruct.pack({ data: testString }) |
| 180 | + const unpacked = StringStruct.unpack(packed) |
| 181 | + |
| 182 | + expect(unpacked.data).toBe(testString) |
| 183 | + expect(unpacked.length).toBe(Buffer.byteLength(testString)) |
| 184 | + }) |
| 185 | + |
| 186 | + it("should handle strings with mixed ascii and unicode", () => { |
| 187 | + const StringStruct = defineStruct([ |
| 188 | + ["data", "char*"], |
| 189 | + ["length", "u64", { lengthOf: "data" }], |
| 190 | + ] as const) |
| 191 | + |
| 192 | + const testString = "Hello Café 日本語 🌍 Привет" |
| 193 | + const packed = StringStruct.pack({ data: testString }) |
| 194 | + const unpacked = StringStruct.unpack(packed) |
| 195 | + |
| 196 | + expect(unpacked.data).toBe(testString) |
| 197 | + }) |
| 198 | +}) |
0 commit comments