Files
Web_FreeCAD_Bitbybit/tests/propertyLinkSubHiddenTransaction.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

68 lines
4.8 KiB
TypeScript

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