@goplusvn/core 0.1.67 → 0.1.69
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +85 -1
- package/bin/goerp-guardrails.mjs +45 -0
- package/eslint/index.mjs +120 -0
- package/package.json +10 -3
- package/scripts/doctor.ts +99 -0
- package/src/guardrails/__tests__/guardrails.test.ts +430 -0
- package/src/guardrails/index.ts +57 -0
- package/src/guardrails/preset.ts +75 -0
- package/src/guardrails/primitives.ts +307 -0
- package/src/guardrails/rules/auth.ts +178 -0
- package/src/guardrails/rules/debt.ts +71 -0
- package/src/guardrails/rules/design.ts +95 -0
- package/src/guardrails/rules/layering.ts +160 -0
- package/src/guardrails/rules/one-door.ts +115 -0
- package/src/guardrails/rules/rbac.ts +282 -0
- package/src/guardrails/rules/safety.ts +86 -0
- package/src/guardrails/rules/structure.ts +136 -0
- package/src/guardrails/run.ts +130 -0
- package/src/guardrails/scanner.ts +144 -0
- package/src/guardrails/types.ts +181 -0
- package/src/types/index.ts +1 -1
- package/src/ui/data-display/shallow-pagination.tsx +189 -0
- package/src/ui/index.tsx +1 -0
- package/src/user/components/index.ts +1 -0
- package/src/user/components/user-toolbar.tsx +8 -2
- package/src/user/components/user-visuals.tsx +84 -0
- package/src/user/components/users-card-view.tsx +1 -26
- package/src/user/components/users-table.tsx +215 -0
- package/src/user/pages/users-client-page.tsx +84 -259
- package/templates/starter-app/AGENTS.md +39 -3
- package/templates/starter-app/eslint.config.mjs +85 -0
- package/templates/starter-app/package.json +19 -2
- package/templates/starter-app/prettier.config.mjs +54 -0
- package/templates/starter-app/src/__tests__/architecture.test.ts +35 -143
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
|
|
5
|
+
import { afterAll, describe, expect, it } from "vitest";
|
|
6
|
+
|
|
7
|
+
import { evaluateGuardrails } from "../preset";
|
|
8
|
+
import { createContext, stripComments } from "../scanner";
|
|
9
|
+
import { forbidPattern, perDirectoryCeiling } from "../primitives";
|
|
10
|
+
import type { GuardrailOptions, GuardrailResult } from "../types";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Test của chính bộ hàng rào.
|
|
14
|
+
*
|
|
15
|
+
* Guardrail sai còn tệ hơn không có: nó bật đèn xanh cho đúng thứ nó được lập
|
|
16
|
+
* ra để chặn. Nên mỗi rule ở đây được kiểm bằng một app giả có vi phạm THẬT,
|
|
17
|
+
* chứ không chỉ kiểm "chạy không nổ".
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
const roots: string[] = [];
|
|
21
|
+
|
|
22
|
+
/** Dựng một app giả trong thư mục tạm: `{ "lib/prisma.ts": "..." }`. */
|
|
23
|
+
function fixture(files: Record<string, string>): string {
|
|
24
|
+
const root = mkdtempSync(join(tmpdir(), "goerp-guardrails-"));
|
|
25
|
+
roots.push(root);
|
|
26
|
+
for (const [rel, content] of Object.entries(files)) {
|
|
27
|
+
const full = join(root, rel.startsWith("src/") ? rel : `src/${rel}`);
|
|
28
|
+
mkdirSync(dirname(full), { recursive: true });
|
|
29
|
+
writeFileSync(full, content);
|
|
30
|
+
}
|
|
31
|
+
return root;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Đặt file ở GỐC app (không phải trong src) — .dockerignore, tailwind.config… */
|
|
35
|
+
function atRoot(root: string, rel: string, content: string) {
|
|
36
|
+
const full = join(root, rel);
|
|
37
|
+
mkdirSync(dirname(full), { recursive: true });
|
|
38
|
+
writeFileSync(full, content);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const resultOf = (results: GuardrailResult[], id: string) =>
|
|
42
|
+
results.find((r) => r.rule.id === id)!;
|
|
43
|
+
|
|
44
|
+
const run = (root: string, options: GuardrailOptions = {}) =>
|
|
45
|
+
evaluateGuardrails({ root, ...options });
|
|
46
|
+
|
|
47
|
+
afterAll(() => {
|
|
48
|
+
for (const root of roots) rmSync(root, { recursive: true, force: true });
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe("scanner", () => {
|
|
52
|
+
it("bỏ chú thích nhưng giữ nguyên URL trong chuỗi", () => {
|
|
53
|
+
const code = stripComments(
|
|
54
|
+
[
|
|
55
|
+
'const a = "http://x.test"',
|
|
56
|
+
"// window.open(export)",
|
|
57
|
+
"/* as any */",
|
|
58
|
+
].join("\n"),
|
|
59
|
+
);
|
|
60
|
+
expect(code).toContain("http://x.test");
|
|
61
|
+
expect(code).not.toContain("window.open");
|
|
62
|
+
expect(code).not.toContain("as any");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("callBodies cắt đúng thân lời gọi trải nhiều dòng", () => {
|
|
66
|
+
const bodies = createContext({ root: fixture({}) }).callBodies(
|
|
67
|
+
"await db.$transaction(async (tx) => {\n await fn(1)\n})\nother()",
|
|
68
|
+
"$transaction(",
|
|
69
|
+
);
|
|
70
|
+
expect(bodies).toHaveLength(1);
|
|
71
|
+
expect(bodies[0]).toContain("await fn(1)");
|
|
72
|
+
expect(bodies[0]).not.toContain("other()");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("không quét file test và thư mục build", () => {
|
|
76
|
+
const ctx = createContext({
|
|
77
|
+
root: fixture({
|
|
78
|
+
"lib/a.ts": "export const a = 1",
|
|
79
|
+
"lib/a.test.ts": "const x: any = 1",
|
|
80
|
+
".next/b.ts": "const y: any = 2",
|
|
81
|
+
}),
|
|
82
|
+
});
|
|
83
|
+
expect(ctx.files.map(ctx.rel)).toEqual(["lib/a.ts"]);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe("rule bắt được vi phạm thật", () => {
|
|
88
|
+
it("one-door/single-prisma-client — client thứ hai bị bắt, cửa chính thì không", () => {
|
|
89
|
+
const root = fixture({
|
|
90
|
+
"lib/prisma.ts": "export const db = new PrismaClient()",
|
|
91
|
+
"server/report.ts": "const other = new PrismaClient()",
|
|
92
|
+
});
|
|
93
|
+
expect(
|
|
94
|
+
resultOf(run(root), "one-door/single-prisma-client").violations,
|
|
95
|
+
).toEqual(["server/report.ts"]);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("one-door/storage — chỉ cửa được import core, và cửa phải cấu hình", () => {
|
|
99
|
+
const leaky = fixture({
|
|
100
|
+
"lib/storage.ts":
|
|
101
|
+
'import { configureStorage } from "@goerp/core/storage"\nconfigureStorage({ bucket: "x" })',
|
|
102
|
+
"app/api/upload/route.ts":
|
|
103
|
+
'import { putObject } from "@goerp/core/storage"\nexport const POST = apiHandler(fn)',
|
|
104
|
+
});
|
|
105
|
+
expect(resultOf(run(leaky), "one-door/storage").violations).toEqual([
|
|
106
|
+
"app/api/upload/route.ts",
|
|
107
|
+
]);
|
|
108
|
+
|
|
109
|
+
const unconfigured = fixture({
|
|
110
|
+
"lib/storage.ts": 'export { putObject } from "@goerp/core/storage"',
|
|
111
|
+
});
|
|
112
|
+
expect(
|
|
113
|
+
resultOf(run(unconfigured), "one-door/storage").violations.join(),
|
|
114
|
+
).toContain("KHÔNG gọi configureStorage()");
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("one-door/storage — app chưa có cửa nào thì rule tự bỏ qua", () => {
|
|
118
|
+
const root = fixture({ "lib/a.ts": "export const a = 1" });
|
|
119
|
+
expect(resultOf(run(root), "one-door/storage").status).toBe(
|
|
120
|
+
"not-applicable",
|
|
121
|
+
);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("auth/api-route-gated — không gác thì phải được KHAI công khai ở proxy.ts", () => {
|
|
125
|
+
const root = fixture({
|
|
126
|
+
"proxy.ts":
|
|
127
|
+
'createAuthProxy({ publicApiPrefixes: ["/api/better-auth", "/api/zma"] })',
|
|
128
|
+
"app/api/orders/route.ts":
|
|
129
|
+
"export async function GET() { return Response.json([]) }",
|
|
130
|
+
"app/api/roles/route.ts":
|
|
131
|
+
"export const GET = apiHandler(fn, { resource: 'roles' })",
|
|
132
|
+
// Khai công khai (self-auth) → không đòi cổng phiên.
|
|
133
|
+
"app/api/better-auth/[...all]/route.ts": "export const GET = handler",
|
|
134
|
+
"app/api/zma/orders/route.ts":
|
|
135
|
+
"export async function GET() { return Response.json([]) }",
|
|
136
|
+
});
|
|
137
|
+
expect(resultOf(run(root), "auth/api-route-gated").violations).toEqual([
|
|
138
|
+
"app/api/orders/route.ts",
|
|
139
|
+
]);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("auth/api-route-gated — stub app/api được lần tới handler thật trong module", () => {
|
|
143
|
+
const root = fixture({
|
|
144
|
+
"app/api/orders/route.ts": 'export * from "@/modules/sales/api/orders"',
|
|
145
|
+
"modules/sales/api/orders.ts":
|
|
146
|
+
"export const GET = apiHandler(fn, { resource: 'orders', action: 'view' })",
|
|
147
|
+
});
|
|
148
|
+
expect(resultOf(run(root), "auth/api-route-gated").violations).toEqual([]);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("auth/server-action-bare-session — chỉ bắt file 'use server'", () => {
|
|
152
|
+
const root = fixture({
|
|
153
|
+
"actions/orders.ts": '"use server"\nconst s = await getSession()',
|
|
154
|
+
"lib/read.ts": "const s = await getSession()",
|
|
155
|
+
});
|
|
156
|
+
expect(
|
|
157
|
+
resultOf(run(root), "auth/server-action-bare-session").violations,
|
|
158
|
+
).toEqual(["actions/orders.ts"]);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("auth/proxy-matcher-covers-api — matcher loại trừ /api thì đỏ", () => {
|
|
162
|
+
const bad = fixture({
|
|
163
|
+
"proxy.ts": "export const config = { matcher: ['/((?!api|_next).*)'] }",
|
|
164
|
+
});
|
|
165
|
+
expect(
|
|
166
|
+
resultOf(run(bad), "auth/proxy-matcher-covers-api").violations,
|
|
167
|
+
).toHaveLength(1);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("layering/domain-framework-free — domain dính next/* thì đỏ, modules/*/api thì không", () => {
|
|
171
|
+
const root = fixture({
|
|
172
|
+
"modules/sales/services/order-service.ts":
|
|
173
|
+
'import { revalidatePath } from "next/cache"',
|
|
174
|
+
"modules/sales/api/orders.ts":
|
|
175
|
+
'import { NextResponse } from "next/server"',
|
|
176
|
+
});
|
|
177
|
+
expect(
|
|
178
|
+
resultOf(run(root), "layering/domain-framework-free").violations.join(),
|
|
179
|
+
).toContain("modules/sales/services/order-service.ts");
|
|
180
|
+
expect(
|
|
181
|
+
resultOf(run(root), "layering/domain-framework-free").violations.join(),
|
|
182
|
+
).not.toContain("modules/sales/api/orders.ts");
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("layering/module-public-api — deep-import bị bắt, server action thì được miễn", () => {
|
|
186
|
+
const root = fixture({
|
|
187
|
+
"modules/sales/index.ts": "export {}",
|
|
188
|
+
"modules/sales/services/x.ts": "export const x = 1",
|
|
189
|
+
"modules/sales/actions/save.ts":
|
|
190
|
+
'"use server"\nexport async function save() {}',
|
|
191
|
+
"app/page.tsx": 'import { x } from "@/modules/sales/services/x"',
|
|
192
|
+
"app/form.tsx": 'import { save } from "@/modules/sales/actions/save"',
|
|
193
|
+
});
|
|
194
|
+
const v = resultOf(run(root), "layering/module-public-api").violations;
|
|
195
|
+
expect(v.join()).toContain("app/page.tsx");
|
|
196
|
+
expect(v.join()).not.toContain("app/form.tsx");
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("design/no-fragile-flex-hidden — `flex lg:hidden` đỏ, `hidden max-lg:flex` xanh", () => {
|
|
200
|
+
const root = fixture({
|
|
201
|
+
"app/bad.tsx": '<div className="flex items-center lg:hidden" />',
|
|
202
|
+
"app/good.tsx": '<div className="hidden items-center max-lg:flex" />',
|
|
203
|
+
});
|
|
204
|
+
const v = resultOf(run(root), "design/no-fragile-flex-hidden").violations;
|
|
205
|
+
expect(v.join()).toContain("app/bad.tsx");
|
|
206
|
+
expect(v.join()).not.toContain("app/good.tsx");
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it("safety/no-network-in-transaction — chỉ bắt lời gọi NẰM TRONG thân transaction", () => {
|
|
210
|
+
const root = fixture({
|
|
211
|
+
"modules/a/inside.ts":
|
|
212
|
+
"await db.$transaction(async (tx) => {\n await fetch(url)\n})",
|
|
213
|
+
"modules/a/outside.ts":
|
|
214
|
+
"await fetch(url)\nawait db.$transaction(async (tx) => {})",
|
|
215
|
+
});
|
|
216
|
+
expect(
|
|
217
|
+
resultOf(run(root), "safety/no-network-in-transaction").violations,
|
|
218
|
+
).toEqual(["modules/a/inside.ts"]);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it("structure/dockerignore-keeps-source — glob nuốt mã nguồn thì đỏ", () => {
|
|
222
|
+
const root = fixture({
|
|
223
|
+
"modules/sales/api/check-resold.ts": "export const GET = apiHandler(fn)",
|
|
224
|
+
"app/page.tsx": "export default function Page() {}",
|
|
225
|
+
});
|
|
226
|
+
atRoot(root, ".dockerignore", "node_modules\n**/check-*.ts\n");
|
|
227
|
+
expect(
|
|
228
|
+
resultOf(run(root), "structure/dockerignore-keeps-source").violations,
|
|
229
|
+
).toEqual(["src/modules/sales/api/check-resold.ts"]);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("rbac/gate-action-declared — cổng đòi action registry chưa khai thì đỏ", () => {
|
|
233
|
+
const root = fixture({
|
|
234
|
+
"app/api/orders/route.ts":
|
|
235
|
+
"export const POST = apiHandler(fn, { resource: 'orders', action: 'approve' })",
|
|
236
|
+
});
|
|
237
|
+
const results = run(root, {
|
|
238
|
+
permissionRegistry: [
|
|
239
|
+
{ resources: [{ code: "orders", actions: ["view", "create"] }] },
|
|
240
|
+
],
|
|
241
|
+
});
|
|
242
|
+
expect(resultOf(results, "rbac/gate-action-declared").violations).toEqual([
|
|
243
|
+
"app/api/orders/route.ts → orders:approve",
|
|
244
|
+
]);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it("rbac/gate-* chỉ đọc đối tượng khai ở CỔNG, không đọc payload nhật ký", () => {
|
|
248
|
+
const root = fixture({
|
|
249
|
+
// Ba thứ cùng mang key `resource` trong một file, chỉ MỘT là khai quyền:
|
|
250
|
+
// sắp xếp của Prisma, payload ghi vết thực thể, và cổng thật.
|
|
251
|
+
"app/api/orders/route.ts": [
|
|
252
|
+
"export const GET = apiHandler(async (req) => {",
|
|
253
|
+
" await db.auditLog.groupBy({ by: ['resource'], orderBy: { _count: { resource: 'desc' } } })",
|
|
254
|
+
" await logEntityAction({ resource: 'stock-movement', action: 'add-document' })",
|
|
255
|
+
" return NextResponse.json({})",
|
|
256
|
+
"}, { resource: 'orders', action: 'view' })",
|
|
257
|
+
].join("\n"),
|
|
258
|
+
});
|
|
259
|
+
const results = run(root, {
|
|
260
|
+
permissionRegistry: [
|
|
261
|
+
{ resources: [{ code: "orders", actions: ["view"] }] },
|
|
262
|
+
],
|
|
263
|
+
});
|
|
264
|
+
expect(resultOf(results, "rbac/gate-resource-declared").violations).toEqual(
|
|
265
|
+
[],
|
|
266
|
+
);
|
|
267
|
+
expect(resultOf(results, "rbac/gate-action-declared").violations).toEqual(
|
|
268
|
+
[],
|
|
269
|
+
);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("rbac/gate-resource-declared — cổng khai resource ngoài registry thì đỏ", () => {
|
|
273
|
+
const root = fixture({
|
|
274
|
+
"app/api/orders/route.ts":
|
|
275
|
+
"export const GET = apiHandler(fn, { resource: 'ghost', action: 'view' })",
|
|
276
|
+
});
|
|
277
|
+
const results = run(root, {
|
|
278
|
+
permissionRegistry: [
|
|
279
|
+
{ resources: [{ code: "orders", actions: ["view"] }] },
|
|
280
|
+
],
|
|
281
|
+
});
|
|
282
|
+
expect(resultOf(results, "rbac/gate-resource-declared").violations).toEqual(
|
|
283
|
+
["app/api/orders/route.ts → ghost"],
|
|
284
|
+
);
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
it("rbac/nav-resource-declared — mục menu trỏ resource chưa khai thì đỏ", () => {
|
|
288
|
+
const results = run(fixture({ "lib/a.ts": "export const a = 1" }), {
|
|
289
|
+
permissionRegistry: [
|
|
290
|
+
{ resources: [{ code: "orders", actions: ["view"] }] },
|
|
291
|
+
],
|
|
292
|
+
navigations: [{ items: [{ resource: "orders" }, { resource: "ghost" }] }],
|
|
293
|
+
});
|
|
294
|
+
expect(resultOf(results, "rbac/nav-resource-declared").violations).toEqual([
|
|
295
|
+
"ghost",
|
|
296
|
+
]);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it("rbac/* tự bỏ qua khi app không truyền registry", () => {
|
|
300
|
+
const results = run(fixture({ "lib/a.ts": "export const a = 1" }));
|
|
301
|
+
expect(resultOf(results, "rbac/known-actions").status).toBe(
|
|
302
|
+
"not-applicable",
|
|
303
|
+
);
|
|
304
|
+
});
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
describe("ratchet: allowlist chỉ được rút bớt", () => {
|
|
308
|
+
const spec = {
|
|
309
|
+
id: "test/forbid",
|
|
310
|
+
title: "t",
|
|
311
|
+
why: "w",
|
|
312
|
+
fix: "f",
|
|
313
|
+
pattern: /forbidden/,
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
it("allowlist che được vi phạm đã biết", () => {
|
|
317
|
+
const ctx = createContext({
|
|
318
|
+
root: fixture({ "a.ts": "forbidden", "b.ts": "forbidden" }),
|
|
319
|
+
allowlists: { "test/forbid": ["a.ts"] },
|
|
320
|
+
});
|
|
321
|
+
expect(forbidPattern(spec).run(ctx)).toEqual(["b.ts"]);
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
it("entry hết vi phạm (hoặc file đã xoá) bị báo là ôi", () => {
|
|
325
|
+
const ctx = createContext({
|
|
326
|
+
root: fixture({ "a.ts": "clean" }),
|
|
327
|
+
allowlists: { "test/forbid": ["a.ts", "deleted.ts"] },
|
|
328
|
+
});
|
|
329
|
+
expect(forbidPattern(spec).staleAllowlist!(ctx).sort()).toEqual([
|
|
330
|
+
"a.ts",
|
|
331
|
+
"deleted.ts",
|
|
332
|
+
]);
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
it("entry kết thúc bằng `/` miễn trừ cả thư mục", () => {
|
|
336
|
+
const ctx = createContext({
|
|
337
|
+
root: fixture({ "_handlers/a.ts": "forbidden", "b.ts": "forbidden" }),
|
|
338
|
+
allowlists: { "test/forbid": ["_handlers/"] },
|
|
339
|
+
});
|
|
340
|
+
expect(forbidPattern(spec).run(ctx)).toEqual(["b.ts"]);
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
it("entry thư mục hết vi phạm cũng bị báo là ôi", () => {
|
|
344
|
+
const ctx = createContext({
|
|
345
|
+
root: fixture({ "_handlers/a.ts": "sạch" }),
|
|
346
|
+
allowlists: { "test/forbid": ["_handlers/"] },
|
|
347
|
+
});
|
|
348
|
+
expect(forbidPattern(spec).staleAllowlist!(ctx)).toEqual(["_handlers/"]);
|
|
349
|
+
});
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
describe("ratchet: trần theo thư mục", () => {
|
|
353
|
+
const [over, slack] = perDirectoryCeiling({
|
|
354
|
+
id: "test/any",
|
|
355
|
+
title: "t",
|
|
356
|
+
why: "w",
|
|
357
|
+
fix: "f",
|
|
358
|
+
pattern: /\bas any\b/g,
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
it("app mới không khai trần = trần 0 ở mọi thư mục", () => {
|
|
362
|
+
const ctx = createContext({ root: fixture({ "lib/a.ts": "x as any" }) });
|
|
363
|
+
expect(over.run(ctx)).toEqual(["lib: 1 > trần 0"]);
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
it("trần đúng hiện trạng thì xanh cả hai chiều", () => {
|
|
367
|
+
const ctx = createContext({
|
|
368
|
+
root: fixture({ "lib/a.ts": "x as any" }),
|
|
369
|
+
ceilings: { "test/any": { lib: 1 } },
|
|
370
|
+
});
|
|
371
|
+
expect(over.run(ctx)).toEqual([]);
|
|
372
|
+
expect(slack.run(ctx)).toEqual([]);
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
it("trần dư cũng đỏ — trả nợ tới đâu hạ trần tới đó", () => {
|
|
376
|
+
const ctx = createContext({
|
|
377
|
+
root: fixture({ "lib/a.ts": "sạch" }),
|
|
378
|
+
ceilings: { "test/any": { lib: 5 } },
|
|
379
|
+
});
|
|
380
|
+
expect(slack.run(ctx)).toEqual(["lib: trần 5 → thực tế 0"]);
|
|
381
|
+
});
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
describe("van an toàn `skip`", () => {
|
|
385
|
+
it("skip theo id và theo cả nhóm đều có tác dụng", () => {
|
|
386
|
+
const root = fixture({ "server/x.ts": "new PrismaClient()" });
|
|
387
|
+
expect(
|
|
388
|
+
resultOf(
|
|
389
|
+
run(root, { skip: { "one-door/single-prisma-client": "nợ cũ" } }),
|
|
390
|
+
"one-door/single-prisma-client",
|
|
391
|
+
).status,
|
|
392
|
+
).toBe("skipped");
|
|
393
|
+
expect(
|
|
394
|
+
resultOf(
|
|
395
|
+
run(root, { skip: { "one-door/*": "nợ cũ" } }),
|
|
396
|
+
"one-door/single-prisma-client",
|
|
397
|
+
).status,
|
|
398
|
+
).toBe("skipped");
|
|
399
|
+
});
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
describe("app sạch", () => {
|
|
403
|
+
it("một app tối thiểu, đúng chuẩn thì không rule nào đỏ", () => {
|
|
404
|
+
const root = fixture({
|
|
405
|
+
"proxy.ts": [
|
|
406
|
+
'import { getSessionCookie } from "better-auth/cookies"',
|
|
407
|
+
'if (pathname.startsWith("/api") && !isPublicApiPath(pathname)) return deny()',
|
|
408
|
+
"export const config = { matcher: ['/((?!_next).*)'] }",
|
|
409
|
+
].join("\n"),
|
|
410
|
+
"lib/prisma.ts": "export const db = new PrismaClient()",
|
|
411
|
+
"app/globals.css": '@import "@goerp/core/styles/base.css";',
|
|
412
|
+
"app/api/roles/route.ts":
|
|
413
|
+
"export const GET = apiHandler(fn, { resource: 'roles', action: 'view' })",
|
|
414
|
+
"app/[lang]/(main)/roles/page.tsx":
|
|
415
|
+
"export default function Page() { return null }",
|
|
416
|
+
"app/[lang]/(main)/roles/roles-client-page.tsx":
|
|
417
|
+
"export function C() { return null }",
|
|
418
|
+
});
|
|
419
|
+
// globals.css không phải .ts nên fixture ghi thẳng; rule đọc bằng đường dẫn.
|
|
420
|
+
const failed = run(root, {
|
|
421
|
+
permissionRegistry: [
|
|
422
|
+
{ resources: [{ code: "roles", actions: ["view"] }] },
|
|
423
|
+
],
|
|
424
|
+
navigations: [{ items: [{ resource: "roles" }] }],
|
|
425
|
+
}).filter((r) => r.status === "fail");
|
|
426
|
+
expect(
|
|
427
|
+
failed.map((r) => `${r.rule.id}: ${r.violations.join(", ")}`),
|
|
428
|
+
).toEqual([]);
|
|
429
|
+
});
|
|
430
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hàng rào kiến trúc dùng chung — luật đi theo core, dữ liệu ở lại app.
|
|
3
|
+
*
|
|
4
|
+
* Mỗi rule ở đây là một **ca lỗi có thật** đã tốn thời gian ở app gốc, viết lại
|
|
5
|
+
* thành phép quét tĩnh. Ba lý do để chúng sống trong core thay vì trong từng app:
|
|
6
|
+
*
|
|
7
|
+
* 1. **Luật lan được.** Bài học rút ra hôm nay tới mọi app ở lần `pnpm up` sau,
|
|
8
|
+
* thay vì đóng băng trong bản chép tay của ngày app được scaffold.
|
|
9
|
+
* 2. **Thông báo lỗi mới là chỗ người ta đọc.** Rule mang theo `why` — nguyên
|
|
10
|
+
* văn sự cố — nên khi test đỏ, người/agent sửa hiểu vì sao chứ không đi tìm
|
|
11
|
+
* cách làm cho nó xanh.
|
|
12
|
+
* 3. **Van an toàn hiện hình.** `skip` bắt buộc kèm lý do và tự đỏ khi nợ đã
|
|
13
|
+
* trả, nên bump core không làm gãy app đang chạy mà cũng không đẻ ra chỗ trốn.
|
|
14
|
+
*
|
|
15
|
+
* Dùng trong `src/__tests__/architecture.test.ts` của app:
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { runCoreGuardrails } from "@goerp/core/guardrails"
|
|
19
|
+
* import { permissionRegistry } from "@/configs/permissions"
|
|
20
|
+
* import { navigations } from "@/data/navigations"
|
|
21
|
+
*
|
|
22
|
+
* runCoreGuardrails({ permissionRegistry, navigations })
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
export { runCoreGuardrails } from "./run";
|
|
27
|
+
export { coreGuardrails, evaluateGuardrails, skipReasonFor } from "./preset";
|
|
28
|
+
export { createContext, callBodies, stripComments, resolveOptions } from "./scanner";
|
|
29
|
+
export {
|
|
30
|
+
allowlistFor,
|
|
31
|
+
fileContract,
|
|
32
|
+
forbidFile,
|
|
33
|
+
forbidPattern,
|
|
34
|
+
perDirectoryCeiling,
|
|
35
|
+
singleDoorImport,
|
|
36
|
+
} from "./primitives";
|
|
37
|
+
export { anyDebtRules, statusLiteralCeiling } from "./rules/debt";
|
|
38
|
+
export type { StatusLiteralSpec } from "./rules/debt";
|
|
39
|
+
export { STANDARD_ACTIONS } from "./rules/rbac";
|
|
40
|
+
export { authRules } from "./rules/auth";
|
|
41
|
+
export { rbacRules } from "./rules/rbac";
|
|
42
|
+
export { layeringRules } from "./rules/layering";
|
|
43
|
+
export { oneDoorRules } from "./rules/one-door";
|
|
44
|
+
export { structureRules } from "./rules/structure";
|
|
45
|
+
export { designRules } from "./rules/design";
|
|
46
|
+
export { safetyRules } from "./rules/safety";
|
|
47
|
+
export { NOT_APPLICABLE } from "./types";
|
|
48
|
+
export type {
|
|
49
|
+
GuardrailContext,
|
|
50
|
+
GuardrailGroup,
|
|
51
|
+
GuardrailOptions,
|
|
52
|
+
GuardrailResult,
|
|
53
|
+
GuardrailRule,
|
|
54
|
+
NavigationGroupLike,
|
|
55
|
+
PermissionFeatureLike,
|
|
56
|
+
ResolvedGuardrailOptions,
|
|
57
|
+
} from "./types";
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { authRules } from "./rules/auth";
|
|
2
|
+
import { anyDebtRules } from "./rules/debt";
|
|
3
|
+
import { designRules } from "./rules/design";
|
|
4
|
+
import { layeringRules } from "./rules/layering";
|
|
5
|
+
import { oneDoorRules } from "./rules/one-door";
|
|
6
|
+
import { rbacRules } from "./rules/rbac";
|
|
7
|
+
import { safetyRules } from "./rules/safety";
|
|
8
|
+
import { structureRules } from "./rules/structure";
|
|
9
|
+
import { createContext } from "./scanner";
|
|
10
|
+
import type {
|
|
11
|
+
GuardrailOptions,
|
|
12
|
+
GuardrailResult,
|
|
13
|
+
GuardrailRule,
|
|
14
|
+
} from "./types";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Bộ hàng rào chuẩn — chưng cất từ app gốc (vinhhoa) sau nhiều đợt refactor.
|
|
18
|
+
*
|
|
19
|
+
* Chỉ giữ những rule ĐÚNG VỚI MỌI APP dựng trên core. Rule dính từ vựng nghiệp
|
|
20
|
+
* vụ (trạng thái thanh toán, một cửa repository tiền, chokepoint kho hàng) KHÔNG
|
|
21
|
+
* nằm ở đây — chúng đi qua `options.extraRules`, dựng bằng chính các primitive
|
|
22
|
+
* mà bộ này dùng.
|
|
23
|
+
*/
|
|
24
|
+
export function coreGuardrails(options: GuardrailOptions = {}): GuardrailRule[] {
|
|
25
|
+
return [
|
|
26
|
+
...authRules,
|
|
27
|
+
...rbacRules,
|
|
28
|
+
...layeringRules,
|
|
29
|
+
...oneDoorRules,
|
|
30
|
+
...structureRules,
|
|
31
|
+
...designRules,
|
|
32
|
+
...safetyRules,
|
|
33
|
+
...anyDebtRules,
|
|
34
|
+
...(options.extraRules ?? []),
|
|
35
|
+
];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** `skip` nhận cả id chính xác lẫn cả nhóm (`"design/*"`). */
|
|
39
|
+
export function skipReasonFor(
|
|
40
|
+
id: string,
|
|
41
|
+
skip: Record<string, string>,
|
|
42
|
+
): string | undefined {
|
|
43
|
+
if (skip[id]) return skip[id];
|
|
44
|
+
const group = id.split("/")[0];
|
|
45
|
+
return skip[`${group}/*`];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Chạy toàn bộ hàng rào mà KHÔNG cần vitest — dùng cho `goerp doctor` và cho
|
|
50
|
+
* bất kỳ script CI nào muốn báo cáo thay vì ném lỗi.
|
|
51
|
+
*/
|
|
52
|
+
export function evaluateGuardrails(
|
|
53
|
+
options: GuardrailOptions = {},
|
|
54
|
+
): GuardrailResult[] {
|
|
55
|
+
const ctx = createContext(options);
|
|
56
|
+
const skip = options.skip ?? {};
|
|
57
|
+
|
|
58
|
+
return coreGuardrails(options).map((rule): GuardrailResult => {
|
|
59
|
+
const skipReason = skipReasonFor(rule.id, skip);
|
|
60
|
+
if (skipReason) {
|
|
61
|
+
return { rule, status: "skipped", violations: [], staleAllowlist: [], skipReason };
|
|
62
|
+
}
|
|
63
|
+
const violations = rule.run(ctx);
|
|
64
|
+
if (violations === null) {
|
|
65
|
+
return { rule, status: "not-applicable", violations: [], staleAllowlist: [] };
|
|
66
|
+
}
|
|
67
|
+
const stale = rule.staleAllowlist?.(ctx) ?? [];
|
|
68
|
+
return {
|
|
69
|
+
rule,
|
|
70
|
+
status: violations.length === 0 && stale.length === 0 ? "pass" : "fail",
|
|
71
|
+
violations,
|
|
72
|
+
staleAllowlist: stale,
|
|
73
|
+
};
|
|
74
|
+
});
|
|
75
|
+
}
|