File size: 10,945 Bytes
5afd2f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import assert from 'node:assert/strict';
import fs from 'node:fs';
import test from 'node:test';
import {LABELS, QUOTA_FIELDS, RECORDING_KEYS, observerBodies, setOptional, addArrangement, setArrangementLabels, unimplementedMotion, batchTotal, castKinds, configRow, defaultBatch, distSummary, freshSeed, missingGroupFields, setSceneCount, toggleScene, unfilledHints} from '../src/np-batch.ts';
import type {Dist, GenSchema, SingleConfig} from '../src/np-batch.ts';

test('默认三场景共12段,默认分布与设计一致且每次独立', () => {
  const batch = defaultBatch(['a', 'b', 'c']);
  assert.equal(batchTotal(batch), 12);
  assert.equal(defaultBatch(['a'], 7).seed, 7);
  assert.deepEqual(batch.template.groups[0].duration, {dist: 'choice', values: [90, 360], weights: [3, 1]});
  assert.deepEqual(batch.template.groups[0].observer_mix.view, {dist: 'fixed', value: 'auto'});
  assert.deepEqual(batch.template.groups[0].flavor_mix, {dist: 'fixed', value: 'plain'});
  batch.template.groups[0].cast_mix.humanoid = 99;
  assert.notEqual(defaultBatch(['a']).template.groups[0].cast_mix.humanoid, 99);
  assert.equal(batchTotal(defaultBatch([])), 0);
  const doubled = defaultBatch(['a', 'b', 'c']);
  doubled.template.groups.push(structuredClone(doubled.template.groups[0]));
  assert.equal(batchTotal(doubled), 24);
});
test('schema 默认的空组(groups:[{}])报缺字段,默认分布不缺:屏 1 不把空组交给编辑器而白屏', () => {
  assert.deepEqual(missingGroupFields(defaultBatch(['a'])), []);
  const empty = {seed: 0, scenes: [], template: {groups: [{}]}} as unknown as Parameters<typeof missingGroupFields>[0];
  assert.deepEqual(missingGroupFields(empty), ['第 1 组 时长', '第 1 组 主体组成', '第 1 组 观察者', '第 1 组 段风格', '第 1 组 情形安排']);
  assert.deepEqual(missingGroupFields({seed: 0, scenes: []} as unknown as Parameters<typeof missingGroupFields>[0]), ['template.groups']);
});

