Files
u-claw/tests/origin.test.mjs
zheng 08f22edf44
All checks were successful
Tests / test (push) Successful in 1m8s
fix(ci): align Node v22.22.3 pins and unblock pre-push tests
Sync install scripts and CI container image with NODE_VERSION, regenerate
Electron Config.html, and skip gitignored portable/app in origin scan.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-18 18:47:41 +08:00

148 lines
7.0 KiB
JavaScript

import { readFileSync, readdirSync, statSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { join, relative, extname } from 'node:path';
import test from 'node:test';
import assert from 'node:assert/strict';
const repoRoot = fileURLToPath(new URL('..', import.meta.url));
const origin = JSON.parse(readFileSync(join(repoRoot, 'origin.json'), 'utf8'));
const SKIPPED_DIRS = new Set(['.git', 'node_modules', 'dist', '.download-cache', 'tests']);
// portable/app/ is gitignored — setup downloads Node/OpenClaw there for local runs.
const SKIPPED_PATH_PREFIXES = ['portable/app/'];
// Lockfiles are full of third-party funding and repository links. They say
// nothing about where *this* build fetches from.
const SKIPPED_FILES = new Set(['package-lock.json']);
const SCANNED = new Set(['.sh', '.ps1', '.bat', '.command', '.mjs', '.js', '.yml', '.yaml', '.json', '.html', '.md']);
function* walk(dir) {
for (const entry of readdirSync(dir)) {
if (SKIPPED_DIRS.has(entry)) continue;
const full = join(dir, entry);
const rel = relative(repoRoot, full);
if (rel === 'portable/app' || SKIPPED_PATH_PREFIXES.some((p) => rel.startsWith(p))) continue;
if (statSync(full).isDirectory()) yield* walk(full);
else yield full;
}
}
test('origin.json describes a coherent origin', () => {
assert.match(origin.repo.owner, /^[\w.-]+$/);
assert.match(origin.repo.name, /^[\w.-]+$/);
// A template, not a base: Gitea and GitHub lay raw paths out differently.
assert.match(origin.urls.rawTemplate, /\{ref\}.*\{path\}/, 'rawTemplate needs both placeholders');
for (const key of ['web', 'rawTemplate', 'releases', 'issues', 'website']) {
assert.match(origin.urls[key], /^https:\/\//, `${key} should be an https URL`);
}
assert.match(origin.urls.ssh, /^ssh:\/\//, 'ssh should be an ssh URL');
});
// This is a fork. Every URL the build fetches at runtime points at somebody
// else's host until we move it. The move is only safe if nothing is hiding in a
// file we forgot about, which is what this test is for.
test('no file points at an owner other than the one origin.json declares', () => {
const declaredOwner = origin.repo.owner;
const OWNER_PATTERN = new RegExp(
`${origin.repo.host.replace(/\./g, '\\.')}\\/([\\w.-]+)\\/|github\\.com\\/([\\w.-]+)\\/`, 'g');
const KNOWN_THIRD_PARTY = new Set([
'openclaw', // upstream runtime, a real dependency
'electron', // electron mirrors
'ventoy', // bootable USB
'dongsheng123132', // the project we forked — referenced, never fetched from
]);
const offenders = [];
for (const file of walk(repoRoot)) {
if (!SCANNED.has(extname(file))) continue;
const rel = relative(repoRoot, file);
if (rel === 'origin.json' || SKIPPED_FILES.has(rel.split('/').pop())) continue;
const content = readFileSync(file, 'utf8');
content.split(/\r?\n/).forEach((line, i) => {
for (const match of line.matchAll(OWNER_PATTERN)) {
const owner = match[1] ?? match[2];
if (owner === declaredOwner || KNOWN_THIRD_PARTY.has(owner)) continue;
offenders.push(`${rel}:${i + 1} points at ${owner}`);
}
});
}
assert.deepEqual(offenders, [], `unexpected owners:\n${offenders.join('\n')}`);
});
// The one-line installers run through curl|bash with no checkout, so they cannot
// read origin.json and carry the same URLs as literals. Without this check a
// migration would update origin.json, look done, and leave curl|bash users
// fetching from the old host.
test('the standalone installers carry URLs that match origin.json', () => {
const expected = {
releases: origin.urls.releases,
website: origin.urls.website,
};
for (const script of ['install/install.sh', 'install/install.ps1']) {
const content = readFileSync(join(repoRoot, script), 'utf8');
const rawPrefix = origin.urls.rawTemplate.split('{ref}')[0];
assert.ok(
!/raw\.githubusercontent\.com/.test(content) || content.includes(rawPrefix),
`${script} still fetches raw content from GitHub`,
);
if (content.includes('install-skills.mjs')) {
assert.ok(content.includes(rawPrefix), `${script} should fetch the installer from ${rawPrefix}`);
}
const releaseUrls = [...content.matchAll(new RegExp(`https://[\\w.-]+/[\\w.-]+/[\\w.-]+/releases`, 'g'))].map((m) => m[0]);
for (const url of releaseUrls) {
assert.equal(url, expected.releases, `${script} downloads from ${url}, origin.json says ${expected.releases}`);
}
const siteUrls = [...content.matchAll(/https:\/\/u-claw\.org|https:\/\/[\w.-]*u-claw[\w.-]*\.\w+/g)].map((m) => m[0]);
for (const url of siteUrls) {
assert.equal(url, expected.website, `${script} points at ${url}, origin.json says ${expected.website}`);
}
}
});
test('install-skills.mjs reads origin.json rather than hardcoding the host', () => {
const content = readFileSync(join(repoRoot, 'lib', 'install-skills.mjs'), 'utf8');
assert.match(content, /origin\.json/, 'it should read origin.json');
// A literal fallback is fine and necessary — the remote installers download
// this file on its own, with no repo around it — but it must agree.
// A literal fallback is necessary — the remote installers download this file on
// its own, with no repo around it — but it must agree with origin.json.
const fallback = content.match(/\?\?\s*'(https:\/\/[^']+)'/)?.[1];
assert.ok(fallback, 'there should be a hardcoded fallback template');
assert.equal(fallback, origin.urls.rawTemplate, 'the fallback template disagrees with origin.json');
});
test('we do not tell users to email a domain we do not own', () => {
// u-claw.org is the upstream project's domain. Any address on it reaches them,
// not us — so a support line pointing there sends our users' problems to
// someone with no reason to answer.
const OURS = new Set(Object.values(origin.upstream?.addresses ?? {}).filter((v) => /@/.test(v)));
const offenders = [];
for (const file of walk(repoRoot)) {
if (!SCANNED.has(extname(file))) continue;
const rel = relative(repoRoot, file);
if (rel === 'origin.json' || SKIPPED_FILES.has(rel.split('/').pop())) continue;
readFileSync(file, 'utf8').split(/\r?\n/).forEach((line, i) => {
for (const [found] of line.matchAll(/[\w.+-]+@u-claw\.org/g)) {
// Upstream's own addresses may appear where the text is about upstream.
if (OURS.has(found)) continue;
offenders.push(`${rel}:${i + 1} offers ${found}, a domain we do not control`);
}
});
}
assert.deepEqual(offenders, [], offenders.join('\n'));
});
test('support routes somewhere we actually control', () => {
assert.match(origin.support.issues, /^https:\/\//, 'there has to be a working support route');
assert.ok(
origin.support.issues.includes(origin.repo.host),
'support should point at our own host, not the upstream project',
);
// Until we have an address of our own, saying so beats leaving a stale one.
assert.ok('email' in origin.support, 'origin.json should record whether we have an address yet');
assert.match(origin.support.note, /do not own u-claw\.org/i);
});