Files
image-workbench-desktop/tests/generation-default-model-test.js
T

44 lines
2.7 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'
// src/generation-default-model.js 行为契约(2026-11-25)
// 守的是「新建配置节点默认模型」的三条铁律:未配置零变化 / 有效配置照用 / 失效回退主服务首个。
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const vm = require('vm')
const root = path.resolve(__dirname, '..')
const box = { console: { log() {}, warn() {}, error() {} } }
vm.runInNewContext(fs.readFileSync(path.join(root, 'src', 'generation-default-model.js'), 'utf8'), box)
const M = box.IWBDefaultGenModel
assert.ok(M && typeof M.wResolveDefaultGen === 'function', '应导出 IWBDefaultGenModel.wResolveDefaultGen')
const eq = (a, b, msg) => assert.strictEqual(JSON.stringify(a), JSON.stringify(b), msg)
const cfg = {
providers: [
{ id: 'p1', name: '主服务', keys: [{ id: 'k1' }, { id: 'k1b' }], models: [{ name: 'm1', visible: true }, { name: 'm2', visible: false }, { name: 'm1', visible: true }] },
{ id: 'p2', name: '备用', keys: [{ id: 'k2' }], models: [{ name: 'm3', visible: true }] },
],
}
eq(M.wDefaultGenOptions(cfg).map((o) => o.label), ['主服务 · m1', '备用 · m3'], '选项只含可见模型、去重保序,标签=服务名 · 模型名')
eq(M.wResolveDefaultGen(cfg), null, '未配置 ⇒ null(宿主保留硬编码,存量行为零变化)')
eq(M.wResolveDefaultGen({ ...cfg, defaultGen: { providerId: 'p2', model: 'm3' } }),
{ providerId: 'p2', model: 'm3', keyGroup: 'k2' }, '有效配置照用,keyGroup 取该服务首个 Key')
eq(M.wResolveDefaultGen({ ...cfg, defaultGen: { providerId: 'p1', model: 'm1', keyGroup: '自定义组' } }),
{ providerId: 'p1', model: 'm1', keyGroup: '自定义组' }, '有效配置保留用户显式 keyGroup')
eq(M.wResolveDefaultGen({ ...cfg, defaultGen: { providerId: 'p1', model: 'm2' } }),
{ providerId: 'p1', model: 'm1', keyGroup: 'k1' }, '配置的模型被关闭 ⇒ 回退主服务首个可见模型')
eq(M.wResolveDefaultGen({ ...cfg, defaultGen: { providerId: 'pX', model: 'gone' } }),
{ providerId: 'p1', model: 'm1', keyGroup: 'k1' }, '服务被删 ⇒ 同样回退主服务首个')
eq(M.wResolveDefaultGen({ providers: [], defaultGen: { providerId: 'p1', model: 'm1' } }), null, '无可用模型 ⇒ null')
eq(M.wNormalizeDefaultGen(cfg, { providerId: 'p2', model: 'm3' }),
{ providerId: 'p2', model: 'm3', keyGroup: 'k2' }, '归一化接受有效项')
eq(M.wNormalizeDefaultGen(cfg, { providerId: 'p1', model: 'm2' }), null, '归一化拒绝不可见模型(让读取侧走回退)')
eq(M.wNormalizeDefaultGen(cfg, null), null, '空值归一化为 null')
console.log('generation-default-model checks passed(未配置零变化 / 有效照用 / 失效回退 / 保存校验)')