Files

61 lines
3.1 KiB
JavaScript
Raw Permalink 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/canvas-normalize.js 的单测(P2-9 第一片:Workbench 纯逻辑 → src/)
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const vm = require('vm')
const root = path.resolve(__dirname, '..')
const NODE_H = { prompt: 216, ref: 252, gen: 460, out: 320, layer: 520 }
const DEFAULTS = { prompt: { text: '' }, ref: { url: '', data: null, name: '', localName: null }, gen: { model: 'm' }, out: {}, layer: { layers: [] } }
const sandbox = {}
vm.runInNewContext(fs.readFileSync(path.join(root, 'src', 'canvas-normalize.js'), 'utf8'), sandbox)
const M = sandbox.IWBCanvasNormalize
assert.ok(M && typeof M.create === 'function', 'IWBCanvasNormalize.create 应被导出')
const api = M.create({ NODE_H, DEFAULTS })
assert.strictEqual(typeof api.normalizeNodes, 'function')
// normalizeNodes:F3 永不丢节点(未知类型 → _unknown / 带 variant → plugin)+ 合并 DEFAULTS + 补默认宽度 + ref 迁移
const nodes = api.normalizeNodes([
{ id: 'p', type: 'prompt', data: {} },
{ id: 'x', type: 'bogus', data: {} },
{ id: 'v', type: 'bogus2', data: { variant: 'hello' } },
{ id: 'l', type: 'layer' },
{ id: 'g', type: 'gen', w: 999 },
{ id: 'r', type: 'ref', data: { images: [{ url: 'u', data: 'd', name: 'n' }], urlInput: 'junk' } },
])
assert.strictEqual(nodes.length, 6, 'F3:未登记类型不再被丢弃(旧实现会静默丢节点)')
const byId = (id) => nodes.find((n) => n.id === id)
assert.strictEqual(byId('x').type, '_unknown', '未知类型且无 variant ⇒ 保留为 _unknown')
assert.strictEqual(byId('x').data.__raw, true)
assert.strictEqual(byId('v').type, 'plugin', '未知类型但有 data.variant ⇒ 归为 plugin(保留变体)')
assert.strictEqual(byId('v').data.variant, 'hello')
assert.strictEqual(byId('p').data.text, '', '应合并 DEFAULTS')
assert.strictEqual(byId('l').w, 520, 'layer 无 w 时补 520')
assert.strictEqual(byId('g').w, 999, '已有 w 不应被覆盖')
const r = byId('r')
assert.strictEqual(r.data.url, 'u')
assert.strictEqual(r.data.data, 'd')
assert.strictEqual(r.data.name, 'n')
assert.strictEqual(r.data.images, undefined, '旧 images[] 应删除')
assert.strictEqual(r.data.urlInput, undefined, 'urlInput 应删除')
// normalizeEdges:给 nodeIds 时丢弃悬空边
assert.strictEqual(api.normalizeEdges([{ from: 'a', to: 'b' }, { from: 'a', to: 'z' }], new Set(['a', 'b'])).length, 1)
assert.strictEqual(api.normalizeEdges([{ from: 'a', to: 'b' }, { from: 'a', to: 'z' }]).length, 2, '不给 nodeIds 时不过滤')
// normalizeGroups:只留 memberIds 数组
assert.strictEqual(api.normalizeGroups([{ memberIds: [] }, { foo: 1 }]).length, 1)
// migrateGenTaskIds:旧「来源 id」状态复制到「来源@批量节点」
const ns = [{ id: 'g1', type: 'gen', data: { taskActive: { s1: 'x' }, taskVariants: { s1: { a: 1 } } } }]
const migrated = api.migrateGenTaskIds(ns, [{ to: 'g1', from: 'b1', batchSrcId: 's1' }])
assert.strictEqual(migrated, true)
assert.strictEqual(ns[0].data.taskActive['s1@b1'], 'x')
assert.deepStrictEqual(ns[0].data.taskVariants['s1@b1'], { a: 1 })
console.log('canvas normalize checks passed')