refactor(architecture): eliminate direct domains/*/api imports in CLI entrypoints and add boundary tests
This commit is contained in:
1 parent
fb11fedb76
commit
5a0c277993
9 files changed
+256
-6
No files matched your search
+1
-2
@@ -14,8 +14,7 @@ import {
|
||||
parseRuleBody,
|
||||
validateGatewayAction,
|
||||
} from "./domains/approval/index.js";
|
||||
import { getRawBillFlow } from "./domains/bill/api.js";
|
||||
import { approveBill, rejectBill, getTranslatorContext } from "./domains/bill/tools.js";
|
||||
import { approveBill, rejectBill, getRawBillFlow, getTranslatorContext } from "./domains/bill/tools.js";
|
||||
import { translateBillFlowToMarkdown } from "./domains/bill/translator.js";
|
||||
import { VERSION, formatVersionLabel, getVersionInfo } from "./version.js";
|
||||
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { readdirSync, readFileSync, statSync } from "node:fs";
|
||||
import { dirname, join, relative, resolve } from "node:path";
|
||||
|
||||
const SRC_ROOT = join(import.meta.dir, "..");
|
||||
|
||||
function collectTsFiles(dir: string, includeTests = false): string[] {
|
||||
const results: string[] = [];
|
||||
for (const entry of readdirSync(dir)) {
|
||||
const full = join(dir, entry);
|
||||
const stat = statSync(full);
|
||||
if (stat.isDirectory()) {
|
||||
results.push(...collectTsFiles(full, includeTests));
|
||||
} else if (full.endsWith(".ts") && (includeTests || !full.endsWith(".test.ts"))) {
|
||||
results.push(full);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
function extractImportSpecifiers(source: string): string[] {
|
||||
const specifiers: string[] = [];
|
||||
const staticImportRe = /(?:import|export)\s+(?:type\s+)?(?:[\s\S]*?\s+from\s+)?["']([^"']+)["']/g;
|
||||
const dynamicImportRe = /import\(\s*["']([^"']+)["']\s*\)/g;
|
||||
|
||||
for (const match of source.matchAll(staticImportRe)) {
|
||||
if (match[1]) specifiers.push(match[1]);
|
||||
}
|
||||
for (const match of source.matchAll(dynamicImportRe)) {
|
||||
if (match[1]) specifiers.push(match[1]);
|
||||
}
|
||||
return specifiers;
|
||||
}
|
||||
|
||||
function resolveImportRelativeToSrc(fromFile: string, specifier: string): string | null {
|
||||
if (!specifier.startsWith(".")) return null;
|
||||
const abs = resolve(dirname(fromFile), specifier).replace(/\.[jt]s$/, "");
|
||||
return relative(SRC_ROOT, abs);
|
||||
}
|
||||
|
||||
describe("Architecture Layering Boundaries", () => {
|
||||
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")),
|
||||
join(SRC_ROOT, "server.ts"),
|
||||
join(SRC_ROOT, "cli.ts"),
|
||||
join(SRC_ROOT, "aipro-cli.ts"),
|
||||
];
|
||||
|
||||
const violations: string[] = [];
|
||||
for (const file of entrypointFiles) {
|
||||
const content = readFileSync(file, "utf8");
|
||||
const specifiers = extractImportSpecifiers(content);
|
||||
for (const spec of specifiers) {
|
||||
const targetRel = resolveImportRelativeToSrc(file, spec);
|
||||
if (targetRel && /^domains\/[^/]+\/api$/.test(targetRel)) {
|
||||
violations.push(`${relative(SRC_ROOT, file)} -> ${targetRel}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
expect(violations).toEqual([]);
|
||||
});
|
||||
|
||||
test("src/domains/ must remain pure SDK and never import from cli, http, or server entrypoints", () => {
|
||||
const domainFiles = collectTsFiles(join(SRC_ROOT, "domains"));
|
||||
const violations: string[] = [];
|
||||
|
||||
for (const file of domainFiles) {
|
||||
const content = readFileSync(file, "utf8");
|
||||
const specifiers = extractImportSpecifiers(content);
|
||||
for (const spec of specifiers) {
|
||||
const targetRel = resolveImportRelativeToSrc(file, spec);
|
||||
if (
|
||||
targetRel &&
|
||||
(targetRel.startsWith("cli/") ||
|
||||
targetRel.startsWith("http/") ||
|
||||
targetRel === "server" ||
|
||||
targetRel === "cli" ||
|
||||
targetRel === "aipro-cli")
|
||||
) {
|
||||
violations.push(`${relative(SRC_ROOT, file)} -> ${targetRel}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
expect(violations).toEqual([]);
|
||||
});
|
||||
|
||||
test("src/core/ must never depend upward on domains, cli, http, or server entrypoints", () => {
|
||||
const coreFiles = collectTsFiles(join(SRC_ROOT, "core"));
|
||||
const violations: string[] = [];
|
||||
|
||||
for (const file of coreFiles) {
|
||||
const content = readFileSync(file, "utf8");
|
||||
const specifiers = extractImportSpecifiers(content);
|
||||
for (const spec of specifiers) {
|
||||
const targetRel = resolveImportRelativeToSrc(file, spec);
|
||||
if (
|
||||
targetRel &&
|
||||
(targetRel.startsWith("domains/") ||
|
||||
targetRel.startsWith("cli/") ||
|
||||
targetRel.startsWith("http/") ||
|
||||
targetRel === "server" ||
|
||||
targetRel === "cli" ||
|
||||
targetRel === "aipro-cli")
|
||||
) {
|
||||
violations.push(`${relative(SRC_ROOT, file)} -> ${targetRel}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
expect(violations).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,5 @@
|
||||
import { fetchWorkbenchAppsList } from "../../domains/menu/tools.js";
|
||||
import { getPlatform } from "../../domains/datalink/api.js";
|
||||
import { getPlatform } from "../../domains/datalink/services.js";
|
||||
import { createClientsForCorp } from "../helpers.js";
|
||||
import { stringOption, booleanOption } from "../parser.js";
|
||||
import { printJson, printTable } from "../printer.js";
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import * as api from "../../domains/datalink/api.js";
|
||||
import * as api from "../../domains/datalink/services.js";
|
||||
import {
|
||||
auditEntityRecords,
|
||||
batchSafeEditRecords,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { canRefreshCorpAccessToken, hasOpenApiPersistentCredentials } from "../../core/auth/login.js";
|
||||
import { createSessionClient, getEasoClient } from "../../core/http/index.js";
|
||||
import * as api from "../../domains/platform/api.js";
|
||||
import * as api from "../../domains/platform/services.js";
|
||||
import { printJson, printKeyValue, formatSourceLabel, formatExpiresAt } from "../printer.js";
|
||||
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ import {
|
||||
listAllWorkflows,
|
||||
findWorkflowsByTaskType,
|
||||
} from "../../domains/platform/services.js";
|
||||
import * as api from "../../domains/platform/api.js";
|
||||
import * as api from "../../domains/platform/services.js";
|
||||
import { stringOption, numberOption, booleanOption } from "../parser.js";
|
||||
import { createClientsForCorp, createPiperClientForCorp } from "../helpers.js";
|
||||
import { printJson, printTable, printKeyValue } from "../printer.js";
|
||||
|
||||
@@ -1569,6 +1569,7 @@ export async function inspectFormSpecification(
|
||||
}
|
||||
|
||||
export {
|
||||
getRawBillFlow,
|
||||
getFormSpecification,
|
||||
type FormSpecification,
|
||||
type FormSpecificationComponent,
|
||||
|
||||
@@ -9,6 +9,64 @@ import type {
|
||||
DatalinkRecordPage,
|
||||
} from "./types.js";
|
||||
|
||||
export type { SafeRecordEditOptions } from "./api.js";
|
||||
|
||||
export function listPlatforms(...args: Parameters<typeof api.listPlatforms>) {
|
||||
return api.listPlatforms(...args);
|
||||
}
|
||||
|
||||
export function listAllPlatforms(...args: Parameters<typeof api.listAllPlatforms>) {
|
||||
return api.listAllPlatforms(...args);
|
||||
}
|
||||
|
||||
export function getPlatform(...args: Parameters<typeof api.getPlatform>) {
|
||||
return api.getPlatform(...args);
|
||||
}
|
||||
|
||||
export function listPlatformEntities(...args: Parameters<typeof api.listPlatformEntities>) {
|
||||
return api.listPlatformEntities(...args);
|
||||
}
|
||||
|
||||
export function searchEntityRecords(...args: Parameters<typeof api.searchEntityRecords>) {
|
||||
return api.searchEntityRecords(...args);
|
||||
}
|
||||
|
||||
export function listImportJobs(...args: Parameters<typeof api.listImportJobs>) {
|
||||
return api.listImportJobs(...args);
|
||||
}
|
||||
|
||||
export function getImportResult(...args: Parameters<typeof api.getImportResult>) {
|
||||
return api.getImportResult(...args);
|
||||
}
|
||||
|
||||
export function toggleRecordState(...args: Parameters<typeof api.toggleRecordState>) {
|
||||
return api.toggleRecordState(...args);
|
||||
}
|
||||
|
||||
export function getRecordDetail(...args: Parameters<typeof api.getRecordDetail>) {
|
||||
return api.getRecordDetail(...args);
|
||||
}
|
||||
|
||||
export function getRecordFormSchema(...args: Parameters<typeof api.getRecordFormSchema>) {
|
||||
return api.getRecordFormSchema(...args);
|
||||
}
|
||||
|
||||
export function editRecord(...args: Parameters<typeof api.editRecord>) {
|
||||
return api.editRecord(...args);
|
||||
}
|
||||
|
||||
export function buildSafeRecordEditPayload(...args: Parameters<typeof api.buildSafeRecordEditPayload>) {
|
||||
return api.buildSafeRecordEditPayload(...args);
|
||||
}
|
||||
|
||||
export function safeEditRecord(...args: Parameters<typeof api.safeEditRecord>) {
|
||||
return api.safeEditRecord(...args);
|
||||
}
|
||||
|
||||
export function recomputeRecordLedger(...args: Parameters<typeof api.recomputeRecordLedger>) {
|
||||
return api.recomputeRecordLedger(...args);
|
||||
}
|
||||
|
||||
function matches(value: unknown, target: string): boolean {
|
||||
return typeof value === "string" && value.toLowerCase() === target.toLowerCase();
|
||||
}
|
||||
|
||||
@@ -16,6 +16,82 @@ export interface PlatformClients {
|
||||
easoClient: KyInstance;
|
||||
}
|
||||
|
||||
export function listPlatforms(...args: Parameters<typeof api.listPlatforms>) {
|
||||
return api.listPlatforms(...args);
|
||||
}
|
||||
|
||||
export function listWorkflows(...args: Parameters<typeof api.listWorkflows>) {
|
||||
return api.listWorkflows(...args);
|
||||
}
|
||||
|
||||
export function getWorkflowStats(...args: Parameters<typeof api.getWorkflowStats>) {
|
||||
return api.getWorkflowStats(...args);
|
||||
}
|
||||
|
||||
export function getWorkflowCharge(...args: Parameters<typeof api.getWorkflowCharge>) {
|
||||
return api.getWorkflowCharge(...args);
|
||||
}
|
||||
|
||||
export function getWorkflowDetail(...args: Parameters<typeof api.getWorkflowDetail>) {
|
||||
return api.getWorkflowDetail(...args);
|
||||
}
|
||||
|
||||
export function updateWorkflow(...args: Parameters<typeof api.updateWorkflow>) {
|
||||
return api.updateWorkflow(...args);
|
||||
}
|
||||
|
||||
export function listWorkflowSnapshots(...args: Parameters<typeof api.listWorkflowSnapshots>) {
|
||||
return api.listWorkflowSnapshots(...args);
|
||||
}
|
||||
|
||||
export function listWorkflowLogs(...args: Parameters<typeof api.listWorkflowLogs>) {
|
||||
return api.listWorkflowLogs(...args);
|
||||
}
|
||||
|
||||
export function toggleWorkflow(...args: Parameters<typeof api.toggleWorkflow>) {
|
||||
return api.toggleWorkflow(...args);
|
||||
}
|
||||
|
||||
export function listRunningJobs(...args: Parameters<typeof api.listRunningJobs>) {
|
||||
return api.listRunningJobs(...args);
|
||||
}
|
||||
|
||||
export function getJob(...args: Parameters<typeof api.getJob>) {
|
||||
return api.getJob(...args);
|
||||
}
|
||||
|
||||
export function getJobExecutionTree(...args: Parameters<typeof api.getJobExecutionTree>) {
|
||||
return api.getJobExecutionTree(...args);
|
||||
}
|
||||
|
||||
export function getJobFailedExecutions(...args: Parameters<typeof api.getJobFailedExecutions>) {
|
||||
return api.getJobFailedExecutions(...args);
|
||||
}
|
||||
|
||||
export function getJobExecutionLevel(...args: Parameters<typeof api.getJobExecutionLevel>) {
|
||||
return api.getJobExecutionLevel(...args);
|
||||
}
|
||||
|
||||
export function getExecutionLogs(...args: Parameters<typeof api.getExecutionLogs>) {
|
||||
return api.getExecutionLogs(...args);
|
||||
}
|
||||
|
||||
export function listDsl(...args: Parameters<typeof api.listDsl>) {
|
||||
return api.listDsl(...args);
|
||||
}
|
||||
|
||||
export function listStaffs(...args: Parameters<typeof api.listStaffs>) {
|
||||
return api.listStaffs(...args);
|
||||
}
|
||||
|
||||
export function getWebhookInfo(...args: Parameters<typeof api.getWebhookInfo>) {
|
||||
return api.getWebhookInfo(...args);
|
||||
}
|
||||
|
||||
export function sampleWorkflowData(...args: Parameters<typeof api.sampleWorkflowData>) {
|
||||
return api.sampleWorkflowData(...args);
|
||||
}
|
||||
|
||||
// ── 服务层:跨应用聚合 ───────────────────────────────────────────────────────
|
||||
|
||||
export interface ListAllWorkflowsOptions {
|
||||
|
||||
Reference in new issue
Block a user