import { test } from 'node:test' import assert from 'node:assert/strict' import { unzipSync } from 'fflate' import { createWebCadFacade } from '../src/facade/mockFacade' import { decodeFcstdPropertyValue } from '../src/facade/fcstd' import type { DocumentSnapshot, LinkSubListValue } from '../src/facade/types' const initialValue: LinkSubListValue = { schemaVersion: 1, entries: [{ objectId: 'SourceBox', subElement: 'Face1' }, { objectId: 'SourceBox', subElement: 'Face2' }, { objectId: 'SecondBox', subElement: 'Face1' }] } const editedValue: LinkSubListValue = { schemaVersion: 1, entries: [{ objectId: 'SecondBox', subElement: null }, { objectId: 'SecondBox', subElement: 'Face1' }] } const fixture = (): DocumentSnapshot => ({ id: 'property-linksublistglobal-facade', label: 'PropertyLinkSubListGlobal Facade', version: 1, dirty: false, readOnly: false, units: 'mm', tree: [{ id: 'ShapeBinderProbe', label: 'Shape binder probe', type: 'feature', state: 'valid' }, { id: 'SourceBox', label: 'Source box', type: 'feature', state: 'valid' }, { id: 'SecondBox', label: 'Second box', type: 'feature', state: 'valid' }], objects: [ { id: 'ShapeBinderProbe', typeId: 'PartDesign::ShapeBinder', properties: [{ name: 'Support', label: 'Support', group: '', scope: 'data', type: 'App::PropertyLinkSubListGlobal', value: initialValue, recompute: true }] }, { id: 'SourceBox', typeId: 'Part::Feature', properties: [] }, { id: 'SecondBox', typeId: 'Part::Feature', properties: [] }, ], dependencies: [], recompute: { generation: 0, status: 'idle', objectStates: { ShapeBinderProbe: 'up-to-date', SourceBox: 'up-to-date', SecondBox: 'up-to-date' }, dirtyObjects: [], order: [], errors: [] }, }) const valueOf = (facade: ReturnType) => facade.app.document.getObject('ShapeBinderProbe')?.properties.find(({ name }) => name === 'Support')?.value as LinkSubListValue test('App::PropertyLinkSubListGlobal preserves ordered duplicate references and real dependencies', async () => { const facade = createWebCadFacade({ initialDocument: fixture(), runtimeMode: 'mock' }) facade.app.document.setProperty({ objectId: 'ShapeBinderProbe', propertyName: 'Support', value: editedValue }) assert.deepEqual(valueOf(facade), editedValue) assert.deepEqual(facade.app.document.getDependencies().map(({ targetId, relation, propertyName, reference }) => ({ targetId, relation, propertyName, reference })), [ { targetId: 'SecondBox', relation: 'link', propertyName: 'Support', reference: 'Support[0]' }, { targetId: 'SecondBox', relation: 'link', propertyName: 'Support', reference: 'Support[1]' }, ]) assert.equal(facade.app.document.getActive().recompute?.objectStates.ShapeBinderProbe, 'touched') assert.throws(() => facade.app.document.setProperty({ objectId: 'ShapeBinderProbe', propertyName: 'Support', value: { schemaVersion: 1, entries: [{ objectId: 'Missing', subElement: null }] } }), /target does not exist/) assert.deepEqual(valueOf(facade), editedValue) const archive = facade.project.fcstd.serializeMetadata(facade.app.document.getActive()) const xml = new TextDecoder().decode(unzipSync(archive)['Document.xml']) assert.match(xml, /<\/LinkSubList><\/Property>/) const summary = facade.project.fcstd.inspect(archive).objects.find(({ name }) => name === 'ShapeBinderProbe')?.properties.find(({ name }) => name === 'Support') assert.deepEqual({ typeId: summary?.typeId, element: summary?.element, linkSubs: summary?.linkSubs }, { typeId: 'App::PropertyLinkSubListGlobal', element: 'LinkSubList', linkSubs: [{ objectId: 'SecondBox', subElement: '' }, { objectId: 'SecondBox', subElement: 'Face1' }] }) assert.deepEqual(decodeFcstdPropertyValue(summary!), { value: editedValue, decoded: true }) const rewritten = facade.project.fcstd.rewriteLinkSubListGlobal(archive, { objectName: 'ShapeBinderProbe', propertyName: 'Support', value: initialValue, expectedValue: editedValue }) const rewrittenSummary = facade.project.fcstd.inspect(rewritten).objects.find(({ name }) => name === 'ShapeBinderProbe')?.properties.find(({ name }) => name === 'Support') assert.deepEqual(decodeFcstdPropertyValue(rewrittenSummary!), { value: initialValue, decoded: true }) await facade.project.dispose() })