Files
Genarrative/scripts/check-npm-workspaces.test.mjs
T
Git Hooks Test 5460c13216
Project CI / Backend tests (push) Failing after 5m11s
Project CI / Native shell tests (push) Failing after 6m28s
Project CI / Repository checks (push) Successful in 3m37s
Project CI / Frontend tests (push) Successful in 4m3s
提升 AGC 版本至 0.1.12
同步 npm、Tauri、Cargo 与锁文件版本

更新 workspace 和 release 版本校验
2026-08-28 21:44:20 +08:00

171 lines
5.8 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/shared': '@genarrative/shared',
'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' },
};
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,
);
});