-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopen-source.test.ts
More file actions
202 lines (185 loc) · 8.35 KB
/
Copy pathopen-source.test.ts
File metadata and controls
202 lines (185 loc) · 8.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { describe, expect, test } from "bun:test";
import { existsSync, lstatSync, readFileSync, statSync } from "node:fs";
import { dirname, resolve } from "node:path";
const root = resolve(import.meta.dirname, "..");
/** 可安全 readFileSync 的路径:存在且最终目标是普通文件(跳过目录、目录符号链接)。 */
function isReadableRegularFile(absolute: string): boolean {
try {
const link = lstatSync(absolute);
if (link.isSymbolicLink()) {
return statSync(absolute).isFile();
}
return link.isFile();
} catch {
return false;
}
}
function publicWorktreeFiles(): string[] {
return output(["git", "ls-files", "--cached", "--others", "--exclude-standard"])
.trim()
.split("\n")
.filter((file) => file && isReadableRegularFile(resolve(root, file)));
}
function publicWorktreeFilesMatching(predicate: (file: string) => boolean): string[] {
return publicWorktreeFiles().filter(predicate);
}
function isVisiblePath(file: string): boolean {
return file.split("/").every((segment) => !segment.startsWith("."));
}
function output(command: string[]): string {
const result = Bun.spawnSync(command, { cwd: root, stdout: "pipe", stderr: "pipe" });
if (result.exitCode !== 0) throw new Error(result.stderr.toString());
return result.stdout.toString();
}
describe("open-source repository invariants", () => {
test("public packages share one version for on-demand Playground resolution", () => {
const packagePaths = ["packages/sdk/package.json", "packages/playground/package.json", "packages/cli/package.json"];
const manifests = packagePaths.map(
(file) => JSON.parse(readFileSync(resolve(root, file), "utf8")) as { name: string; version: string },
);
expect(new Set(manifests.map((manifest) => manifest.version)).size).toBe(1);
const changesets = JSON.parse(readFileSync(resolve(root, ".changeset/config.json"), "utf8")) as {
fixed: string[][];
};
expect(changesets.fixed).toContainEqual(manifests.map((manifest) => manifest.name));
});
test("public packages require a maintained Node.js baseline", () => {
const nodeBaselines: Record<string, string> = {
sdk: ">=18.17.0",
playground: ">=22",
cli: ">=22",
};
for (const [pkg, expectedNodeRange] of Object.entries(nodeBaselines)) {
const manifest = JSON.parse(readFileSync(resolve(root, `packages/${pkg}/package.json`), "utf8")) as {
engines?: { node?: string };
};
expect(manifest.engines?.node).toBe(expectedNodeRange);
}
});
test("published packages have no runtime dependency on private workspaces", () => {
const packageFiles = publicWorktreeFilesMatching(
(file) => isVisiblePath(file) && (file === "package.json" || file.endsWith("/package.json")),
);
const manifests = packageFiles.map(
(file) =>
JSON.parse(readFileSync(resolve(root, file), "utf8")) as {
name?: string;
private?: boolean;
dependencies?: Record<string, string>;
optionalDependencies?: Record<string, string>;
peerDependencies?: Record<string, string>;
},
);
const privateNames = new Set(
manifests.filter((manifest) => manifest.private && manifest.name).map((manifest) => manifest.name as string),
);
const invalid: string[] = [];
for (const manifest of manifests.filter((entry) => entry.name?.startsWith("@openagentpack/") && !entry.private)) {
for (const dependencies of [manifest.dependencies, manifest.optionalDependencies, manifest.peerDependencies]) {
for (const name of Object.keys(dependencies ?? {})) {
if (privateNames.has(name)) invalid.push(`${manifest.name} -> ${name}`);
}
}
}
expect(invalid).toEqual([]);
});
test("workspace identity matches the lockfile", () => {
const manifest = JSON.parse(readFileSync(resolve(root, "package.json"), "utf8")) as { name: string };
const lockfile = readFileSync(resolve(root, "bun.lock"), "utf8");
expect(lockfile).toContain(`"name": "${manifest.name}"`);
expect(lockfile).not.toContain('"name": "open-cma-workspace"');
});
test("generated internal artifacts are absent", () => {
expect(existsSync(resolve(root, "apps/webui/outputs"))).toBe(false);
expect(publicWorktreeFiles().filter((file) => file.startsWith("apps/webui/work/"))).toEqual([]);
for (const pkg of ["sdk", "cli", "playground"]) {
expect(existsSync(resolve(root, `packages/${pkg}/LICENSE`))).toBe(false);
}
});
test("the vendored skill archive carries the project license", () => {
const listing = output(["unzip", "-Z1", "examples/qoder/bailian-cli/skills/bailian-cli.zip"]);
expect(listing.split("\n")).toContain("LICENSE");
});
test("all local Markdown links resolve", () => {
const files = publicWorktreeFilesMatching((file) => isVisiblePath(file) && file.endsWith(".md"));
const missing: string[] = [];
for (const file of files) {
const body = readFileSync(resolve(root, file), "utf8");
for (const match of body.matchAll(/\]\(([^)]+)\)/g)) {
let target = match[1].split("#")[0].split("?")[0].replace(/^<|>$/g, "");
if (!target || /^(?:https?:|mailto:)/.test(target)) continue;
target = decodeURIComponent(target);
if (!existsSync(resolve(root, dirname(file), target))) missing.push(`${file} -> ${target}`);
}
}
expect(missing).toEqual([]);
});
test("third-party GitHub Actions are pinned to full commit SHAs", () => {
const workflows = publicWorktreeFilesMatching(
(file) => file.startsWith(".github/workflows/") && (file.endsWith(".yml") || file.endsWith(".yaml")),
);
const unpinned: string[] = [];
for (const file of workflows) {
const body = readFileSync(resolve(root, file), "utf8");
for (const match of body.matchAll(/uses:\s*[^@\s]+@([^\s#]+)/g)) {
if (!/^[0-9a-f]{40}$/.test(match[1])) unpinned.push(`${file} -> ${match[1]}`);
}
}
expect(unpinned).toEqual([]);
});
test("npm publishing auto-starts stable releases, remains approval-gated, and cannot be cancelled", () => {
const workflow = readFileSync(resolve(root, ".github/workflows/release.yml"), "utf8");
expect(workflow).toMatch(/\n {2}push:\n {4}branches: \[main\]/);
expect(workflow).toContain("contains(github.event.head_commit.message, 'chore: release packages')");
expect(workflow).toContain("environment: npm-release");
expect(workflow).toContain("vars.NPM_RELEASE_ENABLED == 'true'");
expect(workflow).toContain("inputs.confirm == 'PUBLISH'");
expect(workflow).toContain(["dist-tag: $", "{{ steps.release.outputs.dist-tag }}"].join(""));
expect(workflow).toContain('bun run release:publish -- --tag "$NPM_DIST_TAG"');
expect(workflow).toContain("id-token: write");
expect(workflow).toContain("cancel-in-progress: false");
expect(workflow).toContain("workflow_dispatch:");
expect(workflow).toContain('git config user.name "github-actions[bot]"');
expect(workflow).toContain("github-actions[bot]@users.noreply.github.com");
expect(workflow).toContain("registry-ready:");
expect(workflow).toMatch(/registry-ready:[\s\S]*?timeout-minutes: 20/);
expect(workflow).toContain("post-release-consumer:");
expect(workflow).toContain("os: [ubuntu-latest, windows-latest, macos-latest]");
expect(workflow).toContain("node: [22, 24]");
expect(workflow).toContain("needs: [preflight, publish, post-release-consumer]");
const consumerGate = workflow.indexOf("post-release-consumer:");
const finalize = workflow.indexOf("finalize-release:");
expect(consumerGate).toBeGreaterThan(-1);
expect(finalize).toBeGreaterThan(consumerGate);
});
test("public worktree has no high-confidence secrets or internal machine references", () => {
const findings: string[] = [];
const patterns = [
/github_pat_[A-Za-z0-9_]{20,}/,
/gh[pousr]_[A-Za-z0-9_]{20,}/,
/AKIA[0-9A-Z]{16}/,
/npm_[A-Za-z0-9]{20,}/,
/-----BEGIN (?:RSA |EC |OPENSSH )?PRIVATE KEY-----/,
/\/Users\/[A-Za-z0-9._-]+\//,
new RegExp(["registry", "anpm", "alibaba-inc", "com"].join("\\.")),
new RegExp(["gitlab", "alibaba-inc", "com"].join("\\.")),
];
for (const file of publicWorktreeFiles()) {
if (file === "scripts/open-source.test.ts") continue;
const absolute = resolve(root, file);
let body: string;
try {
body = readFileSync(absolute, "utf8");
} catch (error) {
// 竞态或残留目录项:跳过不可读路径
if ((error as NodeJS.ErrnoException).code === "EISDIR") continue;
throw error;
}
if (body.includes("\0")) continue;
for (const pattern of patterns) {
if (pattern.test(body)) findings.push(`${file} -> ${pattern.source}`);
}
}
expect(findings).toEqual([]);
});
});