2acf55cf67
- 保留统一 POST /api/game-distribution/versions 与旧两步 POST /games、POST /games/{gameId}/versions 两支发布路径并存。
- GameMetadata 恢复 forkAuthorization/fork;fork 标为可选,渲染层不提交,改由原生链路从项目内 .agent/fork-source.json 读取。
- NewGameVersionRequest 增加可选 changeSummary,统一发布事务按血缘行判定衍生后校验并落库,母版忽略;网页 gamePublishSubmission 与 AGC 原生发布均透传。
- 恢复 resolve_version_number(max_existing, requested) 与 VersionNumberExhausted,以及旧两步 procedure、路由、DTO 与 spacetime-client facade 方法。
- 恢复 GAME_DISTRIBUTION_ACTION_CREATE_GAME 常量与旧 create_version 收据 action 常量。
- 恢复 GameDistributionCreateGameRequest、GameDistributionCreateVersionRequest 与 From<&...> for GameMetadata,并在 DTO parity 中登记两组。
- local_project_id → project_key 为模块未上线前的硬切改名,继续按 SPACETIME_SCHEMA_GUARD_ALLOW_BREAKING=1 走窗口。
- 同步 decision-log 合并处理记录。
- 验证:cargo check --workspace --all-targets;cargo test -p module-game-distribution、shared-contracts、spacetime-client、spacetime-module game_distribution、api-server game_distribution;AGC cargo test -- game_fork;check:generated-bindings、check:game-distribution-dto-parity、check:spacetime-schema、check:encoding、git diff --check;网页与 AGC 定向 vitest 全绿。
309 lines
9.3 KiB
TypeScript
309 lines
9.3 KiB
TypeScript
import { EventEmitter } from 'node:events';
|
|
import { createServer } from 'node:http';
|
|
import type { AddressInfo } from 'node:net';
|
|
import { resolve } from 'node:path';
|
|
|
|
import { describe, expect, test } from 'vitest';
|
|
|
|
import {
|
|
agcDatabase,
|
|
agcDataDir,
|
|
buildDevAllBaseEnvironment,
|
|
buildDevAllEnvironment,
|
|
buildLocalSchemaConflictHint,
|
|
buildManagedSpawnOptions,
|
|
buildRootDevArgs,
|
|
buildStartupSummary,
|
|
isHttpReady,
|
|
isLocalSchemaConflictOutput,
|
|
isSharedRootStackReady,
|
|
renderStartupTable,
|
|
runDevAll,
|
|
validateDevAllArgs,
|
|
} from './dev-all.mjs';
|
|
type FakeChild = EventEmitter & {
|
|
pid: number;
|
|
exitCode: number | null;
|
|
signalCode: string | null;
|
|
};
|
|
|
|
function makeReadyState(root = resolve('fixture-root')) {
|
|
const instanceId = 'dev-fixture';
|
|
const service = (status = 'running', url = 'http://127.0.0.1:1') => ({
|
|
repoRoot: root,
|
|
instanceId,
|
|
status,
|
|
url,
|
|
});
|
|
|
|
return {
|
|
command: 'all',
|
|
instanceId,
|
|
repoRoot: root,
|
|
database: agcDatabase,
|
|
spacetimeDataDir: agcDataDir,
|
|
services: {
|
|
spacetime: service('reused', 'http://127.0.0.1:3101'),
|
|
'api-server': service('running', 'http://127.0.0.1:8082'),
|
|
'bgfilter-worker': service('running', 'http://127.0.0.1:8083'),
|
|
web: service('running', 'http://127.0.0.1:3000'),
|
|
'admin-web': service('running', 'http://127.0.0.1:3102/admin/'),
|
|
},
|
|
};
|
|
}
|
|
|
|
describe('dev:all 参数和共享开发栈合同', () => {
|
|
test('根 dev 固定使用 AGC 配套数据库,并保留调用方选项', () => {
|
|
expect(buildRootDevArgs(['--watch', '--api-port', '9082'])).toEqual([
|
|
'run',
|
|
'dev',
|
|
'--',
|
|
'--database',
|
|
agcDatabase,
|
|
'--spacetime-data-dir',
|
|
'server-rs/.spacetimedb/ai-game-creator/data',
|
|
'--preserve-database',
|
|
'--no-interactive',
|
|
'--watch',
|
|
'--api-port',
|
|
'9082',
|
|
]);
|
|
});
|
|
|
|
test('拒绝会破坏单后端拓扑的数据库和 data dir 覆盖', () => {
|
|
expect(() => validateDevAllArgs(['--database', 'other'])).toThrow(
|
|
'不能覆盖 --database 或 --spacetime-data-dir',
|
|
);
|
|
expect(() => validateDevAllArgs(['--database=other'])).toThrow(
|
|
'不能覆盖 --database 或 --spacetime-data-dir',
|
|
);
|
|
expect(() => validateDevAllArgs(['--spacetime-data-dir', 'other'])).toThrow(
|
|
'不能覆盖 --database 或 --spacetime-data-dir',
|
|
);
|
|
expect(() => validateDevAllArgs(['backend'])).toThrow('只接受 dev 选项');
|
|
});
|
|
|
|
test('预留 AGC Vite 端口并关闭 AGC 自带后台', () => {
|
|
const baseEnv = { AGC_DEV_ADMIN_WEB: '1' };
|
|
const env = buildDevAllEnvironment(baseEnv, 3080);
|
|
|
|
expect(env.GENARRATIVE_AGC_VITE_PORT).toBe('3080');
|
|
expect(env.AGC_DEV_ADMIN_WEB).toBe('0');
|
|
expect(baseEnv.AGC_DEV_ADMIN_WEB).toBe('1');
|
|
});
|
|
|
|
test('CLI port-range 覆盖旧环境并同步给 AGC', () => {
|
|
const env = buildDevAllBaseEnvironment(['--port-range', '10100-10199'], {
|
|
GENARRATIVE_DEV_PORT_RANGE: '10000-10099',
|
|
});
|
|
|
|
expect(env.GENARRATIVE_DEV_PORT_RANGE).toBe('10100-10199');
|
|
});
|
|
|
|
test.each([
|
|
[200, true],
|
|
[204, true],
|
|
[299, true],
|
|
[400, false],
|
|
[401, false],
|
|
[403, false],
|
|
[404, false],
|
|
[500, false],
|
|
[503, false],
|
|
])('HTTP %i 的前端就绪结果为 %s', async (status, expected) => {
|
|
const server = createServer((_request, response) => {
|
|
response.writeHead(status).end();
|
|
});
|
|
await new Promise<void>((resolveListen) => {
|
|
server.listen(0, '127.0.0.1', resolveListen);
|
|
});
|
|
|
|
try {
|
|
const { port } = server.address() as AddressInfo;
|
|
await expect(isHttpReady(`http://127.0.0.1:${port}`)).resolves.toBe(
|
|
expected,
|
|
);
|
|
} finally {
|
|
server.closeAllConnections();
|
|
await new Promise<void>((resolveClose, rejectClose) => {
|
|
server.close((error) => {
|
|
if (error) rejectClose(error);
|
|
else resolveClose();
|
|
});
|
|
});
|
|
}
|
|
});
|
|
|
|
test('Windows 下通过 shell 启动 npm.cmd', () => {
|
|
const options = buildManagedSpawnOptions(process.env);
|
|
|
|
expect(options.shell).toBe(process.platform === 'win32');
|
|
expect(options.windowsHide).toBe(process.platform === 'win32');
|
|
expect(options.detached).toBe(process.platform !== 'win32');
|
|
});
|
|
|
|
test('按最终端口构造完整启动摘要表', () => {
|
|
const rows = buildStartupSummary(makeReadyState(), {
|
|
url: 'http://127.0.0.1:3080/',
|
|
});
|
|
|
|
expect(rows).toHaveLength(7);
|
|
expect(rows).toEqual(
|
|
expect.arrayContaining([
|
|
expect.objectContaining({
|
|
项目: 'SpacetimeDB',
|
|
状态: '已复用',
|
|
地址: 'http://127.0.0.1:3101',
|
|
}),
|
|
expect.objectContaining({
|
|
项目: 'Admin 后台',
|
|
地址: 'http://127.0.0.1:3102/admin/',
|
|
}),
|
|
expect.objectContaining({
|
|
项目: 'AGC Vite',
|
|
地址: 'http://127.0.0.1:3080/',
|
|
}),
|
|
]),
|
|
);
|
|
});
|
|
|
|
test('渲染摘要表时不输出字符串引号', () => {
|
|
const rows = buildStartupSummary(makeReadyState(), {
|
|
url: 'http://127.0.0.1:3080/',
|
|
});
|
|
const table = renderStartupTable(rows);
|
|
|
|
expect(table).not.toContain("'");
|
|
expect(table).toContain('| Web 主站');
|
|
expect(table).toContain('http://127.0.0.1:3080/');
|
|
});
|
|
|
|
test('仅接受同一工作树、实例、数据库和 data dir 的完整栈', () => {
|
|
const root = resolve('fixture-root');
|
|
const ready = makeReadyState(root);
|
|
|
|
expect(
|
|
isSharedRootStackReady(ready, {
|
|
expectedRepoRoot: root,
|
|
expectedDatabase: agcDatabase,
|
|
expectedDataDir: agcDataDir,
|
|
}),
|
|
).toBe(true);
|
|
|
|
const wrongInstance = structuredClone(ready);
|
|
wrongInstance.services.web.instanceId = 'other-instance';
|
|
expect(
|
|
isSharedRootStackReady(wrongInstance, {
|
|
expectedRepoRoot: root,
|
|
expectedDatabase: agcDatabase,
|
|
expectedDataDir: agcDataDir,
|
|
}),
|
|
).toBe(false);
|
|
|
|
const incomplete = structuredClone(ready);
|
|
incomplete.services['admin-web'].status = 'idle';
|
|
expect(
|
|
isSharedRootStackReady(incomplete, {
|
|
expectedRepoRoot: root,
|
|
expectedDatabase: agcDatabase,
|
|
expectedDataDir: agcDataDir,
|
|
}),
|
|
).toBe(false);
|
|
});
|
|
|
|
test('任一子进程退出时收束另一侧进程树', async () => {
|
|
const children: FakeChild[] = [];
|
|
const stopCalls: FakeChild[] = [];
|
|
let nextPid = 100;
|
|
const run = runDevAll([], {
|
|
resolveEndpoint: async () => ({
|
|
port: 3080,
|
|
url: 'http://127.0.0.1:3080/',
|
|
}),
|
|
spawnProcess: () => {
|
|
const child = new EventEmitter() as FakeChild;
|
|
child.pid = nextPid;
|
|
child.exitCode = null;
|
|
child.signalCode = null;
|
|
nextPid += 1;
|
|
children.push(child);
|
|
if (children.length === 2) {
|
|
queueMicrotask(() => {
|
|
child.exitCode = 1;
|
|
child.emit('exit', 1, null);
|
|
});
|
|
}
|
|
return child;
|
|
},
|
|
waitForRoot: async () => makeReadyState(),
|
|
readState: () => null,
|
|
waitForAgc: async () => true,
|
|
stopProcessTree: async (child) => {
|
|
stopCalls.push(child);
|
|
if (child?.exitCode == null) {
|
|
child.exitCode = 0;
|
|
child.emit('exit', 0, null);
|
|
}
|
|
},
|
|
});
|
|
|
|
await expect(run).resolves.toBe(1);
|
|
expect(children).toHaveLength(2);
|
|
expect(stopCalls).toEqual([children[1], children[0]]);
|
|
});
|
|
|
|
test('默认仍固定保留 AGC 本地库(不改变默认行为)', () => {
|
|
const args = buildRootDevArgs(['--watch']);
|
|
expect(args).toContain('--preserve-database');
|
|
expect(args).not.toContain('--clear-database');
|
|
});
|
|
|
|
test('显式 --clear-database 时不再固定 --preserve-database,开关逐字透传给根栈', () => {
|
|
const args = buildRootDevArgs(['--clear-database']);
|
|
expect(args).not.toContain('--preserve-database');
|
|
expect(
|
|
args.filter((argument) => argument === '--clear-database'),
|
|
).toHaveLength(1);
|
|
expect(args).toEqual([
|
|
'run',
|
|
'dev',
|
|
'--',
|
|
'--database',
|
|
agcDatabase,
|
|
'--spacetime-data-dir',
|
|
'server-rs/.spacetimedb/ai-game-creator/data',
|
|
'--no-interactive',
|
|
'--clear-database',
|
|
]);
|
|
});
|
|
|
|
test('识别本地库 schema 冲突的服务端输出', () => {
|
|
expect(isLocalSchemaConflictOutput('… needs manual migration')).toBe(true);
|
|
expect(
|
|
isLocalSchemaConflictOutput(
|
|
'Reordering table game_distribution_game requires a manual migration',
|
|
),
|
|
).toBe(true);
|
|
expect(
|
|
isLocalSchemaConflictOutput(
|
|
'Aborting publish due to required manual migration.',
|
|
),
|
|
).toBe(true);
|
|
expect(
|
|
isLocalSchemaConflictOutput('Finished `dev` profile [unoptimized]'),
|
|
).toBe(false);
|
|
expect(isLocalSchemaConflictOutput(undefined)).toBe(false);
|
|
});
|
|
|
|
test('本地库 schema 冲突提示含库名/data dir/两条出路/丢数据警告', () => {
|
|
const hint = buildLocalSchemaConflictHint();
|
|
expect(hint).toContain(agcDatabase);
|
|
expect(hint).toContain('server-rs/.spacetimedb/ai-game-creator/data');
|
|
expect(hint).toContain('npm run dev:all -- --clear-database');
|
|
expect(hint).toContain('不带 --preserve-database');
|
|
expect(hint).toContain('都会丢弃该本地库里的数据');
|
|
expect(hint).toContain('不是代码问题');
|
|
expect(hint).not.toContain('undefined');
|
|
});
|
|
});
|