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

44 lines
2.8 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)
// 守的是「新建配置节点默认模型」的三条铁律:有效配置照用 / 未配置与失效都回退主服务首个 / 无模型才 null。
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), { providerId: 'p1', model: 'm1', keyGroup: 'k1' }, '未配置 ⇒ 回退主服务首个可见模型(保证「默认」标识始终有指向)')
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(有效照用 / 未配置与失效回退 / 无模型为 null / 保存校验)')