gongxiangp2/lib/__tests__/desktop-agent-rules.test.ts

33 lines
2.1 KiB
TypeScript

/** @jest-environment node */
import { desktopAgentRules } from '../desktop-client/agent-rules'
import { createHash } from 'node:crypto'
import { emptyBrief, validateBrief } from '../desktop-client/creation.mjs'
describe('desktop Agent versioned rule catalog', () => {
test('lists only real ready combinations and returns an exact scoped package', () => {
const index = desktopAgentRules(null) as any
expect(index.items).toHaveLength(1)
expect(index.items[0].label).toBe('顾家 · 电脑桌')
const selected = desktopAgentRules(index.items[0].id) as any
expect(selected.catalog.brands).toHaveLength(1)
expect(selected.catalog.shops[0].id).toBe(index.items[0].shopId)
expect(selected.catalog.categories[0].id).toBe(index.items[0].categoryId)
expect(selected.catalog.bindings).toHaveLength(1)
const canonical = JSON.stringify(selected.catalog, (_, item) => item && typeof item === 'object' && !Array.isArray(item)
? Object.fromEntries(Object.keys(item).sort().map(key => [key,item[key]])) : item)
expect(selected.contentHash).toBe(createHash('sha256').update(canonical).digest('hex'))
expect(index.items[0].contentHash).toBe(selected.contentHash)
expect(index.items[0].projectBrandId).toBe(index.items[0].shopId)
expect(index.categories.some((c: any) => c.name === '化妆台')).toBe(true)
expect(index.items.some((c: any) => c.categoryName === '化妆台')).toBe(false)
expect(() => desktopAgentRules('unknown')).toThrow('尚未配置')
expect(() => desktopAgentRules('../other')).toThrow('编号无效')
})
test('old briefs keep an empty category, while an explicit category survives cloud validation', () => {
const old = emptyBrief() as any
delete old.categoryId; delete old.categoryName
expect(validateBrief(old)).toMatchObject({ categoryId: '', categoryName: '' })
expect(validateBrief({ ...old, categoryId: 'real-category', categoryName: '电脑桌' })).toMatchObject({ categoryId: 'real-category', categoryName: '电脑桌' })
expect(() => validateBrief({ ...old, categoryId: false })).toThrow('品类格式无效')
})
})