Files
image-workbench-desktop/tests/history-store-test.js
T

111 lines
4.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict'
// tests/history-store.js 的单测(状态模型 epic · Pilot 1:撤销栈机器 → src/)
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const vm = require('vm')
const root = path.resolve(__dirname, '..')
const sandbox = { setTimeout, clearTimeout }
vm.runInNewContext(fs.readFileSync(path.join(root, 'src', 'canvas-history-store.js'), 'utf8'), sandbox)
const HS = sandbox.IWBHistoryStore
assert.ok(HS && typeof HS.create === 'function')
assert.strictEqual(HS.HISTORY_MAX, 20)
assert.strictEqual(HS.HISTORY_MIN_KEEP, 3)
// snapshot 计数 + restore 记录
let snapSeq = 0
const restored = []
const make = () => HS.create({ snapshot: () => ({ id: ++snapSeq }), restore: (s) => restored.push(s.id) })
const store = make()
store.setActive({ stack: [], idx: -1 })
assert.strictEqual(store.canUndo(), false)
assert.strictEqual(store.canRedo(), false)
// push:首帧补基准快照(载入后第一次操作也能一次撤销)
store.push()
assert.strictEqual(store.getActive().stack.length, 2, 'idx<0 首帧应补基准快照(baseline + snap)')
assert.strictEqual(store.getActive().idx, 1)
store.push()
assert.strictEqual(store.getActive().stack.length, 3)
assert.strictEqual(store.getActive().idx, 2)
assert.strictEqual(store.canUndo(), true)
// undo/redo 移动游标并回调 restore
assert.strictEqual(store.undo(), true)
assert.strictEqual(store.getActive().idx, 1)
assert.strictEqual(restored.length, 1)
assert.strictEqual(store.canRedo(), true)
assert.strictEqual(store.redo(), true)
assert.strictEqual(store.getActive().idx, 2)
assert.strictEqual(restored.length, 2)
// 深度上限 HISTORY_MAX:入栈 30 次,栈长封顶 20
const store2 = make()
store2.setActive({ stack: [], idx: -1 })
for (let i = 0; i < 30; i++) store2.push()
assert.strictEqual(store2.getActive().stack.length, HS.HISTORY_MAX, '超过 HISTORY_MAX 应 shift')
// 跨 session 预算淘汰:active 栈 + 另一个大栈,超预算时按 MIN_KEEP 淘汰最旧
const other = { stack: [], idx: 0 }
for (let i = 0; i < 10; i++) { other.stack.push({ big: i }); other.idx = i }
const store3 = HS.create({ bytesOf: () => 20 * 1048576, otherStacks: () => [other] })
store3.setActive({ stack: [{ big: 'a' }, { big: 'b' }, { big: 'c' }, { big: 'd' }], idx: 3 })
store3.evictGlobal()
assert.ok(store3.getActive().stack.length >= HS.HISTORY_MIN_KEEP, '淘汰后不得低于 MIN_KEEP')
assert.ok(other.stack.length >= HS.HISTORY_MIN_KEEP, '其他栈也不得低于 MIN_KEEP')
// 订阅:push/undo/redo 后通知
const store4 = make()
store4.setActive({ stack: [], idx: -1 })
let ticks = 0
store4.subscribe(() => ticks++)
store4.push()
assert.strictEqual(ticks, 1)
// 方案2 新增:打字状态(now 可注入 ⇒ 确定性)与清扫状态仪表
let clock = 1000
const store5 = HS.create({ now: () => clock })
assert.strictEqual(store5.isTyping(), false, '初始不在打字期')
store5.noteTyping()
assert.strictEqual(store5.isTyping(), true, 'noteTyping 后进入打字期')
clock = 1000 + 1700
assert.strictEqual(store5.isTyping(), false, '超过 1.6s 后退出打字期')
store5.noteTyping()
store5.clearTyping()
assert.strictEqual(store5.isTyping(), false, 'clearTyping 立即退出打字期')
const sweep0 = store5.sweepState()
assert.strictEqual(sweep0.sweeping, false, '初始 sweeping 为 false')
assert.strictEqual(sweep0.swept, 0, '初始 swept 为 0')
// 方案2:清扫器取栈来源 = 注入的 getStack(栈仍在 app.js,不依赖 setActive)
;(async () => {
const fakeStack = [{ id: 'a', __big: true }, { id: 'b', __big: true }]
const scrubbed = []
const store6 = HS.create({
getStack: () => fakeStack,
hasBigImage: (s) => !!s.__big,
unhydrate: (s) => { scrubbed.push(s.id); delete s.__big },
now: () => 0,
})
assert.strictEqual(store6.getActive().stack.length, 0, '未 setActive 时 active 为空(栈在 app.js)')
store6.schedule()
await new Promise((r) => setTimeout(r, 30))
assert.ok(scrubbed.length >= 1, 'schedule 应清扫 getStack 注入的栈,实际:' + JSON.stringify(scrubbed))
// 第三片:discardPending 丢弃未入栈的防抖窗口(切画板用)
let snaps = 0
const store7 = HS.create({ snapshot: () => ({ id: ++snaps }), restore: () => {} })
store7.setActive({ stack: [], idx: -1 })
store7.pushDebounced()
store7.discardPending()
await new Promise((r) => setTimeout(r, 900))
assert.strictEqual(store7.getActive().stack.length, 0, 'discardPending 后不得入栈')
store7.pushDebounced()
await new Promise((r) => setTimeout(r, 900))
assert.strictEqual(store7.getActive().stack.length, 2, '未丢弃时应到点入栈(首帧含基准快照)')
console.log('history store checks passed')
})().catch((e) => { console.error(e); process.exit(1) })