233 lines
8.5 KiB
JavaScript
233 lines
8.5 KiB
JavaScript
'use strict';
|
|
const test = require('node:test'),
|
|
assert = require('node:assert/strict'),
|
|
fs = require('node:fs'),
|
|
os = require('node:os'),
|
|
path = require('node:path');
|
|
const { RuleStore } = require('../../src/modules/order-rules/store.cjs');
|
|
const { OrderExecutionCoordinator } = require('../../src/modules/order-execution/coordinator.cjs');
|
|
const principal = {
|
|
user: { id: 'admin', username: 'admin', role: 'admin' },
|
|
sessionId: 'test-session',
|
|
};
|
|
function fixture(t) {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'field-skill-')),
|
|
store = new RuleStore(dir, { clock: () => new Date('2026-10-20T02:00:00Z') });
|
|
t.after(() => {
|
|
store.close();
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
});
|
|
const connection = { id: 'c1', owner: 'admin', status: 'connected' },
|
|
workspace = {
|
|
getConnection: () => connection,
|
|
snapshot: () => ({ connections: [connection] }),
|
|
};
|
|
const row = {
|
|
source: 'todo',
|
|
instanceId: '1',
|
|
taskId: '1',
|
|
revision: '1',
|
|
processName: '备损订单评审',
|
|
processVersion: '2.0',
|
|
nodeName: '订单专员(订单通知书)',
|
|
receivedAt: '2026-10-01T02:00:00Z',
|
|
title: '订单号:123机芯号:ABC客户简称:PRIVATE_CUSTOMER总数量:0',
|
|
serialNum: 'TEST',
|
|
};
|
|
let captures = 0;
|
|
const provider = {
|
|
scan: async () => [row, { ...row, taskId: '2', instanceId: '2' }],
|
|
captureFields: async (c, p, r) => {
|
|
captures++;
|
|
return {
|
|
sample: {
|
|
...r,
|
|
connectionId: c.id,
|
|
fields: { 'Order.Number': '123', 'Order.Quantity': '0', 'Order.Amount': '999' },
|
|
},
|
|
snapshot: {
|
|
purpose: 'field-mapping',
|
|
processVersion: r.processVersion,
|
|
fields: [
|
|
{ path: 'Order.Number', label: '订单号', value: '123' },
|
|
{ path: 'Order.Quantity', label: '数量', value: '0' },
|
|
{ path: 'Order.Amount', label: '总金额', value: '999' },
|
|
],
|
|
},
|
|
};
|
|
},
|
|
};
|
|
const execution = new OrderExecutionCoordinator({
|
|
store,
|
|
workspace,
|
|
provider,
|
|
archive: {},
|
|
mail: {},
|
|
clock: () => new Date('2026-10-20T02:00:00Z'),
|
|
});
|
|
return {
|
|
deps: { workspace, orderRules: store, orderExecution: execution },
|
|
store,
|
|
connection,
|
|
captures: () => captures,
|
|
};
|
|
}
|
|
test('ZCode project skill collects one sample per missing version and only prepares checked drafts', async (t) => {
|
|
const skill = require('../../src/modules/assistant/field-skill.cjs'),
|
|
f = fixture(t),
|
|
progress = [];
|
|
const report = await skill.collect(f.deps, 'c1', principal, 'action1', (p) => progress.push(p));
|
|
assert.equal(report.captured, 1);
|
|
assert.equal(f.captures(), 1);
|
|
assert.equal(progress.at(-1).completed, 1);
|
|
const sample = f.store.all('capture-samples')[0],
|
|
guard = skill.guardMapping(f.deps, sample.id, principal.user);
|
|
const result = await skill.prepare(f.deps, sample.id, principal, 'prepare1', guard);
|
|
assert.equal(result.status, 'checked');
|
|
assert.equal(f.store.all('field-mapping-drafts').length, 1);
|
|
assert.equal(f.store.all('field-adapters').length, 0);
|
|
assert.deepEqual(f.store.all('orders'), []);
|
|
assert.deepEqual(f.store.all('batches'), []);
|
|
const context = skill.coverage(f.deps, principal.user);
|
|
assert.deepEqual(context.samples, [
|
|
{
|
|
id: sample.id,
|
|
connectionId: 'c1',
|
|
processName: sample.processName,
|
|
version: sample.processVersion,
|
|
},
|
|
]);
|
|
// Opaque sample hashes may contain "999"; inspect the permitted metadata,
|
|
// whose exact sample shape above also excludes raw fields and evidence.
|
|
assert.doesNotMatch(
|
|
JSON.stringify(context, (key, value) => (key === 'id' ? undefined : value)),
|
|
/PRIVATE_CUSTOMER|Order\.Number|999|订单号:123/,
|
|
);
|
|
assert.equal((await skill.collect(f.deps, 'c1', principal, 'action2', () => {})).captured, 0);
|
|
assert.equal(f.captures(), 1);
|
|
await assert.rejects(
|
|
skill.prepare(f.deps, sample.id, principal, 'stale', guard),
|
|
(e) => e.status === 409,
|
|
);
|
|
f.connection.status = 'disabled';
|
|
assert.throws(
|
|
() => skill.guardMapping(f.deps, sample.id, principal.user),
|
|
(e) => e.status === 403,
|
|
);
|
|
});
|
|
test('mapping recommendations keep ambiguous fields unresolved and never mix amount and quantity', () => {
|
|
const { recommend } = require('../../src/modules/assistant/field-skill.cjs');
|
|
const r = recommend([
|
|
{ path: 'A.Order', label: '订单号', value: '123' },
|
|
{ path: 'B.Order', label: '订单号', value: '123' },
|
|
{ path: 'Amount', label: '总金额', value: '10' },
|
|
{ path: 'Qty', label: '数量', value: '0' },
|
|
]);
|
|
assert.equal(r.sources.orderNo, undefined);
|
|
assert.equal(r.sources.quantity.path, 'Qty');
|
|
assert.equal(r.sources.totalAmount.path, 'Amount');
|
|
assert.ok(r.issues.length);
|
|
assert.equal(
|
|
recommend([{ path: 'ProductRequirement.Antennanum', label: '数量', value: '2' }]).sources
|
|
.quantity,
|
|
undefined,
|
|
);
|
|
});
|
|
test('application skill is versioned, injected into prompt and cannot enable native tools', () => {
|
|
const { projectSkills } = require('../../src/modules/assistant/skills.cjs'),
|
|
{ prompt, runtimeConfig } = require('../../src/modules/assistant/zcode-runtime.cjs');
|
|
const skills = projectSkills();
|
|
assert.equal(skills[0].id, 'bpm-field-mapping');
|
|
assert.match(skills[0].sha256, /^[a-f0-9]{64}$/);
|
|
const input = prompt({
|
|
text: '采集字段样本',
|
|
history: [],
|
|
context: {},
|
|
config: { allowActions: true },
|
|
});
|
|
assert.match(input, /BPM 信息识别技能/);
|
|
assert.match(input, /collect_field_samples/);
|
|
assert.match(input, /HA/);
|
|
const config = runtimeConfig({ timeoutSeconds: 30 }, 'test');
|
|
assert.equal(config.features.skill, false);
|
|
assert.ok(config.permission.disallowedTools.includes('Skill'));
|
|
});
|
|
|
|
test('historical evidence can check a mapping while real notification matching still rejects history', async (t) => {
|
|
const f = fixture(t),
|
|
{ recommend } = require('../../src/modules/assistant/field-skill.cjs'),
|
|
row = {
|
|
source: 'history',
|
|
configurationOnly: true,
|
|
instanceId: '1',
|
|
taskId: '1',
|
|
revision: 0,
|
|
connectionId: 'c1',
|
|
processName: '备损订单评审',
|
|
processVersion: '2.0',
|
|
nodeName: '历史查看(未提供办理节点)',
|
|
receivedAt: '2026-10-01T00:00:00Z',
|
|
timestampType: 'process-start',
|
|
serialNum: 'TEST',
|
|
title: '订单号:123机芯号:ABC',
|
|
fields: { order: '123' },
|
|
},
|
|
snapshot = {
|
|
purpose: 'field-mapping',
|
|
processVersion: '2.0',
|
|
fields: [{ path: 'order', label: '订单号', value: '123' }],
|
|
};
|
|
const sample = f.store.recordCapture({ sample: row, snapshot, taskId: 'h1' }),
|
|
draft = f.store.saveMapping(
|
|
{
|
|
processName: row.processName,
|
|
processVersion: row.processVersion,
|
|
sampleId: sample.id,
|
|
sources: recommend(snapshot.fields).sources,
|
|
version: 0,
|
|
idempotencyKey: 'draft',
|
|
},
|
|
principal.user,
|
|
);
|
|
const result = f.store.checkMapping(
|
|
{ id: draft.id, version: draft.version, sampleId: sample.id, idempotencyKey: 'check' },
|
|
principal.user,
|
|
);
|
|
assert.equal(result.valid, true);
|
|
assert.equal(result.result.configurationOnly, true);
|
|
assert.ok(result.result.warnings.length);
|
|
assert.equal(f.store.capturedPreview(sample.id).valid, false);
|
|
});
|
|
|
|
test('assistant prepares the same active-only selection as page preview and never activates or publishes it', async (t) => {
|
|
const f = fixture(t),
|
|
skill = require('../../src/modules/assistant/field-skill.cjs');
|
|
await skill.collect(f.deps, 'c1', principal, 'collect-consistent', () => {});
|
|
const sample = f.store.all('capture-samples')[0];
|
|
f.store.put('field-adapters', {
|
|
id: 'legacy-selected',
|
|
processName: sample.processName,
|
|
processVersion: sample.processVersion,
|
|
verified: true,
|
|
version: 2,
|
|
sources: { orderNo: { type: 'form', path: 'Order.Number' }, brief: { type: 'title' } },
|
|
});
|
|
const proposal = f.store.proposeMapping({ sampleId: sample.id }),
|
|
before = f.store.all('rules');
|
|
assert.equal(proposal.mapping.basis, 'active');
|
|
assert.equal(proposal.mapping.sources.quantity, undefined);
|
|
const result = await skill.prepare(
|
|
f.deps,
|
|
sample.id,
|
|
principal,
|
|
'prepare-consistent',
|
|
skill.guardMapping(f.deps, sample.id, principal.user),
|
|
);
|
|
assert.equal(result.status, 'checked');
|
|
const draft = f.store.all('field-mapping-drafts')[0];
|
|
assert.equal(draft.sources.quantity, undefined);
|
|
assert.deepEqual(f.store.all('rules'), before);
|
|
assert.equal(f.store.all('field-adapters').length, 1);
|
|
assert.equal(f.store.settings().enabled, false);
|
|
});
|