Improved harness - with card selection
This commit is contained in:
45
card-harness/scripts/surface-defaults.test.mjs
Normal file
45
card-harness/scripts/surface-defaults.test.mjs
Normal file
@@ -0,0 +1,45 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { resolve } from 'node:path'
|
||||
import { readSurfaceDefaults, updateSurfaceDefaults, validateSurfaceDefaults } from './surface-defaults.mjs'
|
||||
|
||||
const initial = {
|
||||
schemaVersion: 1,
|
||||
finishes: {
|
||||
'Printed ink': { finishStrength: 0.6 }, Foil: { finishStrength: 1 },
|
||||
Holographic: { finishStrength: 0.33 }, 'Gold leaf': { finishStrength: 0.6 },
|
||||
'Frosted glass': { finishStrength: 0.6 },
|
||||
},
|
||||
materials: {
|
||||
Paper: { surfaceDetail: 0.14, roughness: 0.23 },
|
||||
Linen: { surfaceDetail: 0.05, roughness: 0.23 },
|
||||
Metal: { surfaceDetail: 1, roughness: 0.23, metalBrushHorizontal: false },
|
||||
Wood: { surfaceDetail: 0.14, roughness: 0.23 },
|
||||
Leather: { surfaceDetail: 0.45, roughness: 0.23 },
|
||||
},
|
||||
}
|
||||
|
||||
test('surface defaults validate requested finish/material strengths and reject unknown settings', () => {
|
||||
assert.equal(validateSurfaceDefaults(structuredClone(initial)).finishes.Holographic.finishStrength, 0.33)
|
||||
const malformed = structuredClone(initial)
|
||||
malformed.materials.Linen.shaderSecret = 1
|
||||
assert.throws(() => validateSurfaceDefaults(malformed), /Unsupported setting/)
|
||||
})
|
||||
|
||||
test('surface default saves are atomic, scoped, and revision checked', async (t) => {
|
||||
const directory = await mkdtemp(resolve(tmpdir(), 'surface-defaults-'))
|
||||
t.after(() => rm(directory, { recursive: true }))
|
||||
const path = resolve(directory, 'defaults.json')
|
||||
await writeFile(path, `${JSON.stringify(initial, null, 2)}\n`)
|
||||
const before = await readSurfaceDefaults(true, path)
|
||||
const after = await updateSurfaceDefaults({ revision: before.revision, group: 'finish',
|
||||
name: 'Holographic', values: { finishStrength: 0.41 } }, path)
|
||||
assert.equal(after.defaults.finishes.Holographic.finishStrength, 0.41)
|
||||
assert.deepEqual(after.defaults.materials, initial.materials)
|
||||
assert.notEqual(after.revision, before.revision)
|
||||
await assert.rejects(updateSurfaceDefaults({ revision: before.revision, group: 'material',
|
||||
name: 'Linen', values: { surfaceDetail: 0.08, roughness: 0.23 } }, path), /changed since/)
|
||||
assert.doesNotMatch(await readFile(path, 'utf8'), /\.tmp/)
|
||||
})
|
||||
Reference in New Issue
Block a user