Files
Web_FreeCAD_Bitbybit/tests/propertyLinkSubHiddenFacade.test.ts
wangdequan 54649da16c
Some checks failed
real-verification / chrome (push) Has been cancelled
real-verification / freecad-oracle (push) Has been cancelled
real-verification / wasm (push) Has been cancelled
feat: close link sub property codecs
2026-08-17 21:37:33 -04:00

54 lines
4.2 KiB
TypeScript

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()
})