test('场景加减、段数更新不修改原对象、不新增未知场景', () => {
  const batch = defaultBatch(['a']), added = toggleScene(batch, 'b');
  assert.deepEqual(added.scenes, [{scene: 'a', count: 4}, {scene: 'b', count: 4}]);
  assert.deepEqual(toggleScene(added, 'a').scenes, [{scene: 'b', count: 4}]);
  assert.equal(batchTotal(setSceneCount(added, 'a', 7)), 11);
  assert.deepEqual(setSceneCount(added, 'missing', 7), added);
  assert.deepEqual(batch.scenes, [{scene: 'a', count: 4}]);
});
const summaries: [string, Dist, string | undefined, string][] = [
  ['裸数字', 90, 's', '90 s'], ['裸枚举', 'seek', undefined, '去目标点'], ['未知枚举', 'future', undefined, 'future'],
  ['固定', {dist: 'fixed', value: 90}, 's', '固定 90 s'], ['固定枚举', {dist: 'fixed', value: 'plain'}, undefined, '固定 普通'],
  ['均匀', {dist: 'uniform', lo: 60, hi: 120}, 's', '均匀 60–120 s'],
  ['正态', {dist: 'normal', mean: 1.2, sd: .2, lo: .5, hi: 2}, undefined, '正态 均值 1.2,标准差 0.2(限 0.5–2)'],
  ['无界正态', {dist: 'normal', mean: 1, sd: .2}, undefined, '正态 均值 1,标准差 0.2'],
  ['单边截断', {dist: 'normal', mean: 1, sd: .2, lo: 0}, undefined, '正态 均值 1,标准差 0.2(限 0–∞)'],
  ['对数正态', {dist: 'lognormal', median: 2, sigma: .3}, undefined, '对数正态 中位 2,sigma 0.3'],
  ['加权选择', {dist: 'choice', values: [90, 360], weights: [3, 1]}, 's', '选一:90 s 占 3 份、360 s 占 1 份'],
  ['等概率', {dist: 'choice', values: ['seek', 'roam']}, undefined, '选一:去目标点、漫游(等概率)'],
  ['混合', {dist: 'mixture', of: [1, {dist: 'uniform', lo: 2, hi: 3}]}, undefined, '混合 2 种'],
];
for (const [title, dist, unit, expected] of summaries) test('分布摘要:' + title, () => assert.equal(distSummary(dist, unit), expected));
test('单段行展示真实主体、观察者视角与安排;缺少可选项不补造结果', () => {
  const config: SingleConfig = {id: 'gen-a-g0-0000', scene: 'a', seed: 5, duration_s: 90,
    cast: [{id: 'h0', kind: 'humanoid'}, {id: 'h1', kind: 'humanoid'}, {id: 'q0', kind: 'quadruped'}],
    observers: [{id: 'A', kind: 'embodied', motion_mode: 'seek', view: {mode: 'heading'}}, {id: 'B', kind: 'virtual_bound', motion_mode: 'roam', view: {mode: 'auto'}}],
    arrangements: [{type: 'stop_at', who: ['h0']}]};
  assert.equal(configRow(config).arrangements, 'stop_at(h0)');   // 台里不写安排中文名
  setArrangementLabels({stop_at: '在某处站住'});                     // 由生成器 schema 给
  assert.deepEqual(configRow(config), {id: 'gen-a-g0-0000', scene: 'a', seed: 5, duration: 90, cast: '人形 2 · 机器狗 1', observers: 'A 实体观察者·去目标点·朝前进方向;B 虚拟观察者(沿通行区走)·漫游·自动', arrangements: '在某处站住(h0)'});
  assert.equal(configRow({...config, arrangements: []}).arrangements, '无');
  assert.equal(configRow({...config, observers: undefined}).observers, '无');
});
test('主体种类从schema取,排除camera和所有_cam后缀,保留新种类', () => {
  const schema = {bodies: {humanoid: {}, quadruped: {}, camera: {}, quadruped_cam: {}, new_cam: {}, future: {}}} as GenSchema;
  assert.deepEqual(castKinds(schema), ['humanoid', 'quadruped', 'future']);
  assert.equal(Object.keys(schema.bodies).length, 6);
});

test('第 5 卡一键加安排:每组加一条必须、每段 1 次,已有的组不重复', () => {
  const batch = defaultBatch(['a']);
  batch.template.groups.push(structuredClone(batch.template.groups[0]));
  batch.template.groups[1].arrangement_quota.push({type: 'observer_body', count: 2, strength: 'soft'});
  const next = addArrangement(batch, 'observer_body');
  assert.deepEqual(next.template.groups[0].arrangement_quota.at(-1), {type: 'observer_body', count: 1, strength: 'hard'});
  assert.equal(next.template.groups[1].arrangement_quota.filter(q => q.type === 'observer_body').length, 1);
  assert.equal(batch.template.groups[0].arrangement_quota.length, 1);
});

test('展开表问题列:尽量的放不下列成提示,必须的不在这里(已是配置问题)', () => {
  const cfg = {scene: 's', seed: 0, duration_s: 90, cast: [], unfilled: [
    {what: 'cast', type: 'humanoid', strength: 'soft', reason: '4 drawn, the house holds 3 more humans (max_humans)'},
    {what: 'observer', type: 'embodied', strength: 'hard', reason: 'every kind wears a registered look'}]};
  assert.deepEqual(unfilledHints(cfg), [{text: '主体 人形 没放下(尽量)', reason: '4 drawn, the house holds 3 more humans (max_humans)'}]);
  assert.deepEqual(unfilledHints({scene: 's', seed: 0, duration_s: 90, cast: []}), []);
});

test('生成器机身登记表里的每种机身都有中文名(单段视图的主体种类、观察者机身不露开发 id)', () => {
  const bodies = JSON.parse(fs.readFileSync(new URL('../../../../embodied_pipeline/gen/bodies.json', import.meta.url), 'utf8')).bodies;
  assert.deepEqual(Object.keys(bodies).filter(kind => !LABELS[kind]), []);
});

test('新建批次各取一个种子,不再都用 0(同种子的两批剧本和题完全重复)', () => {
  const seeds = new Set(Array.from({length: 20}, () => defaultBatch(['a']).seed));
  assert.ok(seeds.size > 1);
  for (const seed of seeds) assert.ok(Number.isInteger(seed) && seed >= 0 && seed < 2 ** 31);
  assert.ok(Number.isInteger(freshSeed()));
});

