Files
Genarrative/scripts/check-npm-workspaces.test.mjs
T
kdletters 4a46f89c9b
Project CI / Repository checks (push) Successful in 2m45s
Project CI / Frontend tests (push) Successful in 3m27s
Project CI / Backend tests (push) Successful in 6m18s
Project CI / Native shell tests (push) Failing after 13m52s
接入 AGC 内置插件宿主并补齐 Cocos 编辑器能力 (#338)
客户端新增随包提供的插件宿主和 Cocos Creator 集成:识别并导入 Cocos 项目,通过内置桥接操作已打开的编辑器,无需安装项目 MCP 扩展。DirectProject 现在公开 36 个独立 cocos_* 工具,保留通用 JavaScript 执行入口。

- 通用插件 SDK、命令/能力/面板注册、编辑器适配器和跨进程内置插件开关。
- Cocos 场景、节点、组件、Prefab、UI、Layout/Widget、资源、保存、撤销、日志与预览调试;目录和实现由 JS/native 共用。
- 编辑事务回读、失败回滚、后续手动修改保护及不确定结果禁止重放;预览截图通过 MCP image 返回。
- DirectProject 跳过无关专业 Agent 历史,将项目打开和历史读取中的同步 I/O 移出窗口线程,消除 Cocos 执行与项目文件锁的错误耦合。

验证:
- 合并 master 后:类型/配置检查、编码检查、Rust 格式检查和提交钩子通过。
- 合并 master 后:Cocos 项目打开、插件面板和开发启动定向测试 10 通过、2 跳过;DirectProject MCP 测试 17 通过、1 项真实 Creator opt-in 忽略;插件宿主测试 9/9。
- 插件行为测试 17/17;native 测试 20/20,4 项 opt-in 测试默认忽略。
- 真实 Creator 3.8.8 的 36/36 操作 smoke,以及客户端 MCP tools/list、tools/call、UI/撤销和预览截图,在功能实现阶段已验证通过;本次 master 合并后未重复真实 GUI smoke。

验证边界:发行安装包和远端 CI 尚未验收。

Reviewed-on: #338
Co-authored-by: kdletters <kdletters@qq.com>
Co-committed-by: kdletters <kdletters@qq.com>
2026-09-13 14:48:55 +08:00

174 lines
6.0 KiB
JavaScript

import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { afterEach, test } from 'node:test';
import {
collectNpmWorkspaceErrors,
REQUIRED_PACKAGE_MANAGER,
REQUIRED_WORKSPACES,
} from './check-npm-workspaces.mjs';
const fixtureDirectories = [];
const workspaceNames = {
'apps/admin-web': '@genarrative/admin-web',
'apps/ai-game-creator-shell': '@genarrative/ai-game-creator-shell',
'apps/desktop-shell': '@genarrative/desktop-shell',
'apps/mobile-shell': '@genarrative/mobile-shell',
'apps/preview-deployer-web': '@genarrative/preview-deployer-web',
'packages/image-canvas-core': '@genarrative/image-canvas-core',
'packages/image-canvas-react': '@genarrative/image-canvas-react',
'packages/agc-plugin-sdk': '@genarrative/agc-plugin-sdk',
'packages/shared': '@genarrative/shared',
'plugins/agc-cocos-editor': '@genarrative/agc-plugin-cocos-editor',
'tools/spine-json-export-validator':
'@genarrative/spine-json-export-validator',
};
const localDependencies = {
'apps/admin-web': { '@genarrative/shared': '0.1.0' },
'apps/ai-game-creator-shell': {
'@genarrative/image-canvas-core': '0.1.0',
'@genarrative/image-canvas-react': '0.1.0',
'@genarrative/shared': '0.1.0',
},
'apps/mobile-shell': { '@genarrative/shared': '0.1.0' },
'packages/image-canvas-react': { '@genarrative/image-canvas-core': '0.1.0' },
'plugins/agc-cocos-editor': { '@genarrative/agc-plugin-sdk': '0.1.0' },
};
afterEach(() => {
for (const fixtureDirectory of fixtureDirectories.splice(0)) {
fs.rmSync(fixtureDirectory, { recursive: true, force: true });
}
});
function writeJson(rootDir, relativePath, value) {
const absolutePath = path.join(rootDir, relativePath);
fs.mkdirSync(path.dirname(absolutePath), { recursive: true });
fs.writeFileSync(absolutePath, `${JSON.stringify(value, null, 2)}\n`);
}
function createValidFixture() {
const rootDir = fs.mkdtempSync(
path.join(os.tmpdir(), 'genarrative-npm-workspaces-'),
);
fixtureDirectories.push(rootDir);
writeJson(rootDir, 'package.json', {
name: 'fixture-root',
private: true,
version: '0.0.0',
packageManager: REQUIRED_PACKAGE_MANAGER,
workspaces: REQUIRED_WORKSPACES,
dependencies: {
'@genarrative/image-canvas-core': '0.1.0',
'@genarrative/image-canvas-react': '0.1.0',
'@genarrative/shared': '0.1.0',
},
});
const packages = {
'': {
name: 'fixture-root',
version: '0.0.0',
workspaces: REQUIRED_WORKSPACES,
},
};
for (const workspacePath of REQUIRED_WORKSPACES) {
const manifest = {
name: workspaceNames[workspacePath],
private: true,
version:
workspacePath === 'apps/ai-game-creator-shell' ? '0.1.12' : '0.1.0',
dependencies: localDependencies[workspacePath],
};
writeJson(rootDir, `${workspacePath}/package.json`, manifest);
packages[workspacePath] = manifest;
packages[`node_modules/${manifest.name}`] = {
resolved: workspacePath,
link: true,
};
}
writeJson(rootDir, 'package-lock.json', {
name: 'fixture-root',
version: '0.0.0',
lockfileVersion: 3,
requires: true,
packages,
});
return rootDir;
}
function readJson(rootDir, relativePath) {
return JSON.parse(fs.readFileSync(path.join(rootDir, relativePath), 'utf8'));
}
test('accepts the fixed npm workspace topology and root lock links', () => {
const rootDir = createValidFixture();
assert.deepEqual(collectNpmWorkspaceErrors(rootDir), []);
});
test('rejects workspace protocol and non-exact local package versions', () => {
const rootDir = createValidFixture();
const manifestPath = 'apps/admin-web/package.json';
const manifest = readJson(rootDir, manifestPath);
manifest.dependencies['@genarrative/shared'] = 'workspace:*';
writeJson(rootDir, manifestPath, manifest);
const errors = collectNpmWorkspaceErrors(rootDir).join('\n');
assert.match(errors, /must not use the unsupported workspace: protocol/u);
assert.match(errors, /must use exact version 0\.1\.0/u);
});
test('rejects missing workspace entries and links in the root lock', () => {
const rootDir = createValidFixture();
const lockfile = readJson(rootDir, 'package-lock.json');
delete lockfile.packages['apps/mobile-shell'];
delete lockfile.packages['node_modules/@genarrative/mobile-shell'];
writeJson(rootDir, 'package-lock.json', lockfile);
const errors = collectNpmWorkspaceErrors(rootDir).join('\n');
assert.match(errors, /packages\["apps\/mobile-shell"\]/u);
assert.match(errors, /node_modules\/@genarrative\/mobile-shell/u);
});
test('rejects nested package locks and shrinkwrap files', () => {
const rootDir = createValidFixture();
writeJson(rootDir, 'apps/admin-web/package-lock.json', {});
writeJson(rootDir, 'packages/shared/fixtures/npm-shrinkwrap.json', {});
const errors = collectNpmWorkspaceErrors(rootDir).join('\n');
assert.match(errors, /apps\/admin-web\/package-lock\.json/u);
assert.match(errors, /packages\/shared\/fixtures\/npm-shrinkwrap\.json/u);
});
test('rejects package manager and explicit workspace list drift', () => {
const rootDir = createValidFixture();
const rootPackage = readJson(rootDir, 'package.json');
rootPackage.packageManager = 'npm@11.0.0';
rootPackage.workspaces = ['apps/*', 'packages/*'];
writeJson(rootDir, 'package.json', rootPackage);
const errors = collectNpmWorkspaceErrors(rootDir).join('\n');
assert.match(errors, /packageManager must be npm@10\.9\.7/u);
assert.match(errors, /workspaces must be the explicit ordered list/u);
});
test('rejects test and build aliases tied to a nested workspace install', () => {
const rootDir = createValidFixture();
fs.writeFileSync(
path.join(rootDir, 'vitest.config.ts'),
"const cubone = 'apps/ai-game-creator-shell/node_modules/@cubone/react-file-manager';\n",
);
const errors = collectNpmWorkspaceErrors(rootDir).join('\n');
assert.match(
errors,
/vitest\.config\.ts: resolve dependencies from the workspace manifest/u,
);
});