67 lines
2.7 KiB
JavaScript
67 lines
2.7 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import { createHash } from 'node:crypto'
|
|
import { after, before, test } from 'node:test'
|
|
import knowledgeProxy from './knowledge-proxy.ts'
|
|
|
|
const { proxyKnowledge } = knowledgeProxy
|
|
|
|
const catalogPath = '/api/xiaoyi/v1/templates'
|
|
const manifestPath = `${catalogPath}/test-chair/releases/v1/manifest`
|
|
const intake = {
|
|
version: 1,
|
|
productFields: [{
|
|
id: 'material', label: '面料', type: 'text', multiline: true,
|
|
placeholder: '填写本款面料名称。\n不同颜色的面料分行填写。',
|
|
help: '按本款确认资料填写;尺寸注明单位,暂无资料可留空。',
|
|
}],
|
|
barcodeFields: [
|
|
{ id: 'color', label: '颜色', type: 'text', placeholder: '填写本条码对应的颜色。' },
|
|
{ id: 'width', label: '座宽', type: 'number', unit: 'cm' },
|
|
],
|
|
}
|
|
const originalEnv = process.env
|
|
|
|
before(() => {
|
|
process.env = {
|
|
...originalEnv,
|
|
XIAOYI_KNOWLEDGE_UPSTREAM_ORIGIN: 'https://knowledge.test',
|
|
XIAOYI_KNOWLEDGE_SESSION_SECRET: 'a'.repeat(32),
|
|
}
|
|
})
|
|
|
|
after(() => { process.env = originalEnv })
|
|
|
|
function readTemplate(path) {
|
|
const request = new Request(`https://platform.test/api/desktop/knowledge/proxy${path}`)
|
|
return proxyKnowledge(request, 'test-owner', new Date(Date.now() + 60_000), false)
|
|
}
|
|
|
|
test('forwards product and barcode placeholders with the published catalog', async ({ mock }) => {
|
|
const catalog = { items: [{ templateId: 'test-chair', intake }] }
|
|
const upstream = mock.method(globalThis, 'fetch', async () => Response.json(catalog))
|
|
|
|
const response = await readTemplate(catalogPath)
|
|
|
|
assert.equal(upstream.mock.calls[0].arguments[0], `https://knowledge.test${catalogPath}`)
|
|
assert.equal(response.status, 200)
|
|
assert.deepEqual(await response.json(), catalog)
|
|
})
|
|
|
|
test('preserves published manifest bytes and their digest when they contain placeholders', async ({ mock }) => {
|
|
const manifest = Buffer.from(JSON.stringify({ templateId: 'test-chair', version: 'v1', intake }, null, 2) + '\n')
|
|
const sha256 = createHash('sha256').update(manifest).digest('hex')
|
|
const etag = `"${sha256}"`
|
|
const upstream = mock.method(globalThis, 'fetch', async () => new Response(manifest, {
|
|
headers: { 'Content-Type': 'application/json', 'Content-Length': String(manifest.length), ETag: etag },
|
|
}))
|
|
|
|
const response = await readTemplate(manifestPath)
|
|
const body = Buffer.from(await response.arrayBuffer())
|
|
|
|
assert.equal(upstream.mock.calls[0].arguments[0], `https://knowledge.test${manifestPath}`)
|
|
assert.equal(response.status, 200)
|
|
assert.equal(response.headers.get('etag'), etag)
|
|
assert.equal(response.headers.get('content-length'), String(manifest.length))
|
|
assert.deepEqual(body, manifest)
|
|
})
|