test('移动方式:生成器没实现的灰掉', () => {
  const schema = {motion_modes: ['seek', 'roam', 'static', 'follow'], implemented_motion_modes: ['seek', 'roam', 'static']} as GenSchema;
  assert.deepEqual(unimplementedMotion(schema), ['follow']);
});

test('可选字段:清空就删键,不留空键(不填的批量和以前逐字节一样)', () => {
  const quota = {type: 'stop_at', count: 1, strength: 'hard' as const};
  const set = setOptional(quota, 'dwell_s', 8);
  assert.deepEqual(set, {...quota, dwell_s: 8});
  assert.equal(JSON.stringify(setOptional(set, 'dwell_s', undefined)), JSON.stringify(quota));
  assert.deepEqual(quota, {type: 'stop_at', count: 1, strength: 'hard'});   // 不改原对象
});
test('安排字段表排除退役类型和禁用字段,录像六项与后端允许集合一致且都有中文名', () => {
  const gen = fs.readFileSync(new URL("../../../../embodied_pipeline/gen/config.py", import.meta.url), "utf8");
  // 生成器的安排类型和退役集合都从锚定合同派生(embodied_pipeline/gen/config.py ANCHORS),这里读同一份合同
  const anchors = JSON.parse(fs.readFileSync(new URL('../../../../embodied_pipeline/prover/wp_target.json', import.meta.url), 'utf8')).anchors;
  const types: string[] = anchors.types.flatMap((a: {id: string, subtypes?: string[]}) => [a.id, ...(a.subtypes ?? [])]);   // config.py ARRANGEMENTS 与 SUBTYPES
  assert.ok(Array.isArray(anchors.retired), '锚定合同必须声明退役安排集合');
  const retired: string[] = anchors.retired;
  assert.ok(retired.includes('target_visible'));
  assert.deepEqual(Object.keys(QUOTA_FIELDS).sort(), types.filter(type => !retired.includes(type)).sort());
  assert.equal(Object.hasOwn(QUOTA_FIELDS, 'target_visible'), false);
  assert.ok(Object.values(QUOTA_FIELDS).every(fields => !fields.some(field => String(field) === 'min_view_s')));
  assert.deepEqual(QUOTA_FIELDS.covisible_event, ['event']);
  assert.deepEqual(QUOTA_FIELDS.observer_body, ['times']);
  const keys = gen.match(/arrangement=\{(.*?)\}/s)![1];
  for (const fields of Object.values(QUOTA_FIELDS)) for (const f of fields) assert.ok(keys.includes(`'${f}'`), f);
  const recordingDeclaration = gen.match(/\('recording', \{([^}]+)\}\)/);
  assert.ok(recordingDeclaration, '生成器必须声明录像允许键集合');
  const recordingKeys = [...recordingDeclaration[1].matchAll(/'([^']+)'/g)].map(m => m[1]);
  assert.equal(RECORDING_KEYS.length, 6);
  assert.deepEqual([...RECORDING_KEYS].sort(), recordingKeys.sort());
  for (const key of ['split_excl_frac', 'split_offset_max_frac']) assert.ok(recordingKeys.includes(key), key);
  assert.deepEqual(RECORDING_KEYS.filter(k => !LABELS[k]), []);
});

test('观察者机身只列和种类配得上的;种类混抽时定不了,返回空', () => {
  const schema = {bodies: {humanoid: {script_kind: 'humanoid'}, quadruped_cam: {script_kind: 'observer_embodied'}, camera: {script_kind: 'observer_virtual'}}} as unknown as GenSchema;
  assert.deepEqual(observerBodies(schema, 'embodied'), ['quadruped_cam']);
  assert.deepEqual(observerBodies(schema, {dist: 'fixed', value: 'virtual_bound'}), ['camera']);
  assert.deepEqual(observerBodies(schema, {dist: 'choice', values: ['embodied']}), ['quadruped_cam']);
  assert.deepEqual(observerBodies(schema, {dist: 'choice', values: ['embodied', 'virtual_bound'], weights: [2, 1]}), []);
  for (const key of ['body', 'profile', 'calibration', 'eye_height_m']) assert.ok(LABELS[key], key);
});