feat: close link sub property codecs
Some checks are pending
real-verification / chrome (push) Waiting to run
real-verification / freecad-oracle (push) Waiting to run
real-verification / wasm (push) Waiting to run

This commit is contained in:
2026-08-17 21:37:33 -04:00
parent 7bd7ff5055
commit 54649da16c
69 changed files with 6383 additions and 76 deletions

View File

@@ -5848,7 +5848,7 @@ test('Datum and ShapeBinder stay in the active Body without stealing its Tip and
facade.task.apply()
const binder = facade.app.document.getObject('shape-binder')
assert.equal(binder?.typeId, 'PartDesign::ShapeBinder')
assert.deepEqual(binder?.properties.find((property) => property.name === 'Support')?.value, { schemaVersion: 1, objectId: 'pad', subElements: [] })
assert.deepEqual(binder?.properties.find((property) => property.name === 'Support')?.value, { schemaVersion: 1, entries: [{ objectId: 'pad', subElement: null }] })
assert.equal(facade.app.document.getActive().tree.find((item) => item.id === 'body001')?.children?.includes('shape-binder'), true)
assert.equal(facade.app.document.getObject('body001')?.properties.find((property) => property.name === 'Tip')?.value, null)
assert.deepEqual(facade.app.document.getDependencies().filter((edge) => edge.sourceId === 'shape-binder').map((edge) => edge.targetId), ['pad'])

View File

@@ -0,0 +1,53 @@
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 { encodeFreecadPropertyStatus } from '../src/facade/propertyStatus'
import type { DocumentSnapshot, LinkSubValue } from '../src/facade/types'
const hiddenStatus = encodeFreecadPropertyStatus(['PropHidden'])
const initialValue: LinkSubValue = { schemaVersion: 1, objectId: 'ColorSourceA', subElements: ['Face1', 'Face3'] }
const editedValue: LinkSubValue = { schemaVersion: 1, objectId: 'ColorSourceB', subElements: ['Face5'] }
const fixture = (): DocumentSnapshot => ({
id: 'property-linksubhidden-facade', label: 'PropertyLinkSubHidden Facade', version: 1, dirty: false, readOnly: false, units: 'mm',
tree: [
{ id: 'LinkProbe', label: 'Link probe', type: 'feature', state: 'valid' },
{ id: 'ColorSourceA', label: 'Color source A', type: 'feature', state: 'valid' },
{ id: 'ColorSourceB', label: 'Color source B', type: 'feature', state: 'valid' },
],
objects: [
{ id: 'LinkProbe', typeId: 'App::Link', properties: [{ name: 'ColoredElements', label: 'Colored elements', group: ' Link', scope: 'data', type: 'App::PropertyLinkSubHidden', value: initialValue, nativeStatus: hiddenStatus, recompute: true }] },
{ id: 'ColorSourceA', typeId: 'Part::Feature', properties: [] },
{ id: 'ColorSourceB', typeId: 'Part::Feature', properties: [] },
],
dependencies: [],
recompute: { generation: 0, status: 'idle', objectStates: { LinkProbe: 'up-to-date', ColorSourceA: 'up-to-date', ColorSourceB: 'up-to-date' }, dirtyObjects: [], order: [], errors: [] },
})
const valueOf = (facade: ReturnType<typeof createWebCadFacade>) => facade.app.document.getObject('LinkProbe')?.properties.find((property) => property.name === 'ColoredElements')?.value
test('App::PropertyLinkSubHidden preserves structured references without dependency edges', async () => {
const facade = createWebCadFacade({ initialDocument: fixture(), initialSelectedObjectIds: ['LinkProbe'], runtimeMode: 'mock' })
facade.app.document.setProperty({ objectId: 'LinkProbe', propertyName: 'ColoredElements', value: editedValue })
assert.deepEqual(valueOf(facade), editedValue)
assert.deepEqual(facade.app.document.getDependencies(), [])
assert.equal(facade.app.document.getActive().recompute?.objectStates.LinkProbe, 'touched')
assert.throws(() => facade.app.document.setProperty({ objectId: 'LinkProbe', propertyName: 'ColoredElements', value: { schemaVersion: 1, objectId: 'Missing', subElements: ['Face1'] } as LinkSubValue }), /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, new RegExp(`<Property name="ColoredElements" type="App::PropertyLinkSubHidden" status="${hiddenStatus}"><LinkSub value="ColorSourceB" count="1"><Sub value="Face5"/><\\/LinkSub><\\/Property>`))
assert.doesNotMatch(xml, /<Property name="ColoredElements"[^>]+(?:group|doc|attr|ro|hide)=/)
assert.doesNotMatch(xml, /<ObjectDeps Name="LinkProbe" Count="[1-9]/)
const summary = facade.project.fcstd.inspect(archive).objects.find(({ name }) => name === 'LinkProbe')?.properties.find(({ name }) => name === 'ColoredElements')
assert.deepEqual({ typeId: summary?.typeId, element: summary?.element, nativeStatus: summary?.nativeStatus, statusNames: summary?.statusNames }, { typeId: 'App::PropertyLinkSubHidden', element: 'LinkSub', nativeStatus: hiddenStatus, statusNames: ['PropHidden'] })
assert.deepEqual(decodeFcstdPropertyValue(summary!), { value: editedValue, decoded: true })
const rewritten = facade.project.fcstd.rewriteLinkSubHidden(archive, { objectName: 'LinkProbe', propertyName: 'ColoredElements', value: initialValue, expectedValue: editedValue })
const rewrittenSummary = facade.project.fcstd.inspect(rewritten).objects.find(({ name }) => name === 'LinkProbe')?.properties.find(({ name }) => name === 'ColoredElements')
assert.deepEqual(decodeFcstdPropertyValue(rewrittenSummary!), { value: initialValue, decoded: true })
await facade.project.dispose()
})

View File

@@ -0,0 +1,67 @@
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { createWebCadFacade } from '../src/facade/mockFacade'
import { encodeFreecadPropertyStatus } from '../src/facade/propertyStatus'
import type { DocumentSnapshot, LinkSubValue } from '../src/facade/types'
const initialValue: LinkSubValue = { schemaVersion: 1, objectId: 'ColorSourceA', subElements: ['Face1', 'Face3'] }
const editedValue: LinkSubValue = { schemaVersion: 1, objectId: 'ColorSourceB', subElements: ['Face5'] }
const fixture = (): DocumentSnapshot => ({
id: 'property-linksubhidden-transaction', label: 'PropertyLinkSubHidden Transaction', version: 1, dirty: false, readOnly: false, units: 'mm',
tree: [
{ id: 'LinkProbe', label: 'Link probe', type: 'feature', state: 'valid' },
{ id: 'ColorSourceA', label: 'Color source A', type: 'feature', state: 'valid' },
{ id: 'ColorSourceB', label: 'Color source B', type: 'feature', state: 'valid' },
],
objects: [
{ id: 'LinkProbe', typeId: 'App::Link', properties: [{ name: 'ColoredElements', label: 'Colored elements', group: ' Link', scope: 'data', type: 'App::PropertyLinkSubHidden', value: initialValue, nativeStatus: encodeFreecadPropertyStatus(['PropHidden']), recompute: true }] },
{ id: 'ColorSourceA', typeId: 'Part::Feature', properties: [] },
{ id: 'ColorSourceB', typeId: 'Part::Feature', properties: [] },
],
dependencies: [],
recompute: { generation: 0, status: 'idle', objectStates: { LinkProbe: 'up-to-date', ColorSourceA: 'up-to-date', ColorSourceB: 'up-to-date' }, dirtyObjects: [], order: [], errors: [] },
})
const valueOf = (facade: ReturnType<typeof createWebCadFacade>) => facade.app.document.getObject('LinkProbe')?.properties.find((property) => property.name === 'ColoredElements')?.value as LinkSubValue
test('App::PropertyLinkSubHidden transaction closes undo, redo, stale, cancel, failure, hidden dependencies and resources', async () => {
const facade = createWebCadFacade({ initialDocument: fixture(), runtimeMode: 'mock' })
const resourcesBefore = facade.geometry.capabilities()
const requested: LinkSubValue = { schemaVersion: 1, objectId: 'ColorSourceB', subElements: ['Face5'] }
facade.app.document.setProperty({ objectId: 'LinkProbe', propertyName: 'ColoredElements', value: requested, expectedDocumentVersion: 1 })
requested.subElements[0] = 'Face1'
assert.deepEqual(valueOf(facade), editedValue)
const exposed = valueOf(facade)
exposed.subElements[0] = 'Face1'
assert.deepEqual(valueOf(facade), editedValue)
assert.equal(facade.app.document.getActive().version, 2)
assert.equal(facade.app.document.getActive().dirty, true)
assert.equal(facade.app.document.getActive().recompute?.objectStates.LinkProbe, 'touched')
assert.deepEqual(facade.app.document.getDependencies(), [])
facade.history.undo()
assert.deepEqual(valueOf(facade), initialValue)
assert.equal(facade.app.document.getActive().version, 1)
assert.equal(facade.history.canUndo(), false)
assert.equal(facade.history.canRedo(), true)
facade.history.redo()
assert.deepEqual(valueOf(facade), editedValue)
assert.equal(facade.app.document.getActive().version, 2)
assert.equal(facade.history.canUndo(), true)
assert.equal(facade.history.canRedo(), false)
const stable = JSON.stringify(facade.app.document.getActive())
assert.throws(() => facade.app.document.setProperty({ objectId: 'LinkProbe', propertyName: 'ColoredElements', value: editedValue, expectedDocumentVersion: 1 }), /Stale document version: expected 1, current 2/)
const cancelled = new AbortController()
cancelled.abort()
assert.throws(() => facade.app.document.setProperty({ objectId: 'LinkProbe', propertyName: 'ColoredElements', value: initialValue, expectedDocumentVersion: 2, signal: cancelled.signal }), (error: unknown) => error instanceof DOMException && error.name === 'AbortError')
assert.throws(() => facade.app.document.setProperty({ objectId: 'LinkProbe', propertyName: 'ColoredElements', value: { schemaVersion: 1, objectId: 'Missing', subElements: ['Face1'] } as LinkSubValue }), /target does not exist/)
assert.throws(() => facade.app.document.setProperty({ objectId: 'LinkProbe', propertyName: 'ColoredElements', value: { schemaVersion: 1, objectId: 'ColorSourceA', subElements: [1] } as unknown as LinkSubValue }), /TopoRef must be a JSON object/)
assert.equal(JSON.stringify(facade.app.document.getActive()), stable)
assert.equal(facade.history.canUndo(), true)
assert.equal(facade.history.canRedo(), false)
const resourcesAfter = facade.geometry.capabilities()
assert.deepEqual({ shapeCount: resourcesAfter.shapeCount, kernelReferenceCount: resourcesAfter.kernelReferenceCount, releasedShapeCount: resourcesAfter.releasedShapeCount }, { shapeCount: resourcesBefore.shapeCount, kernelReferenceCount: resourcesBefore.kernelReferenceCount, releasedShapeCount: resourcesBefore.releasedShapeCount })
await facade.project.dispose()
})

View File

@@ -0,0 +1,45 @@
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<typeof createWebCadFacade>) => 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, /<Property name="Support" type="App::PropertyLinkSubListGlobal"><LinkSubList count="2"><Link obj="SecondBox" sub=""\/><Link obj="SecondBox" sub="Face1"\/><\/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()
})

