test: 固化应用平台下沉前的兼容与分层契约

覆盖任意深度域 API、应用平台子模块方向和跨模块循环检测。
锁定应用列表基础与详情投影、缺省字段、清洗及详情参数的既有差异。

验证:943 项测试、类型和版本检查通过。
This commit is contained in:
gitea committed 2026-10-30 16:50:14 +08:00
1 parent 3345ce9b39
commit edf300b808
2 files changed
+113 -2

No files matched your search

+59 -2
View File
@@ -39,6 +39,30 @@ function resolveImportRelativeToSrc(fromFile: string, specifier: string): string
return path ? relative(SRC_ROOT, path.replace(/\.[jt]s$/, "")).replaceAll("\\", "/") : null;
}
function platformModule(path: string): string | undefined {
return path.match(/^domains\/platform\/(applications|datalink|workflow)\//)?.[1];
}
function platformDependencyViolation(from: string, to: string): boolean {
const source = platformModule(from), target = platformModule(to);
if (!source || !target || source === target) return false;
return /\/api$/.test(to) || source === "applications" || (source === "datalink" && target === "workflow");
}
function findCycles(graph: Map<string, Set<string>>): string[][] {
const done = new Set<string>(), active: string[] = [], cycles: string[][] = [];
const visit = (node: string) => {
const index = active.indexOf(node);
if (index >= 0) { cycles.push([...active.slice(index), node]); return; }
if (done.has(node)) return;
active.push(node);
for (const next of graph.get(node) ?? []) visit(next);
active.pop(); done.add(node);
};
for (const node of graph.keys()) visit(node);
return cycles;
}
describe("Architecture Layering Boundaries", () => {
test("dependency extraction covers aliases, re-exports and literal dynamic/type imports", () => {
const imports = extractImportSpecifiers(`
@@ -52,6 +76,39 @@ describe("Architecture Layering Boundaries", () => {
expect(resolveImportRelativeToSrc(join(SRC_ROOT, "cli", "example.ts"), imports[1])).toBe("domains/bill/api");
});
test("nested APIs and platform dependency directions cannot bypass boundaries", () => {
const api = /^domains\/(?:[^/]+\/)+api$/;
expect(api.test("domains/platform/workflow/api")).toBe(true);
expect(api.test("domains/bill/api")).toBe(true);
expect(api.test("domains/platform/workflow/services")).toBe(false);
expect(platformDependencyViolation("domains/platform/workflow/services", "domains/platform/applications/services")).toBe(false);
expect(platformDependencyViolation("domains/platform/workflow/services", "domains/platform/applications/api")).toBe(true);
expect(platformDependencyViolation("domains/platform/applications/types", "domains/platform/datalink/types")).toBe(true);
expect(platformDependencyViolation("domains/platform/datalink/services", "domains/platform/workflow/services")).toBe(true);
expect(findCycles(new Map([["a", new Set(["b"])], ["b", new Set(["a"])]]))).toEqual([["a", "b", "a"]]);
expect(findCycles(new Map([["a", new Set(["b"])], ["b", new Set<string>()]]))).toEqual([]);
});
test("platform submodules respect ownership and have no cross-module cycles", () => {
const graph = new Map<string, Set<string>>(), violations: string[] = [];
for (const file of collectTsFiles(join(SRC_ROOT, "domains", "platform"))) {
const from = relative(SRC_ROOT, file).replaceAll("\\", "/").replace(/\.ts$/, "");
const source = platformModule(from);
for (const specifier of extractImportSpecifiers(readFileSync(file, "utf8"))) {
const target = resolveImportRelativeToSrc(file, specifier);
if (!target) continue;
if (platformDependencyViolation(from, target)) violations.push(`${from} -> ${target}`);
const destination = platformModule(target);
if (source && destination && source !== destination) {
if (!graph.has(source)) graph.set(source, new Set());
graph.get(source)!.add(destination);
}
}
}
expect(violations).toEqual([]);
expect(findCycles(graph)).toEqual([]);
});
test("bill Session HTTP calls stay in the transport module", () => {
const violations: string[] = [];
for (const file of collectTsFiles(join(SRC_ROOT, "domains", "bill"))) {
@@ -85,7 +142,7 @@ describe("Architecture Layering Boundaries", () => {
}
expect(violations).toEqual([]);
});
test("Entrypoints (src/cli, src/http, server.ts, cli.ts, aipro-cli.ts) must never import domains/*/api directly", () => {
test("Entrypoints (src/cli, src/http, server.ts, cli.ts, aipro-cli.ts) must never import domains/**/api directly", () => {
const entrypointFiles = [
...collectTsFiles(join(SRC_ROOT, "cli")),
...collectTsFiles(join(SRC_ROOT, "http")),
@@ -100,7 +157,7 @@ describe("Architecture Layering Boundaries", () => {
const specifiers = extractImportSpecifiers(content);
for (const spec of specifiers) {
const targetRel = resolveImportRelativeToSrc(file, spec);
if (targetRel && /^domains\/[^/]+\/api$/.test(targetRel)) {
if (targetRel && /^domains\/(?:[^/]+\/)+api$/.test(targetRel)) {
violations.push(`${relative(SRC_ROOT, file)} -> ${targetRel}`);
}
}
@@ -0,0 +1,54 @@
import { expect, test } from "bun:test";
import type { KyInstance } from "ky";
import { listPlatforms as listBasic } from "./api.js";
import { listPlatforms as listDetailed, listAllPlatforms, getPlatform } from "../datalink/api.js";
function client(body: unknown) {
const calls: Array<{ path: string; options: any }> = [];
return { calls, http: { get: async (path: string, options: unknown) => {
calls.push({ path, options });
return Response.json(body);
} } as unknown as KyInstance };
}
const platform = { id: "app", name: "Fixture", corporationId: "corp", adminIds: null, importMethod: null,
extra: "preserve", staffIds: null, admin: [{ id: "s", name: "Staff", code: "S", active: true, secret: "omit" }] };
test("应用基础与详情查询共享请求形状,但保持不同投影和脱敏", async () => {
const a = client({ items: [platform] }), b = client({ items: [platform] });
const basic = (await listBasic(a.http, "corp"))[0]!;
const detail = (await listDetailed(b.http, "corp"))[0]!;
expect(a.calls).toEqual(b.calls);
expect(a.calls[0]).toEqual({ path: "api/v2/datalink/platform/search", options: { searchParams: { scope: "enable", corpId: "corp" } } });
expect(basic.adminIds).toEqual([]);
expect(basic.importMethod).toEqual([]);
expect(basic).not.toHaveProperty("extra");
expect(basic).not.toHaveProperty("admin");
expect(detail.extra).toBe("preserve");
expect(detail.staffIds).toEqual([]);
expect(detail.admin).toEqual([{ id: "s", name: "Staff", code: "S", active: true }]);
});
test("应用查询保留缺失 items 和无效响应的既有差异", async () => {
for (const body of [{}, { items: {} }, { code: 403, msg: "fixture" }]) {
await expect(listBasic(client(body).http, "corp")).rejects.toThrow();
expect(await listDetailed(client(body).http, "corp")).toEqual([]);
}
for (const query of [listBasic, listDetailed]) {
await expect(query(client({ items: [{ name: "missing id" }] }).http, "corp")).rejects.toThrow();
}
});
test("全量应用及详情保留响应包装、ID 与 join 参数兼容", async () => {
expect(await listAllPlatforms(client([platform]).http, "corp")).toEqual([]);
expect((await listAllPlatforms(client({ items: [platform] }).http, "corp"))[0]?.id).toBe("app");
for (const body of [platform, { data: platform }, { value: platform }]) {
for (const id of ["app", "$app"]) {
const f = client(body);
expect((await getPlatform(f.http, "corp", id)).id).toBe("app");
expect(f.calls[0]?.path).toBe("api/v2/datalink/platform/$app");
const params = f.calls[0]?.options.searchParams as URLSearchParams;
expect(params.get("id")).toBe("app");
expect(params.getAll("join")).toEqual(["icon.fileId,fileId,/v1/attachment/attachments", "adminIds,admin,/v1/organization/staffs"]);
}
}
});