feat: promote font force heat flux and integer set properties
This commit is contained in:
80
tests/propertyFontFacade.test.ts
Normal file
80
tests/propertyFontFacade.test.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
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, FacadeEvent, ObjectPropertySnapshot } from '../src/facade/types'
|
||||
|
||||
const hosts = [
|
||||
{ id: 'FontAnnotation', typeId: 'TechDraw::DrawViewAnnotation', group: 'Annotation' },
|
||||
{ id: 'FontSpreadsheet', typeId: 'TechDraw::DrawViewSpreadsheet', group: 'Spreadsheet' },
|
||||
] as const
|
||||
|
||||
const property = (group: string): ObjectPropertySnapshot => ({ name: 'Font', label: 'Font', group, scope: 'data', type: 'App::PropertyFont', value: 'osifont', recompute: true })
|
||||
const fixture = (): DocumentSnapshot => ({
|
||||
id: 'property-font-facade',
|
||||
label: 'PropertyFont Facade',
|
||||
version: 1,
|
||||
dirty: false,
|
||||
readOnly: false,
|
||||
units: 'mm',
|
||||
tree: hosts.map(({ id }) => ({ id, label: id, type: 'feature', state: 'valid' })),
|
||||
objects: hosts.map(({ id, typeId, group }) => ({ id, typeId, properties: [property(group)] })),
|
||||
dependencies: [],
|
||||
recompute: { generation: 0, status: 'idle', objectStates: Object.fromEntries(hosts.map(({ id }) => [id, 'up-to-date'])), dirtyObjects: [], order: [], errors: [] },
|
||||
})
|
||||
|
||||
test('App::PropertyFont preserves arbitrary UTF-8 family names through the Facade', () => {
|
||||
const facade = createWebCadFacade({ initialDocument: fixture(), initialSelectedObjectIds: ['FontAnnotation'], runtimeMode: 'mock' })
|
||||
const events: FacadeEvent[] = []
|
||||
facade.subscribe((event) => {
|
||||
if (event.type === 'property.before-change' || event.type === 'property.changed' || event.type === 'transaction.committed') events.push(event)
|
||||
})
|
||||
facade.app.document.setProperty({ objectId: 'FontAnnotation', propertyName: 'Font', value: 'Noto Sans CJK SC' })
|
||||
assert.equal(facade.app.document.getObject('FontAnnotation')?.properties[0].value, 'Noto Sans CJK SC')
|
||||
assert.equal(facade.app.document.getActive().version, 2)
|
||||
assert.equal(facade.app.document.getActive().dirty, true)
|
||||
assert.equal(facade.app.document.getActive().recompute?.objectStates.FontAnnotation, 'touched')
|
||||
assert.deepEqual(events.map(({ type }) => type), ['property.before-change', 'property.changed', 'transaction.committed'])
|
||||
|
||||
facade.app.document.setProperty({ objectId: 'FontAnnotation', propertyName: 'Font', value: '' })
|
||||
assert.equal(facade.app.document.getObject('FontAnnotation')?.properties[0].value, '')
|
||||
const stable = JSON.stringify(facade.app.document.getActive())
|
||||
const eventCount = events.length
|
||||
assert.throws(() => facade.app.document.setProperty({ objectId: 'FontAnnotation', propertyName: 'Font', value: 42 as unknown as string }), /requires a string value/)
|
||||
assert.throws(() => facade.app.document.setProperty({ objectId: 'FontAnnotation', propertyName: 'Font', value: `Bad\0Font` }), /unsupported control characters/)
|
||||
assert.throws(() => facade.app.document.setProperty({ objectId: 'FontAnnotation', propertyName: 'Font', value: 'a'.repeat(1025) }), /font family name is too long/)
|
||||
assert.equal(JSON.stringify(facade.app.document.getActive()), stable)
|
||||
assert.equal(events.length, eventCount)
|
||||
})
|
||||
|
||||
test('App::PropertyFont keeps its TypeId around native String FCStd payloads', async () => {
|
||||
const document = fixture()
|
||||
document.objects[0].properties[0].value = 'DejaVu Sans'
|
||||
document.objects[1].properties[0].value = 'Liberation Serif'
|
||||
const facade = createWebCadFacade({ initialDocument: document, runtimeMode: 'mock' })
|
||||
const archive = facade.project.fcstd.serializeMetadata(document)
|
||||
const documentXml = new TextDecoder().decode(unzipSync(archive)['Document.xml'])
|
||||
assert.match(documentXml, /<Property name="Font" type="App::PropertyFont"><String value="DejaVu Sans"\/><\/Property>/)
|
||||
assert.match(documentXml, /<Property name="Font" type="App::PropertyFont"><String value="Liberation Serif"\/><\/Property>/)
|
||||
assert.doesNotMatch(documentXml, /<Property name="Font"[^>]+(?:group|doc|attr|ro|hide)=/)
|
||||
|
||||
const inspection = facade.project.fcstd.inspect(archive)
|
||||
for (const [index, host] of hosts.entries()) {
|
||||
const object = inspection.objects.find(({ name }) => name === host.id)
|
||||
assert.equal(object?.support, 'recognized')
|
||||
const summary = object?.properties.find(({ name }) => name === 'Font')
|
||||
assert.deepEqual({ typeId: summary?.typeId, element: summary?.element }, { typeId: 'App::PropertyFont', element: 'String' })
|
||||
assert.deepEqual(decodeFcstdPropertyValue(summary!), { value: document.objects[index].properties[0].value, decoded: true })
|
||||
}
|
||||
assert.equal(decodeFcstdPropertyValue({ name: 'Font', typeId: 'App::PropertyFont', element: 'PropertyFont', value: 'osifont' }).decoded, false)
|
||||
|
||||
const rewritten = facade.project.fcstd.rewriteFont(archive, { objectName: 'FontAnnotation', propertyName: 'Font', value: 'Noto Sans', expectedValue: 'DejaVu Sans' })
|
||||
const rewrittenProperty = facade.project.fcstd.inspect(rewritten).objects.find(({ name }) => name === 'FontAnnotation')?.properties.find(({ name }) => name === 'Font')
|
||||
assert.deepEqual(decodeFcstdPropertyValue(rewrittenProperty!), { value: 'Noto Sans', decoded: true })
|
||||
assert.match(new TextDecoder().decode(unzipSync(rewritten)['Document.xml']), /<Object(?=[^>]*name="FontAnnotation")(?=[^>]*Touched="1")[^>]*\/>/)
|
||||
assert.throws(() => facade.project.fcstd.rewriteFont(archive, { objectName: 'FontAnnotation', propertyName: 'Font', value: 'Noto Sans', expectedValue: 'Wrong' }), /does not match expectedValue/)
|
||||
assert.throws(() => facade.project.fcstd.rewriteFont(archive, { objectName: 'FontAnnotation', propertyName: 'Font', value: `Bad\0Font` }), /unsupported control characters/)
|
||||
assert.throws(() => facade.project.fcstd.rewriteFont(archive, { objectName: 'FontAnnotation', propertyName: 'Missing', value: 'Noto Sans' }), /property does not exist/)
|
||||
await facade.project.dispose()
|
||||
})
|
||||
74
tests/propertyFontTransaction.test.ts
Normal file
74
tests/propertyFontTransaction.test.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
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, FacadeEvent } from '../src/facade/types'
|
||||
|
||||
const fixture = (): DocumentSnapshot => ({
|
||||
id: 'property-font-transaction',
|
||||
label: 'PropertyFont Transaction',
|
||||
version: 1,
|
||||
dirty: false,
|
||||
readOnly: false,
|
||||
units: 'mm',
|
||||
tree: [
|
||||
{ id: 'FontProbe', label: 'Font probe', type: 'feature', state: 'valid' },
|
||||
{ id: 'ImmutableFontProbe', label: 'Immutable font probe', type: 'feature', state: 'valid' },
|
||||
],
|
||||
objects: [
|
||||
{ id: 'FontProbe', typeId: 'TechDraw::DrawViewAnnotation', properties: [{ name: 'Font', label: 'Font', group: 'Annotation', scope: 'data', type: 'App::PropertyFont', value: 'osifont', recompute: true }] },
|
||||
{ id: 'ImmutableFontProbe', typeId: 'TechDraw::DrawViewAnnotation', properties: [{ name: 'Font', label: 'Immutable font', group: 'Annotation', scope: 'data', type: 'App::PropertyFont', value: 'osifont', nativeStatus: encodeFreecadPropertyStatus(['Immutable']), readOnly: true, recompute: true }] },
|
||||
],
|
||||
dependencies: [],
|
||||
recompute: { generation: 0, status: 'idle', objectStates: { FontProbe: 'up-to-date', ImmutableFontProbe: 'up-to-date' }, dirtyObjects: [], order: [], errors: [] },
|
||||
})
|
||||
|
||||
const valueOf = (facade: ReturnType<typeof createWebCadFacade>, objectId = 'FontProbe') => facade.app.document.getObject(objectId)?.properties.find(({ name }) => name === 'Font')?.value
|
||||
|
||||
test('App::PropertyFont transaction closes undo, redo, stale, cancel, failure and resource ownership', async () => {
|
||||
const facade = createWebCadFacade({ initialDocument: fixture(), runtimeMode: 'mock' })
|
||||
const events: FacadeEvent[] = []
|
||||
facade.subscribe((event) => {
|
||||
if (event.type === 'property.before-change' || event.type === 'property.changed' || event.type === 'transaction.committed') events.push(event)
|
||||
})
|
||||
const resourcesBefore = facade.geometry.capabilities()
|
||||
facade.app.document.setProperty({ objectId: 'FontProbe', propertyName: 'Font', value: 'DejaVu Sans', expectedDocumentVersion: 1 })
|
||||
assert.equal(valueOf(facade), 'DejaVu Sans')
|
||||
assert.equal(facade.app.document.getActive().version, 2)
|
||||
assert.equal(facade.app.document.getActive().dirty, true)
|
||||
assert.equal(facade.app.document.getActive().recompute?.objectStates.FontProbe, 'touched')
|
||||
assert.deepEqual(events.map(({ type }) => type), ['property.before-change', 'property.changed', 'transaction.committed'])
|
||||
assert.equal(new Set(events.map((event) => 'transactionId' in event ? event.transactionId : '')).size, 1)
|
||||
|
||||
facade.history.undo()
|
||||
assert.equal(valueOf(facade), 'osifont')
|
||||
assert.equal(facade.app.document.getActive().version, 1)
|
||||
assert.equal(facade.history.canUndo(), false)
|
||||
assert.equal(facade.history.canRedo(), true)
|
||||
facade.history.redo()
|
||||
assert.equal(valueOf(facade), 'DejaVu Sans')
|
||||
assert.equal(facade.app.document.getActive().version, 2)
|
||||
assert.equal(facade.history.canUndo(), true)
|
||||
assert.equal(facade.history.canRedo(), false)
|
||||
|
||||
const stableState = JSON.stringify(facade.app.document.getActive())
|
||||
const stableEvents = events.length
|
||||
assert.throws(() => facade.app.document.setProperty({ objectId: 'FontProbe', propertyName: 'Font', value: 'Stale Font', expectedDocumentVersion: 1 }), /Stale document version: expected 1, current 2/)
|
||||
const cancelled = new AbortController()
|
||||
cancelled.abort()
|
||||
assert.throws(() => facade.app.document.setProperty({ objectId: 'FontProbe', propertyName: 'Font', value: 'Cancelled Font', expectedDocumentVersion: 2, signal: cancelled.signal }), (error: unknown) => error instanceof DOMException && error.name === 'AbortError')
|
||||
assert.throws(() => facade.app.document.setProperty({ objectId: 'FontProbe', propertyName: 'Font', value: 42 as unknown as string }), /requires a string value/)
|
||||
assert.throws(() => facade.app.document.setProperty({ objectId: 'FontProbe', propertyName: 'Font', value: `Bad\0Font` }), /unsupported control characters/)
|
||||
assert.throws(() => facade.app.document.setProperty({ objectId: 'ImmutableFontProbe', propertyName: 'Font', value: 'Unlocked Font' }), /Immutable font is read-only/)
|
||||
assert.equal(JSON.stringify(facade.app.document.getActive()), stableState)
|
||||
assert.equal(events.length, stableEvents)
|
||||
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()
|
||||
})
|
||||
61
tests/propertyForceFacade.test.ts
Normal file
61
tests/propertyForceFacade.test.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
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 { convertQuantity, quantityFromUnit } from '../src/facade/units'
|
||||
import type { DocumentSnapshot } from '../src/facade/types'
|
||||
|
||||
const fixture = (): DocumentSnapshot => ({
|
||||
id: 'property-force-facade',
|
||||
label: 'PropertyForce Facade',
|
||||
version: 1,
|
||||
dirty: false,
|
||||
readOnly: false,
|
||||
units: 'mm',
|
||||
tree: [
|
||||
{ id: 'ForceProbe', label: 'Force probe', type: 'feature', state: 'valid' },
|
||||
{ id: 'RigidForceProbe', label: 'Rigid force probe', type: 'feature', state: 'valid' },
|
||||
],
|
||||
objects: [
|
||||
{ id: 'ForceProbe', typeId: 'Fem::ConstraintForce', properties: [{ name: 'Force', label: 'Force', group: '', scope: 'data', type: 'App::PropertyForce', value: 0, unit: 'mm*kg/s^2', recompute: true }] },
|
||||
{ id: 'RigidForceProbe', typeId: 'Fem::ConstraintRigidBody', properties: [{ name: 'ForceX', label: 'Force X', group: 'ConstraintRigidBody', scope: 'data', type: 'App::PropertyForce', value: 0, unit: 'mm*kg/s^2', nativeStatus: encodeFreecadPropertyStatus(['PropOutput']), recompute: true }] },
|
||||
],
|
||||
dependencies: [],
|
||||
recompute: { generation: 0, status: 'idle', objectStates: { ForceProbe: 'up-to-date', RigidForceProbe: 'up-to-date' }, dirtyObjects: [], order: [], errors: [] },
|
||||
})
|
||||
|
||||
test('App::PropertyForce uses the native internal unit and Float FCStd codec', () => {
|
||||
const facade = createWebCadFacade({ initialDocument: fixture(), initialSelectedObjectIds: ['ForceProbe'], runtimeMode: 'mock' })
|
||||
facade.app.document.setProperty({ objectId: 'ForceProbe', propertyName: 'Force', value: 500000 })
|
||||
const edited = facade.app.document.getActive()
|
||||
const property = edited.objects[0].properties[0]
|
||||
assert.deepEqual({ type: property.type, value: property.value, unit: property.unit }, { type: 'App::PropertyForce', value: 500000, unit: 'mm*kg/s^2' })
|
||||
assert.equal(edited.version, 2)
|
||||
assert.equal(edited.dirty, true)
|
||||
assert.equal(edited.recompute?.objectStates.ForceProbe, 'touched')
|
||||
assert.throws(() => facade.app.document.setProperty({ objectId: 'ForceProbe', propertyName: 'Force', value: '500 N' as unknown as number }), /finite numeric value/)
|
||||
assert.throws(() => facade.app.document.setProperty({ objectId: 'ForceProbe', propertyName: 'Force', value: Number.POSITIVE_INFINITY }), /finite numeric value/)
|
||||
facade.app.document.setProperty({ objectId: 'RigidForceProbe', propertyName: 'ForceX', value: 1000 })
|
||||
assert.equal(facade.app.document.getObject('RigidForceProbe')?.properties[0].value, 1000)
|
||||
assert.equal(facade.app.document.getActive().recompute?.objectStates.RigidForceProbe, 'up-to-date')
|
||||
|
||||
const archive = facade.project.fcstd.serializeMetadata(edited)
|
||||
const documentXml = new TextDecoder().decode(unzipSync(archive)['Document.xml'])
|
||||
assert.match(documentXml, /<Property name="Force" type="App::PropertyForce"><Float value="500000"\/><\/Property>/)
|
||||
assert.doesNotMatch(documentXml, /<Property name="Force"[^>]+group=/)
|
||||
const summary = facade.project.fcstd.inspect(archive).objects.find(({ name }) => name === 'ForceProbe')?.properties.find(({ name }) => name === 'Force')
|
||||
assert.deepEqual({ typeId: summary?.typeId, element: summary?.element }, { typeId: 'App::PropertyForce', element: 'Float' })
|
||||
assert.deepEqual(decodeFcstdPropertyValue(summary!), { value: 500000, decoded: true })
|
||||
|
||||
const rewritten = facade.project.fcstd.rewriteNumeric(archive, { objectName: 'ForceProbe', propertyName: 'Force', typeId: 'App::PropertyForce', value: -5000, expectedValue: 500000 })
|
||||
const rewrittenSummary = facade.project.fcstd.inspect(rewritten).objects.find(({ name }) => name === 'ForceProbe')?.properties.find(({ name }) => name === 'Force')
|
||||
assert.deepEqual(decodeFcstdPropertyValue(rewrittenSummary!), { value: -5000, decoded: true })
|
||||
assert.throws(() => facade.project.fcstd.rewriteNumeric(archive, { objectName: 'ForceProbe', propertyName: 'Force', typeId: 'App::PropertyForce', value: 1000, expectedValue: 1 }), /expected 1, found 500000/)
|
||||
assert.equal(convertQuantity(quantityFromUnit(2, 'kN'), 'N'), 2000)
|
||||
assert.equal(convertQuantity(quantityFromUnit(1, 'N'), 'mm*kg/s^2'), 1000)
|
||||
facade.app.document.setExpression({ objectId: 'ForceProbe', propertyName: 'Force', expression: '2 N' })
|
||||
assert.equal(facade.app.document.getObject('ForceProbe')?.properties[0].value, 2000)
|
||||
assert.equal(facade.app.document.getObject('ForceProbe')?.properties[0].expression, '2 N')
|
||||
})
|
||||
52
tests/propertyForceTransaction.test.ts
Normal file
52
tests/propertyForceTransaction.test.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
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, FacadeEvent } from '../src/facade/types'
|
||||
|
||||
const fixture = (): DocumentSnapshot => ({
|
||||
id: 'property-force-transaction', label: 'PropertyForce Transaction', version: 1, dirty: false, readOnly: false, units: 'mm',
|
||||
tree: [{ id: 'ForceProbe', label: 'Force probe', type: 'feature', state: 'valid' }, { id: 'OutputForceProbe', label: 'Output force probe', type: 'feature', state: 'valid' }],
|
||||
objects: [
|
||||
{ id: 'ForceProbe', typeId: 'Fem::ConstraintForce', properties: [{ name: 'Force', label: 'Force', group: '', scope: 'data', type: 'App::PropertyForce', value: 0, unit: 'mm*kg/s^2', recompute: true }] },
|
||||
{ id: 'OutputForceProbe', typeId: 'Fem::ConstraintRigidBody', properties: [{ name: 'ForceX', label: 'Force X', group: 'ConstraintRigidBody', scope: 'data', type: 'App::PropertyForce', value: 0, unit: 'mm*kg/s^2', nativeStatus: encodeFreecadPropertyStatus(['PropOutput']), recompute: true }] },
|
||||
],
|
||||
dependencies: [],
|
||||
recompute: { generation: 0, status: 'idle', objectStates: { ForceProbe: 'up-to-date', OutputForceProbe: 'up-to-date' }, dirtyObjects: [], order: [], errors: [] },
|
||||
})
|
||||
|
||||
const force = (facade: ReturnType<typeof createWebCadFacade>) => facade.app.document.getObject('ForceProbe')?.properties[0].value
|
||||
|
||||
test('App::PropertyForce transaction closes undo, redo, abort, stale, failure and resource ownership', async () => {
|
||||
const facade = createWebCadFacade({ initialDocument: fixture(), runtimeMode: 'mock' })
|
||||
const events: FacadeEvent[] = []
|
||||
facade.subscribe((event) => { if (event.type === 'property.before-change' || event.type === 'property.changed' || event.type === 'transaction.committed') events.push(event) })
|
||||
const resourcesBefore = facade.geometry.capabilities()
|
||||
facade.app.document.setProperty({ objectId: 'ForceProbe', propertyName: 'Force', value: 500000, expectedDocumentVersion: 1 })
|
||||
assert.equal(force(facade), 500000)
|
||||
assert.equal(facade.app.document.getActive().version, 2)
|
||||
assert.deepEqual(events.map(({ type }) => type), ['property.before-change', 'property.changed', 'transaction.committed'])
|
||||
assert.equal(new Set(events.map((event) => 'transactionId' in event ? event.transactionId : '')).size, 1)
|
||||
facade.history.undo()
|
||||
assert.equal(force(facade), 0)
|
||||
assert.equal(facade.history.canRedo(), true)
|
||||
facade.history.redo()
|
||||
assert.equal(force(facade), 500000)
|
||||
assert.equal(facade.history.canUndo(), true)
|
||||
|
||||
facade.app.document.setProperty({ objectId: 'OutputForceProbe', propertyName: 'ForceX', value: 1000, expectedDocumentVersion: 2 })
|
||||
assert.equal(facade.app.document.getObject('OutputForceProbe')?.properties[0].value, 1000)
|
||||
assert.equal(facade.app.document.getActive().recompute?.objectStates.OutputForceProbe, 'up-to-date')
|
||||
|
||||
const stable = JSON.stringify(facade.app.document.getActive())
|
||||
const eventCount = events.length
|
||||
assert.throws(() => facade.app.document.setProperty({ objectId: 'ForceProbe', propertyName: 'Force', value: 250000, expectedDocumentVersion: 1 }), /Stale document version/)
|
||||
const cancelled = new AbortController(); cancelled.abort()
|
||||
assert.throws(() => facade.app.document.setProperty({ objectId: 'ForceProbe', propertyName: 'Force', value: 250000, signal: cancelled.signal }), (error: unknown) => error instanceof DOMException && error.name === 'AbortError')
|
||||
assert.throws(() => facade.app.document.setProperty({ objectId: 'ForceProbe', propertyName: 'Force', value: 'invalid' as unknown as number }), /finite numeric value/)
|
||||
assert.equal(JSON.stringify(facade.app.document.getActive()), stable)
|
||||
assert.equal(events.length, eventCount)
|
||||
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()
|
||||
})
|
||||
47
tests/propertyHeatFluxFacade.test.ts
Normal file
47
tests/propertyHeatFluxFacade.test.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
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 { convertQuantity, quantityFromUnit } from '../src/facade/units'
|
||||
import type { DocumentSnapshot } from '../src/facade/types'
|
||||
|
||||
const fixture = (): DocumentSnapshot => ({
|
||||
id: 'property-heatflux-facade',
|
||||
label: 'PropertyHeatFlux Facade',
|
||||
version: 1,
|
||||
dirty: false,
|
||||
readOnly: false,
|
||||
units: 'mm',
|
||||
tree: [{ id: 'HeatFluxProbe', label: 'Heat flux probe', type: 'feature', state: 'valid' }],
|
||||
objects: [{ id: 'HeatFluxProbe', typeId: 'Fem::ConstraintHeatflux', properties: [{ name: 'DFlux', label: 'DFlux', group: 'ConstraintHeatflux', scope: 'data', type: 'App::PropertyHeatFlux', value: 0, unit: 'kg/s^3', recompute: true }] }],
|
||||
dependencies: [],
|
||||
recompute: { generation: 0, status: 'idle', objectStates: { HeatFluxProbe: 'up-to-date' }, dirtyObjects: [], order: [], errors: [] },
|
||||
})
|
||||
|
||||
test('App::PropertyHeatFlux uses the native internal unit and Float FCStd codec', () => {
|
||||
const facade = createWebCadFacade({ initialDocument: fixture(), initialSelectedObjectIds: ['HeatFluxProbe'], runtimeMode: 'mock' })
|
||||
facade.app.document.setProperty({ objectId: 'HeatFluxProbe', propertyName: 'DFlux', value: 500 })
|
||||
const edited = facade.app.document.getActive()
|
||||
const property = edited.objects[0].properties[0]
|
||||
assert.deepEqual({ type: property.type, value: property.value, unit: property.unit }, { type: 'App::PropertyHeatFlux', value: 500, unit: 'kg/s^3' })
|
||||
assert.equal(edited.version, 2)
|
||||
assert.equal(edited.dirty, true)
|
||||
assert.equal(edited.recompute?.objectStates.HeatFluxProbe, 'touched')
|
||||
assert.throws(() => facade.app.document.setProperty({ objectId: 'HeatFluxProbe', propertyName: 'DFlux', value: '500 W/m^2' as unknown as number }), /finite numeric value/)
|
||||
assert.throws(() => facade.app.document.setProperty({ objectId: 'HeatFluxProbe', propertyName: 'DFlux', value: Number.POSITIVE_INFINITY }), /finite numeric value/)
|
||||
|
||||
const archive = facade.project.fcstd.serializeMetadata(edited)
|
||||
const documentXml = new TextDecoder().decode(unzipSync(archive)['Document.xml'])
|
||||
assert.match(documentXml, /<Property name="DFlux" type="App::PropertyHeatFlux" group="ConstraintHeatflux"[^>]*><Float value="500"\/><\/Property>/)
|
||||
const summary = facade.project.fcstd.inspect(archive).objects.find(({ name }) => name === 'HeatFluxProbe')?.properties.find(({ name }) => name === 'DFlux')
|
||||
assert.deepEqual({ typeId: summary?.typeId, element: summary?.element }, { typeId: 'App::PropertyHeatFlux', element: 'Float' })
|
||||
assert.deepEqual(decodeFcstdPropertyValue(summary!), { value: 500, decoded: true })
|
||||
|
||||
const rewritten = facade.project.fcstd.rewriteNumeric(archive, { objectName: 'HeatFluxProbe', propertyName: 'DFlux', typeId: 'App::PropertyHeatFlux', value: -5, expectedValue: 500 })
|
||||
const rewrittenSummary = facade.project.fcstd.inspect(rewritten).objects.find(({ name }) => name === 'HeatFluxProbe')?.properties.find(({ name }) => name === 'DFlux')
|
||||
assert.deepEqual(decodeFcstdPropertyValue(rewrittenSummary!), { value: -5, decoded: true })
|
||||
assert.throws(() => facade.project.fcstd.rewriteNumeric(archive, { objectName: 'HeatFluxProbe', propertyName: 'DFlux', typeId: 'App::PropertyHeatFlux', value: 1000, expectedValue: 1 }), /expected 1, found 500/)
|
||||
assert.equal(convertQuantity(quantityFromUnit(2, 'kW/m^2'), 'W/m^2'), 2000)
|
||||
assert.equal(convertQuantity(quantityFromUnit(1, 'W/mm^2'), 'kg/s^3'), 1000000)
|
||||
})
|
||||
44
tests/propertyHeatFluxTransaction.test.ts
Normal file
44
tests/propertyHeatFluxTransaction.test.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { createWebCadFacade } from '../src/facade/mockFacade'
|
||||
import type { DocumentSnapshot, FacadeEvent } from '../src/facade/types'
|
||||
|
||||
const fixture = (): DocumentSnapshot => ({
|
||||
id: 'property-heatflux-transaction', label: 'PropertyHeatFlux Transaction', version: 1, dirty: false, readOnly: false, units: 'mm',
|
||||
tree: [{ id: 'HeatFluxProbe', label: 'Heat flux probe', type: 'feature', state: 'valid' }],
|
||||
objects: [{ id: 'HeatFluxProbe', typeId: 'Fem::ConstraintHeatflux', properties: [{ name: 'DFlux', label: 'DFlux', group: 'ConstraintHeatflux', scope: 'data', type: 'App::PropertyHeatFlux', value: 0, unit: 'kg/s^3', recompute: true }] }],
|
||||
dependencies: [],
|
||||
recompute: { generation: 0, status: 'idle', objectStates: { HeatFluxProbe: 'up-to-date' }, dirtyObjects: [], order: [], errors: [] },
|
||||
})
|
||||
|
||||
const heatFlux = (facade: ReturnType<typeof createWebCadFacade>) => facade.app.document.getObject('HeatFluxProbe')?.properties[0].value
|
||||
|
||||
test('App::PropertyHeatFlux transaction closes undo, redo, abort, stale, failure and resource ownership', async () => {
|
||||
const facade = createWebCadFacade({ initialDocument: fixture(), runtimeMode: 'mock' })
|
||||
const events: FacadeEvent[] = []
|
||||
facade.subscribe((event) => { if (event.type === 'property.before-change' || event.type === 'property.changed' || event.type === 'transaction.committed') events.push(event) })
|
||||
const resourcesBefore = facade.geometry.capabilities()
|
||||
facade.app.document.setProperty({ objectId: 'HeatFluxProbe', propertyName: 'DFlux', value: 500, expectedDocumentVersion: 1 })
|
||||
assert.equal(heatFlux(facade), 500)
|
||||
assert.equal(facade.app.document.getActive().version, 2)
|
||||
assert.deepEqual(events.map(({ type }) => type), ['property.before-change', 'property.changed', 'transaction.committed'])
|
||||
assert.equal(new Set(events.map((event) => 'transactionId' in event ? event.transactionId : '')).size, 1)
|
||||
facade.history.undo()
|
||||
assert.equal(heatFlux(facade), 0)
|
||||
assert.equal(facade.history.canRedo(), true)
|
||||
facade.history.redo()
|
||||
assert.equal(heatFlux(facade), 500)
|
||||
assert.equal(facade.history.canUndo(), true)
|
||||
|
||||
const stable = JSON.stringify(facade.app.document.getActive())
|
||||
const eventCount = events.length
|
||||
assert.throws(() => facade.app.document.setProperty({ objectId: 'HeatFluxProbe', propertyName: 'DFlux', value: 250, expectedDocumentVersion: 1 }), /Stale document version/)
|
||||
const cancelled = new AbortController(); cancelled.abort()
|
||||
assert.throws(() => facade.app.document.setProperty({ objectId: 'HeatFluxProbe', propertyName: 'DFlux', value: 250, signal: cancelled.signal }), (error: unknown) => error instanceof DOMException && error.name === 'AbortError')
|
||||
assert.throws(() => facade.app.document.setProperty({ objectId: 'HeatFluxProbe', propertyName: 'DFlux', value: 'invalid' as unknown as number }), /finite numeric value/)
|
||||
assert.equal(JSON.stringify(facade.app.document.getActive()), stable)
|
||||
assert.equal(events.length, eventCount)
|
||||
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()
|
||||
})
|
||||
47
tests/propertyIntegerSetFacade.test.ts
Normal file
47
tests/propertyIntegerSetFacade.test.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
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, FacadeEvent } from '../src/facade/types'
|
||||
|
||||
const fixture = (): DocumentSnapshot => ({
|
||||
id: 'property-integerset-facade', label: 'PropertyIntegerSet Facade', version: 1, dirty: false, readOnly: false, units: 'mm',
|
||||
tree: [{ id: 'NodesProbe', label: 'Nodes probe', type: 'feature', state: 'valid' }, { id: 'ElementsProbe', label: 'Elements probe', type: 'feature', state: 'valid' }],
|
||||
objects: [
|
||||
{ id: 'NodesProbe', typeId: 'Fem::FemSetNodesObject', properties: [{ name: 'Nodes', label: 'Nodes', group: 'Node indexes', scope: 'data', type: 'App::PropertyIntegerSet', value: [], recompute: true }] },
|
||||
{ id: 'ElementsProbe', typeId: 'Fem::FemSetElementNodesObject', properties: [{ name: 'Elements', label: 'Elements', group: 'Element indexes', scope: 'data', type: 'App::PropertyIntegerSet', value: [4, 10], recompute: true }] },
|
||||
],
|
||||
dependencies: [],
|
||||
recompute: { generation: 0, status: 'idle', objectStates: { NodesProbe: 'up-to-date', ElementsProbe: 'up-to-date' }, dirtyObjects: [], order: [], errors: [] },
|
||||
})
|
||||
|
||||
const valueOf = (facade: ReturnType<typeof createWebCadFacade>, objectId: string, propertyName: string) => facade.app.document.getObject(objectId)?.properties.find((property) => property.name === propertyName)?.value
|
||||
|
||||
test('App::PropertyIntegerSet normalizes unique integers and uses the native IntegerSet codec', () => {
|
||||
const facade = createWebCadFacade({ initialDocument: fixture(), initialSelectedObjectIds: ['NodesProbe'], runtimeMode: 'mock' })
|
||||
const events: FacadeEvent[] = []
|
||||
facade.subscribe((event) => { if (event.type === 'property.before-change' || event.type === 'property.changed' || event.type === 'transaction.committed') events.push(event) })
|
||||
facade.app.document.setProperty({ objectId: 'NodesProbe', propertyName: 'Nodes', value: [3, 1, 3, 2] })
|
||||
assert.deepEqual(valueOf(facade, 'NodesProbe', 'Nodes'), [1, 2, 3])
|
||||
assert.equal(facade.app.document.getActive().version, 2)
|
||||
assert.equal(facade.app.document.getActive().recompute?.objectStates.NodesProbe, 'touched')
|
||||
assert.deepEqual(events.map((event) => event.type), ['property.before-change', 'property.changed', 'transaction.committed'])
|
||||
assert.throws(() => facade.app.document.setProperty({ objectId: 'NodesProbe', propertyName: 'Nodes', value: [1, 1.5] }), /safe-integer array/)
|
||||
assert.throws(() => facade.app.document.setProperty({ objectId: 'NodesProbe', propertyName: 'Nodes', value: ['1'] as unknown as number[] }), /safe-integer array/)
|
||||
assert.throws(() => facade.app.document.setProperty({ objectId: 'NodesProbe', propertyName: 'Nodes', value: [Number.MAX_SAFE_INTEGER + 1] }), /safe-integer array/)
|
||||
assert.deepEqual(valueOf(facade, 'NodesProbe', 'Nodes'), [1, 2, 3])
|
||||
|
||||
const archive = facade.project.fcstd.serializeMetadata(facade.app.document.getActive())
|
||||
const xml = new TextDecoder().decode(unzipSync(archive)['Document.xml'])
|
||||
assert.match(xml, /<Property name="Nodes" type="App::PropertyIntegerSet" group="Node indexes"[^>]*><IntegerSet count="3"><I v="1"\/><I v="2"\/><I v="3"\/><\/IntegerSet><\/Property>/)
|
||||
const summary = facade.project.fcstd.inspect(archive).objects.find(({ name }) => name === 'NodesProbe')?.properties.find(({ name }) => name === 'Nodes')
|
||||
assert.deepEqual({ typeId: summary?.typeId, element: summary?.element }, { typeId: 'App::PropertyIntegerSet', element: 'IntegerSet' })
|
||||
assert.deepEqual(decodeFcstdPropertyValue(summary!), { value: [1, 2, 3], decoded: true })
|
||||
|
||||
const rewritten = facade.project.fcstd.rewriteIntegerSet(archive, { objectName: 'NodesProbe', propertyName: 'Nodes', value: [8, -1, 8], expectedValue: [1, 2, 3] })
|
||||
const rewrittenSummary = facade.project.fcstd.inspect(rewritten).objects.find(({ name }) => name === 'NodesProbe')?.properties.find(({ name }) => name === 'Nodes')
|
||||
assert.deepEqual(decodeFcstdPropertyValue(rewrittenSummary!), { value: [-1, 8], decoded: true })
|
||||
assert.throws(() => facade.project.fcstd.rewriteIntegerSet(archive, { objectName: 'NodesProbe', propertyName: 'Nodes', value: [4], expectedValue: [9] }), /does not match expectedValue/)
|
||||
})
|
||||
|
||||
45
tests/propertyIntegerSetTransaction.test.ts
Normal file
45
tests/propertyIntegerSetTransaction.test.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { createWebCadFacade } from '../src/facade/mockFacade'
|
||||
import type { DocumentSnapshot, FacadeEvent } from '../src/facade/types'
|
||||
|
||||
const fixture = (): DocumentSnapshot => ({
|
||||
id: 'property-integerset-transaction', label: 'PropertyIntegerSet Transaction', version: 1, dirty: false, readOnly: false, units: 'mm',
|
||||
tree: [{ id: 'NodesProbe', label: 'Nodes probe', type: 'feature', state: 'valid' }],
|
||||
objects: [{ id: 'NodesProbe', typeId: 'Fem::FemSetNodesObject', properties: [{ name: 'Nodes', label: 'Nodes', group: 'Node indexes', scope: 'data', type: 'App::PropertyIntegerSet', value: [1, 3], recompute: true }] }],
|
||||
dependencies: [],
|
||||
recompute: { generation: 0, status: 'idle', objectStates: { NodesProbe: 'up-to-date' }, dirtyObjects: [], order: [], errors: [] },
|
||||
})
|
||||
|
||||
const valueOf = (facade: ReturnType<typeof createWebCadFacade>) => facade.app.document.getObject('NodesProbe')?.properties[0].value
|
||||
|
||||
test('App::PropertyIntegerSet transaction closes normalization, undo, redo, stale, cancel, failure and resources', async () => {
|
||||
const facade = createWebCadFacade({ initialDocument: fixture(), runtimeMode: 'mock' })
|
||||
const events: FacadeEvent[] = []
|
||||
facade.subscribe((event) => { if (event.type === 'property.before-change' || event.type === 'property.changed' || event.type === 'transaction.committed') events.push(event) })
|
||||
const resourcesBefore = facade.geometry.capabilities()
|
||||
const requested = [5, 2, 5]
|
||||
facade.app.document.setProperty({ objectId: 'NodesProbe', propertyName: 'Nodes', value: requested, expectedDocumentVersion: 1 })
|
||||
requested[0] = 99
|
||||
assert.deepEqual(valueOf(facade), [2, 5])
|
||||
const exposed = valueOf(facade) as number[]
|
||||
exposed[0] = 99
|
||||
assert.deepEqual(valueOf(facade), [2, 5])
|
||||
assert.equal(facade.app.document.getActive().version, 2)
|
||||
assert.deepEqual(events.map(({ type }) => type), ['property.before-change', 'property.changed', 'transaction.committed'])
|
||||
assert.equal(new Set(events.map((event) => 'transactionId' in event ? event.transactionId : '')).size, 1)
|
||||
facade.history.undo(); assert.deepEqual(valueOf(facade), [1, 3]); assert.equal(facade.history.canRedo(), true)
|
||||
facade.history.redo(); assert.deepEqual(valueOf(facade), [2, 5]); assert.equal(facade.history.canUndo(), true)
|
||||
|
||||
const stable = JSON.stringify(facade.app.document.getActive())
|
||||
const eventCount = events.length
|
||||
assert.throws(() => facade.app.document.setProperty({ objectId: 'NodesProbe', propertyName: 'Nodes', value: [7], expectedDocumentVersion: 1 }), /Stale document version/)
|
||||
const cancelled = new AbortController(); cancelled.abort()
|
||||
assert.throws(() => facade.app.document.setProperty({ objectId: 'NodesProbe', propertyName: 'Nodes', value: [7], signal: cancelled.signal }), (error: unknown) => error instanceof DOMException && error.name === 'AbortError')
|
||||
assert.throws(() => facade.app.document.setProperty({ objectId: 'NodesProbe', propertyName: 'Nodes', value: [1.5] }), /safe-integer array/)
|
||||
assert.equal(JSON.stringify(facade.app.document.getActive()), stable)
|
||||
assert.equal(events.length, eventCount)
|
||||
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()
|
||||
})
|
||||
Reference in New Issue
Block a user