修复AI游戏创作壳跨平台启动 (#107)
修复 macOS 下 Unix 文件身份比较和临时目录测试兼容 隔离 AI 游戏创作本地数据库与发布身份并阻止旧 schema 降级启动 完善 Tauri 开发栈错误传播和 POSIX 子进程树清理 跳过 macOS 不支持的进程指标回调以消除周期告警 补充开发调度测试、技术方案和团队排障记忆 Reviewed-on: https://git.genarrative.world/git/GenarrativeAI/Genarrative/pulls/107 Co-authored-by: menghao <mh18530625731@163.com> Co-committed-by: menghao <mh18530625731@163.com>
This commit was merged in pull request #107.
This commit is contained in:
+117
-25
@@ -11,7 +11,7 @@ import {
|
||||
writeFileSync,
|
||||
} from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { dirname, join, resolve } from 'node:path';
|
||||
|
||||
import { afterEach, describe, expect, test, vi } from 'vitest';
|
||||
|
||||
@@ -510,9 +510,12 @@ describe('dev scheduler stack state file', () => {
|
||||
|
||||
const snapshot = buildDevStackSnapshot(runner, updatedAt);
|
||||
|
||||
expect(snapshot.schemaVersion).toBe(1);
|
||||
expect(snapshot.schemaVersion).toBe(2);
|
||||
expect(snapshot.command).toBe('web');
|
||||
expect(snapshot.database).toBe('genarrative-test');
|
||||
expect(snapshot.spacetimeDataDir).toBe(
|
||||
resolve('server-rs/.spacetimedb/local/data'),
|
||||
);
|
||||
expect(snapshot.services.web).toMatchObject({
|
||||
status: 'running',
|
||||
pid: 4321,
|
||||
@@ -761,16 +764,20 @@ spacetimedb tool version 2.6.0; spacetimedb-lib version 2.6.0;
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
test('发布 spacetime-module 时忽略 spacetime.json 以免覆盖显式数据库', () => {
|
||||
test('发布 spacetime-module 时使用隔离身份配置并忽略 spacetime.json', () => {
|
||||
const args = buildSpacetimePublishArgs({
|
||||
cliConfigPath: '/tmp/genarrative-cli.toml',
|
||||
database: 'xushi-p4wfr',
|
||||
preserveDatabase: false,
|
||||
server: 'http://127.0.0.1:3101',
|
||||
});
|
||||
|
||||
expect(args).toContain('--no-config');
|
||||
expect(args).not.toContain('--anonymous');
|
||||
expect(args).toEqual(
|
||||
expect.arrayContaining([
|
||||
'--config-path',
|
||||
'/tmp/genarrative-cli.toml',
|
||||
'publish',
|
||||
'xushi-p4wfr',
|
||||
'--server',
|
||||
@@ -780,6 +787,17 @@ spacetimedb tool version 2.6.0; spacetimedb-lib version 2.6.0;
|
||||
);
|
||||
});
|
||||
|
||||
test('远程 SpacetimeDB 发布继续使用默认登录身份', () => {
|
||||
const args = buildSpacetimePublishArgs({
|
||||
database: 'xushi-p4wfr',
|
||||
preserveDatabase: true,
|
||||
server: 'https://spacetime.example.com',
|
||||
});
|
||||
|
||||
expect(args).not.toContain('--anonymous');
|
||||
expect(args).not.toContain('--config-path');
|
||||
});
|
||||
|
||||
test('手动刷新 spacetime 只重新发布模块,不重启 standalone 进程', async () => {
|
||||
const { explicitOptions, options } = parseArgs([], {});
|
||||
const runner = new DevRunner(options, {}, explicitOptions);
|
||||
@@ -812,26 +830,18 @@ spacetimedb tool version 2.6.0; spacetimedb-lib version 2.6.0;
|
||||
expect(runner.publishSpacetimeModule).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('本地 API identity 路径同时绑定 data dir 和规范化 server', () => {
|
||||
test('本地 API identity 路径只绑定 data dir', () => {
|
||||
const first = resolveLocalSpacetimeApiIdentityPath(
|
||||
'/tmp/genarrative-data-a',
|
||||
'http://127.0.0.1:3101',
|
||||
);
|
||||
const normalizedEquivalent = resolveLocalSpacetimeApiIdentityPath(
|
||||
const sameDataDir = resolveLocalSpacetimeApiIdentityPath(
|
||||
'/tmp/genarrative-data-a',
|
||||
'http://127.0.0.1:3101/',
|
||||
);
|
||||
const otherServer = resolveLocalSpacetimeApiIdentityPath(
|
||||
'/tmp/genarrative-data-a',
|
||||
'http://127.0.0.1:3102',
|
||||
);
|
||||
const otherDataDir = resolveLocalSpacetimeApiIdentityPath(
|
||||
'/tmp/genarrative-data-b',
|
||||
'http://127.0.0.1:3101',
|
||||
);
|
||||
|
||||
expect(normalizedEquivalent).toBe(first);
|
||||
expect(otherServer).not.toBe(first);
|
||||
expect(sameDataDir).toBe(first);
|
||||
expect(otherDataDir).not.toBe(first);
|
||||
expect(first).toContain(join('genarrative-data-a', 'dev-api-identities'));
|
||||
});
|
||||
@@ -858,10 +868,7 @@ spacetimedb tool version 2.6.0; spacetimedb-lib version 2.6.0;
|
||||
|
||||
await firstRunner.ensureApiServerSpacetimeToken();
|
||||
|
||||
const identityPath = resolveLocalSpacetimeApiIdentityPath(
|
||||
tempDir,
|
||||
firstRunner.state.spacetimeServer,
|
||||
);
|
||||
const identityPath = resolveLocalSpacetimeApiIdentityPath(tempDir);
|
||||
expect(firstRunner.spacetimeApiToken).toBe('local-api-token');
|
||||
expect(firstRunner.baseEnv.GENARRATIVE_SPACETIME_TOKEN).toBeUndefined();
|
||||
expect(globalThis.fetch).toHaveBeenCalledWith(
|
||||
@@ -869,8 +876,8 @@ spacetimedb tool version 2.6.0; spacetimedb-lib version 2.6.0;
|
||||
expect.objectContaining({ method: 'POST' }),
|
||||
);
|
||||
expect(JSON.parse(readFileSync(identityPath, 'utf8'))).toMatchObject({
|
||||
schemaVersion: 1,
|
||||
server: 'http://127.0.0.1:3101',
|
||||
schemaVersion: 2,
|
||||
scope: 'local-data-dir',
|
||||
identity: 'c200localidentity',
|
||||
token: 'local-api-token',
|
||||
});
|
||||
@@ -880,7 +887,7 @@ spacetimedb tool version 2.6.0; spacetimedb-lib version 2.6.0;
|
||||
}
|
||||
|
||||
const secondRunner = new DevRunner(options, {}, explicitOptions);
|
||||
secondRunner.state.spacetimeServer = 'http://127.0.0.1:3101';
|
||||
secondRunner.state.spacetimeServer = 'http://127.0.0.1:3199';
|
||||
globalThis.fetch = vi.fn();
|
||||
|
||||
await secondRunner.ensureApiServerSpacetimeToken();
|
||||
@@ -899,6 +906,94 @@ spacetimedb tool version 2.6.0; spacetimedb-lib version 2.6.0;
|
||||
}
|
||||
});
|
||||
|
||||
test('旧端口作用域 API identity 会迁移为 data dir 作用域', async () => {
|
||||
const tempDir = mkdtempSync(join(tmpdir(), 'genarrative-api-identity-'));
|
||||
try {
|
||||
const legacyServer = 'http://127.0.0.1:3101';
|
||||
const legacyKey = createHash('sha256').update(legacyServer).digest('hex');
|
||||
const legacyPath = join(
|
||||
tempDir,
|
||||
'dev-api-identities',
|
||||
`${legacyKey}.json`,
|
||||
);
|
||||
mkdirSync(dirname(legacyPath), { recursive: true });
|
||||
writeFileSync(
|
||||
legacyPath,
|
||||
`${JSON.stringify({
|
||||
schemaVersion: 1,
|
||||
server: legacyServer,
|
||||
identity: 'legacy-owner-identity',
|
||||
token: 'legacy-owner-token',
|
||||
})}\n`,
|
||||
{ mode: 0o600 },
|
||||
);
|
||||
const { explicitOptions, options } = parseArgs(
|
||||
['--spacetime-data-dir', tempDir],
|
||||
{},
|
||||
);
|
||||
const runner = new DevRunner(options, {}, explicitOptions);
|
||||
runner.state.spacetimeServer = 'http://127.0.0.1:3199';
|
||||
globalThis.fetch = vi.fn();
|
||||
|
||||
await runner.ensureApiServerSpacetimeToken();
|
||||
|
||||
expect(runner.spacetimeApiToken).toBe('legacy-owner-token');
|
||||
expect(globalThis.fetch).not.toHaveBeenCalled();
|
||||
expect(
|
||||
JSON.parse(
|
||||
readFileSync(resolveLocalSpacetimeApiIdentityPath(tempDir), 'utf8'),
|
||||
),
|
||||
).toMatchObject({
|
||||
schemaVersion: 2,
|
||||
scope: 'local-data-dir',
|
||||
identity: 'legacy-owner-identity',
|
||||
});
|
||||
} finally {
|
||||
rmSync(tempDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('同一 data dir 存在多个旧 identity 时失败关闭', async () => {
|
||||
const tempDir = mkdtempSync(join(tmpdir(), 'genarrative-api-identity-'));
|
||||
try {
|
||||
for (const [port, identity] of [
|
||||
[3101, 'legacy-owner-a'],
|
||||
[3199, 'legacy-owner-b'],
|
||||
] as const) {
|
||||
const server = `http://127.0.0.1:${port}`;
|
||||
const legacyPath = join(
|
||||
tempDir,
|
||||
'dev-api-identities',
|
||||
`${createHash('sha256').update(server).digest('hex')}.json`,
|
||||
);
|
||||
mkdirSync(dirname(legacyPath), { recursive: true });
|
||||
writeFileSync(
|
||||
legacyPath,
|
||||
`${JSON.stringify({
|
||||
schemaVersion: 1,
|
||||
server,
|
||||
identity,
|
||||
token: `${identity}-token`,
|
||||
})}\n`,
|
||||
{ mode: 0o600 },
|
||||
);
|
||||
}
|
||||
const { explicitOptions, options } = parseArgs(
|
||||
['--spacetime-data-dir', tempDir],
|
||||
{},
|
||||
);
|
||||
const runner = new DevRunner(options, {}, explicitOptions);
|
||||
globalThis.fetch = vi.fn();
|
||||
|
||||
await expect(runner.ensureApiServerSpacetimeToken()).rejects.toThrow(
|
||||
'无法安全判断数据库 owner',
|
||||
);
|
||||
expect(globalThis.fetch).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
rmSync(tempDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('外部显式 token 优先于已持久化的本地 API identity', async () => {
|
||||
const tempDir = mkdtempSync(join(tmpdir(), 'genarrative-api-identity-'));
|
||||
const originalToken = process.env.GENARRATIVE_SPACETIME_TOKEN;
|
||||
@@ -954,10 +1049,7 @@ spacetimedb tool version 2.6.0; spacetimedb-lib version 2.6.0;
|
||||
);
|
||||
const runner = new DevRunner(options, {}, explicitOptions);
|
||||
runner.state.spacetimeServer = 'http://127.0.0.1:3101';
|
||||
const identityPath = resolveLocalSpacetimeApiIdentityPath(
|
||||
tempDir,
|
||||
runner.state.spacetimeServer,
|
||||
);
|
||||
const identityPath = resolveLocalSpacetimeApiIdentityPath(tempDir);
|
||||
mkdirSync(dirname(identityPath), { recursive: true });
|
||||
const linkedRecordPath = join(tempDir, 'linked-api-identity.json');
|
||||
writeFileSync(
|
||||
|
||||
Reference in New Issue
Block a user