|
1 |
| -import { Type as RecsType } from "@dojoengine/recs"; |
2 |
| -import { describe, expect, it } from "vitest"; |
| 1 | +import { Type as RecsType, Schema } from "@dojoengine/recs"; |
| 2 | +import { describe, expect, it, vi } from "vitest"; |
3 | 3 |
|
4 | 4 | import { convertValues } from "../utils";
|
5 | 5 |
|
@@ -108,4 +108,237 @@ describe("convertValues", () => {
|
108 | 108 | expect(result.nested).toEqual({ innerField: { value: "42" } });
|
109 | 109 | });
|
110 | 110 | });
|
| 111 | + |
| 112 | + it("should handle null and undefined values", () => { |
| 113 | + const schema: Schema = { |
| 114 | + name: RecsType.String, |
| 115 | + age: RecsType.Number, |
| 116 | + }; |
| 117 | + const values = { |
| 118 | + name: { value: "Alice", type: "string" }, |
| 119 | + age: undefined, |
| 120 | + }; |
| 121 | + const expected = { |
| 122 | + name: "Alice", |
| 123 | + age: undefined, |
| 124 | + }; |
| 125 | + expect(convertValues(schema, values)).toEqual(expected); |
| 126 | + }); |
| 127 | + |
| 128 | + it("should convert enum types correctly", () => { |
| 129 | + const schema: Schema = { |
| 130 | + status: RecsType.String, |
| 131 | + }; |
| 132 | + const values = { |
| 133 | + status: { value: { option: "ACTIVE" }, type: "enum" }, |
| 134 | + }; |
| 135 | + const expected = { |
| 136 | + status: "ACTIVE", |
| 137 | + }; |
| 138 | + expect(convertValues(schema, values)).toEqual(expected); |
| 139 | + }); |
| 140 | + |
| 141 | + it("should handle RecsType.StringArray with empty array", () => { |
| 142 | + const schema: Schema = { |
| 143 | + tags: RecsType.StringArray, |
| 144 | + }; |
| 145 | + const values = { |
| 146 | + tags: { value: [], type: "array" }, |
| 147 | + }; |
| 148 | + const expected = { |
| 149 | + tags: [], |
| 150 | + }; |
| 151 | + expect(convertValues(schema, values)).toEqual(expected); |
| 152 | + }); |
| 153 | + |
| 154 | + it("should handle RecsType.StringArray with enum items", () => { |
| 155 | + const schema: Schema = { |
| 156 | + tags: RecsType.StringArray, |
| 157 | + }; |
| 158 | + const values = { |
| 159 | + tags: { |
| 160 | + value: [ |
| 161 | + { value: { option: "TAG1" }, type: "enum" }, |
| 162 | + { value: { option: "TAG2" }, type: "enum" }, |
| 163 | + ], |
| 164 | + type: "array", |
| 165 | + }, |
| 166 | + }; |
| 167 | + const expected = { |
| 168 | + tags: ["TAG1", "TAG2"], |
| 169 | + }; |
| 170 | + expect(convertValues(schema, values)).toEqual(expected); |
| 171 | + }); |
| 172 | + |
| 173 | + it("should handle RecsType.StringArray with BigInt conversion", () => { |
| 174 | + const schema: Schema = { |
| 175 | + ids: RecsType.StringArray, |
| 176 | + }; |
| 177 | + const values = { |
| 178 | + ids: { |
| 179 | + value: [ |
| 180 | + { value: "12345678901234567890", type: "string" }, |
| 181 | + { value: "98765432109876543210", type: "string" }, |
| 182 | + ], |
| 183 | + type: "array", |
| 184 | + }, |
| 185 | + }; |
| 186 | + const expected = { |
| 187 | + ids: [ |
| 188 | + BigInt("12345678901234567890"), |
| 189 | + BigInt("98765432109876543210"), |
| 190 | + ], |
| 191 | + }; |
| 192 | + expect(convertValues(schema, values)).toEqual(expected); |
| 193 | + }); |
| 194 | + |
| 195 | + it("should fallback to string if BigInt conversion fails", () => { |
| 196 | + vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 197 | + |
| 198 | + const schema: Schema = { |
| 199 | + ids: RecsType.StringArray, |
| 200 | + }; |
| 201 | + const values = { |
| 202 | + ids: { |
| 203 | + value: [{ value: "invalid_bigint", type: "string" }], |
| 204 | + type: "array", |
| 205 | + }, |
| 206 | + }; |
| 207 | + const expected = { |
| 208 | + ids: ["invalid_bigint"], |
| 209 | + }; |
| 210 | + expect(convertValues(schema, values)).toEqual(expected); |
| 211 | + expect(console.warn).toHaveBeenCalledWith( |
| 212 | + "Failed to convert invalid_bigint to BigInt. Using string value instead." |
| 213 | + ); |
| 214 | + }); |
| 215 | + |
| 216 | + it("should handle RecsType.String", () => { |
| 217 | + const schema: Schema = { |
| 218 | + name: RecsType.String, |
| 219 | + }; |
| 220 | + const values = { |
| 221 | + name: { value: "Bob", type: "string" }, |
| 222 | + }; |
| 223 | + const expected = { |
| 224 | + name: "Bob", |
| 225 | + }; |
| 226 | + expect(convertValues(schema, values)).toEqual(expected); |
| 227 | + }); |
| 228 | + |
| 229 | + it("should handle RecsType.BigInt with valid BigInt", () => { |
| 230 | + const schema: Schema = { |
| 231 | + balance: RecsType.BigInt, |
| 232 | + }; |
| 233 | + const values = { |
| 234 | + balance: { value: "1000000000000000000", type: "string" }, |
| 235 | + }; |
| 236 | + const expected = { |
| 237 | + balance: BigInt("1000000000000000000"), |
| 238 | + }; |
| 239 | + expect(convertValues(schema, values)).toEqual(expected); |
| 240 | + }); |
| 241 | + |
| 242 | + it("should handle RecsType.Boolean", () => { |
| 243 | + const schema: Schema = { |
| 244 | + isActive: RecsType.Boolean, |
| 245 | + }; |
| 246 | + const values = { |
| 247 | + isActive: { value: true, type: "boolean" }, |
| 248 | + }; |
| 249 | + const expected = { |
| 250 | + isActive: true, |
| 251 | + }; |
| 252 | + expect(convertValues(schema, values)).toEqual(expected); |
| 253 | + }); |
| 254 | + |
| 255 | + it("should handle RecsType.Number", () => { |
| 256 | + const schema: Schema = { |
| 257 | + score: RecsType.Number, |
| 258 | + }; |
| 259 | + const values = { |
| 260 | + score: { value: "42", type: "string" }, |
| 261 | + }; |
| 262 | + const expected = { |
| 263 | + score: 42, |
| 264 | + }; |
| 265 | + expect(convertValues(schema, values)).toEqual(expected); |
| 266 | + }); |
| 267 | + |
| 268 | + it("should handle nested structs", () => { |
| 269 | + const nestedSchema: Schema = { |
| 270 | + street: RecsType.String, |
| 271 | + zip: RecsType.Number, |
| 272 | + }; |
| 273 | + const schema: Schema = { |
| 274 | + name: RecsType.String, |
| 275 | + address: nestedSchema, |
| 276 | + }; |
| 277 | + const values = { |
| 278 | + name: { value: "Charlie", type: "string" }, |
| 279 | + address: { |
| 280 | + value: { |
| 281 | + street: { value: "123 Main St", type: "string" }, |
| 282 | + zip: { value: "12345", type: "string" }, |
| 283 | + }, |
| 284 | + type: "struct", |
| 285 | + }, |
| 286 | + }; |
| 287 | + const expected = { |
| 288 | + name: "Charlie", |
| 289 | + address: { |
| 290 | + street: "123 Main St", |
| 291 | + zip: 12345, |
| 292 | + }, |
| 293 | + }; |
| 294 | + expect(convertValues(schema, values)).toEqual(expected); |
| 295 | + }); |
| 296 | + |
| 297 | + it("should handle map structures", () => { |
| 298 | + const nestedSchema: Schema = { |
| 299 | + key1: RecsType.String, |
| 300 | + key2: RecsType.Number, |
| 301 | + }; |
| 302 | + const schema: Schema = { |
| 303 | + config: nestedSchema, |
| 304 | + }; |
| 305 | + const values = { |
| 306 | + config: { |
| 307 | + value: new Map([ |
| 308 | + ["key1", { value: "value1", type: "string" }], |
| 309 | + ["key2", { value: "100", type: "string" }], |
| 310 | + ]), |
| 311 | + type: "struct", |
| 312 | + }, |
| 313 | + }; |
| 314 | + const expected = { |
| 315 | + config: { |
| 316 | + key1: "value1", |
| 317 | + key2: 100, |
| 318 | + }, |
| 319 | + }; |
| 320 | + expect(convertValues(schema, values)).toEqual(expected); |
| 321 | + }); |
| 322 | + |
| 323 | + it("should handle primitive fallback in default case", () => { |
| 324 | + const schema: Schema = { |
| 325 | + miscellaneous: RecsType.String, |
| 326 | + }; |
| 327 | + const values = { |
| 328 | + miscellaneous: { value: "some value", type: "unknown" }, |
| 329 | + }; |
| 330 | + const expected = { |
| 331 | + miscellaneous: "some value", |
| 332 | + }; |
| 333 | + expect(convertValues(schema, values)).toEqual(expected); |
| 334 | + }); |
| 335 | + |
| 336 | + it("should handle empty schema", () => { |
| 337 | + const schema: Schema = {}; |
| 338 | + const values = { |
| 339 | + anyKey: { value: "any value", type: "string" }, |
| 340 | + }; |
| 341 | + const expected = {}; |
| 342 | + expect(convertValues(schema, values)).toEqual(expected); |
| 343 | + }); |
111 | 344 | });
|
0 commit comments