View File

@@ -0,0 +1,31 @@
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { createWebCadFacade } from '../src/facade/mockFacade'
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-transaction', label: 'PropertyLinkSubListGlobal Transaction', 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<typeof createWebCadFacade>) => facade.app.document.getObject('ShapeBinderProbe')?.properties.find(({ name }) => name === 'Support')?.value as LinkSubListValue
test('App::PropertyLinkSubListGlobal closes undo, redo, stale, abort and resource ownership', async () => {
const facade = createWebCadFacade({ initialDocument: fixture(), runtimeMode: 'mock' })
const resourcesBefore = facade.geometry.capabilities()
facade.app.document.setProperty({ objectId: 'ShapeBinderProbe', propertyName: 'Support', value: editedValue, expectedDocumentVersion: 1 })
editedValue.entries[0].subElement = 'Face2'
assert.deepEqual(valueOf(facade), { schemaVersion: 1, entries: [{ objectId: 'SecondBox', subElement: null }, { objectId: 'SecondBox', subElement: 'Face1' }] })
facade.history.undo(); assert.deepEqual(valueOf(facade), initialValue); assert.equal(facade.history.canRedo(), true)
facade.history.redo(); assert.deepEqual(valueOf(facade), { schemaVersion: 1, entries: [{ objectId: 'SecondBox', subElement: null }, { objectId: 'SecondBox', subElement: 'Face1' }] })
const stable = JSON.stringify(facade.app.document.getActive())
assert.throws(() => facade.app.document.setProperty({ objectId: 'ShapeBinderProbe', propertyName: 'Support', value: initialValue, expectedDocumentVersion: 1 }), /Stale document version/)
const cancelled = new AbortController(); cancelled.abort(); assert.throws(() => facade.app.document.setProperty({ objectId: 'ShapeBinderProbe', propertyName: 'Support', value: initialValue, signal: cancelled.signal }), (error: unknown) => error instanceof DOMException && error.name === 'AbortError')
assert.throws(() => facade.app.document.setProperty({ objectId: 'ShapeBinderProbe', propertyName: 'Support', value: { schemaVersion: 1, entries: [{ objectId: 'Missing', subElement: null }] } }), /target does not exist/)
assert.equal(JSON.stringify(facade.app.document.getActive()), stable)
const resourcesAfter = facade.geometry.capabilities(); assert.deepEqual({ shapeCount: resourcesAfter.shapeCount, kernelReferenceCount: resourcesAfter.kernelReferenceCount, releasedShapeCount: resourcesAfter.releasedShapeCount }, { shapeCount: resourcesBefore.shapeCount, kernelReferenceCount: resourcesBefore.kernelReferenceCount, releasedShapeCount: resourcesBefore.releasedShapeCount })
await facade.project.dispose()
})