Files
Genarrative/scripts/spacetime-migration-common.test.ts
T
k88936 47743d086d 修复角色动作迁移分页安全
统一 HTTP 对象与 CLI tuple 的批次哈希字段

拒绝迁移 procedure 返回重复分页游标

补充对象返回和循环游标回归测试
2026-08-05 13:25:14 +08:00

178 lines
4.5 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import {
buildSpacetimeCallArgs,
encodeSpacetimeCliOption,
parseProcedureResult,
} from './spacetime-migration-common.mjs';
describe('SpacetimeDB CLI SATS option encoding', () => {
it('keeps absent options null and wraps present values as Some', () => {
expect(encodeSpacetimeCliOption(null)).toBeNull();
expect(encodeSpacetimeCliOption(undefined)).toBeNull();
expect(encodeSpacetimeCliOption('job-2')).toEqual([0, 'job-2']);
expect(encodeSpacetimeCliOption(123)).toEqual([0, 123]);
});
it('serializes non-empty maintenance cursors in the CLI procedure input', () => {
const args = buildSpacetimeCallArgs(
{
database: 'genarrative-prod',
passthrough: [],
serverUrl: 'http://127.0.0.1:3311',
},
'compact_external_generation_job_payloads_and_return',
{
dry_run: true,
limit: 1,
cursor_job_id: encodeSpacetimeCliOption('job-2'),
completed_before_micros: encodeSpacetimeCliOption(123),
},
);
const input = JSON.parse(args.at(-2) ?? 'null');
expect(input.cursor_job_id).toEqual([0, 'job-2']);
expect(input.completed_before_micros).toEqual([0, 123]);
});
it('normalizes editor canvas migration procedure results', () => {
const result = parseProcedureResult(
JSON.stringify([
true,
[
0,
[
'canvas-1',
'project-1',
'source-hash',
'structured-hash',
7,
2,
1,
'resource-hash',
1,
'backfilled',
7,
],
],
[1],
]),
'backfill_editor_canvas_layout_and_return',
);
expect(result).toEqual({
ok: true,
migration: {
canvas_id: 'canvas-1',
project_id: 'project-1',
source_layout_sha256: 'source-hash',
structured_layout_sha256: 'structured-hash',
verified_revision: 7,
layer_count: 2,
dialog_count: 1,
resource_refs_sha256: 'resource-hash',
migration_version: 1,
status: 'backfilled',
revision: 7,
},
error_message: null,
});
});
it('normalizes editor canvas resource repair procedure results', () => {
const result = parseProcedureResult(
JSON.stringify([true, [0, [true, false, 2, 0, 0, 0, 7]], [1]]),
'repair_editor_canvas_resources_and_return',
);
expect(result).toEqual({
ok: true,
repair: {
dry_run: true,
already_repaired: false,
matched_layer_count: 2,
remapped_layer_count: 0,
restored_resource_count: 0,
removed_source_resource_id_count: 0,
revision: 7,
},
error_message: null,
});
});
it('normalizes character action metadata normalization results', () => {
const result = parseProcedureResult(
JSON.stringify([
true,
'asset',
true,
25,
3,
0,
2,
1,
0,
3,
2,
0,
0,
1,
['asset-blocked'],
[0, 'asset-25'],
true,
'a'.repeat(64),
[1],
]),
'normalize_editor_character_animation_metadata_and_return',
);
expect(result).toEqual({
ok: true,
scope: 'asset',
dry_run: true,
scanned_count: 25,
matched_count: 3,
updated_count: 0,
backfilled_frames_count: 2,
backfilled_duration_count: 1,
reclassified_preview_count: 0,
cleaned_generation_inputs_count: 3,
cleaned_frame_index_count: 2,
cleaned_canvas_layer_count: 0,
materialized_resource_count: 0,
blocker_count: 1,
blocker_ids: ['asset-blocked'],
next_cursor: 'asset-25',
has_more: true,
batch_sha256: 'a'.repeat(64),
error_message: null,
});
});
it('normalizes object-shaped character action procedure results', () => {
const result = parseProcedureResult(
JSON.stringify({
ok: true,
scope: 'asset',
dry_run: true,
scanned_count: 25,
matched_count: 3,
updated_count: 0,
blocker_count: 0,
blocker_ids: [],
next_cursor: 'asset-25',
has_more: true,
batch_sha_256: 'b'.repeat(64),
error_message: null,
}),
'normalize_editor_character_animation_metadata_and_return',
);
expect(result).toMatchObject({
batch_sha256: 'b'.repeat(64),
next_cursor: 'asset-25',
});
expect(result).not.toHaveProperty('batch_sha_256');
});
});