|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import fs from "node:fs"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | +import { ToolStarterApp } from "../../tools/object-vector-studio-v2/js/ToolStarterApp.js"; |
| 5 | +import { ObjectVectorStudioV2SchemaService } from "../../tools/object-vector-studio-v2/js/services/ObjectVectorStudioV2SchemaService.js"; |
| 6 | + |
| 7 | +function createJsonResponse(payload) { |
| 8 | + return { |
| 9 | + ok: true, |
| 10 | + status: 200, |
| 11 | + async json() { |
| 12 | + return payload; |
| 13 | + } |
| 14 | + }; |
| 15 | +} |
| 16 | + |
| 17 | +function createLocalFetch() { |
| 18 | + return async (url) => { |
| 19 | + const targetPath = fileURLToPath(url); |
| 20 | + return createJsonResponse(JSON.parse(fs.readFileSync(targetPath, "utf8"))); |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +function clone(value) { |
| 25 | + return JSON.parse(JSON.stringify(value)); |
| 26 | +} |
| 27 | + |
| 28 | +function createPayload() { |
| 29 | + return { |
| 30 | + version: 1, |
| 31 | + toolId: "object-vector-studio-v2", |
| 32 | + name: "Delete Cleanup Object Set", |
| 33 | + vectorMaps: { |
| 34 | + schema: "html-js-gaming.test-vector-map", |
| 35 | + version: 1, |
| 36 | + name: "Delete Cleanup Roles", |
| 37 | + source: "object-vector-studio-v2", |
| 38 | + objectVectorRoles: { |
| 39 | + keepRole: { |
| 40 | + objectId: "object.test.keep", |
| 41 | + tags: ["keep"] |
| 42 | + }, |
| 43 | + tempRole: { |
| 44 | + objectId: "object.test.temp", |
| 45 | + tags: ["temp"] |
| 46 | + } |
| 47 | + }, |
| 48 | + vectors: [] |
| 49 | + }, |
| 50 | + objects: [ |
| 51 | + { |
| 52 | + id: "object.test.keep", |
| 53 | + name: "Keep", |
| 54 | + tags: ["keep"], |
| 55 | + shapes: [] |
| 56 | + }, |
| 57 | + { |
| 58 | + id: "object.test.temp", |
| 59 | + name: "Temp", |
| 60 | + tags: ["temp"], |
| 61 | + shapes: [] |
| 62 | + } |
| 63 | + ] |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +async function createSchemaService() { |
| 68 | + const service = new ObjectVectorStudioV2SchemaService({ |
| 69 | + fetchRef: createLocalFetch(), |
| 70 | + schemaUrl: new URL("../../tools/schemas/tools/object-vector-studio-v2.schema.json", import.meta.url) |
| 71 | + }); |
| 72 | + await service.loadSchema(); |
| 73 | + return service; |
| 74 | +} |
| 75 | + |
| 76 | +export async function run() { |
| 77 | + const service = await createSchemaService(); |
| 78 | + const payload = createPayload(); |
| 79 | + assert.deepEqual(service.validatePayload(payload).errors, []); |
| 80 | + |
| 81 | + const missingObjectIdPayload = createPayload(); |
| 82 | + delete missingObjectIdPayload.vectorMaps.objectVectorRoles.tempRole.objectId; |
| 83 | + const missingObjectIdValidation = service.validatePayload(missingObjectIdPayload); |
| 84 | + assert.equal(missingObjectIdValidation.ok, false); |
| 85 | + assert.equal( |
| 86 | + missingObjectIdValidation.errors.some((message) => message === "root.vectorMaps.objectVectorRoles.tempRole.objectId is required."), |
| 87 | + true, |
| 88 | + ); |
| 89 | + |
| 90 | + const staleReferencePayload = createPayload(); |
| 91 | + staleReferencePayload.objects = staleReferencePayload.objects.filter((object) => object.id !== "object.test.temp"); |
| 92 | + const staleReferenceValidation = service.validatePayload(staleReferencePayload); |
| 93 | + assert.equal(staleReferenceValidation.ok, false); |
| 94 | + assert.equal( |
| 95 | + staleReferenceValidation.errors.some((message) => message === "root.vectorMaps.objectVectorRoles.tempRole.objectId object.test.temp must reference an existing object."), |
| 96 | + true, |
| 97 | + ); |
| 98 | + |
| 99 | + const deletePayload = clone(payload); |
| 100 | + deletePayload.objects = deletePayload.objects.filter((object) => object.id !== "object.test.temp"); |
| 101 | + const app = Object.create(ToolStarterApp.prototype); |
| 102 | + app.removeDeletedObjectReferences(deletePayload, "object.test.temp"); |
| 103 | + assert.equal(Object.hasOwn(deletePayload.vectorMaps.objectVectorRoles, "tempRole"), false); |
| 104 | + assert.equal(deletePayload.vectorMaps.objectVectorRoles.keepRole.objectId, "object.test.keep"); |
| 105 | + assert.equal( |
| 106 | + Object.values(deletePayload.vectorMaps.objectVectorRoles).some((binding) => !Object.hasOwn(binding, "objectId")), |
| 107 | + false, |
| 108 | + ); |
| 109 | + assert.deepEqual(service.validatePayload(deletePayload).errors, []); |
| 110 | +} |
0 commit comments