Files
Web_FreeCAD_Bitbybit/tests/propertyLinkSubListGlobalTransaction.test.ts
wangdequan 54649da16c
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
feat: close link sub property codecs
2026-08-17 21:37:33 -04:00

32 lines
3.9 KiB
TypeScript

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