159 lines
5.0 KiB
TypeScript
159 lines
5.0 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { diagnoseTenantContract, formatTenantDoctorDashboard } from "./tenant-diagnose.js";
|
|
import type { CorpConfig } from "../config/types.js";
|
|
|
|
function createMockClient(opts: {
|
|
whoami?: any;
|
|
billing?: any;
|
|
rules?: any;
|
|
webhooks?: any;
|
|
}) {
|
|
return {
|
|
get: async (url: string) => {
|
|
if (url === "api/v1/auth/me") {
|
|
return {
|
|
json: async () => ({
|
|
code: 0,
|
|
data: opts.whoami ?? {
|
|
user: { id: "u_admin", tenantId: "t_1", name: "Admin", isAdmin: true, permissions: ["*"] },
|
|
tenant: { id: "t_1", name: "Test Corp", tenantSlug: "testcorp" }
|
|
}
|
|
})
|
|
};
|
|
}
|
|
if (url === "api/v1/billing/status") {
|
|
return {
|
|
json: async () => ({
|
|
code: 0,
|
|
data: opts.billing ?? {
|
|
features: [
|
|
{ code: "ai-approval", balance: 5000, deductionAmount: 100 },
|
|
{ code: "approval-rules", quota: 100, used: 10, remaining: 90 }
|
|
]
|
|
}
|
|
})
|
|
};
|
|
}
|
|
if (url === "api/v1/rules") {
|
|
return {
|
|
json: async () => ({
|
|
code: 0,
|
|
data: opts.rules ?? {
|
|
rules: [
|
|
{ id: "r1", description: "Rule 1", scope: "all", strictness: "MUST_FOLLOW", status: "ACTIVE" },
|
|
{ id: "r2", description: "Rule 2", scope: "all", strictness: "SUGGESTION", status: "DISABLED" }
|
|
]
|
|
}
|
|
})
|
|
};
|
|
}
|
|
if (url === "api/v1/agent/ai-approval/config" || url === "api/v1/integrations/webhook") {
|
|
return {
|
|
json: async () => ({
|
|
code: 0,
|
|
data: {
|
|
integrations: opts.webhooks ?? [
|
|
{
|
|
type: "webhook",
|
|
id: "wh_1",
|
|
name: "回放测试通道",
|
|
mode: "assisted",
|
|
incomingDocumentWebhook: { id: "in_1", url: "https://test.com/in" },
|
|
outgoingResultWebhook: "https://test.com/out",
|
|
incomingManualResultWebhook: { id: "manual_1", url: "https://test.com/manual" },
|
|
secret: "sec_123"
|
|
}
|
|
]
|
|
}
|
|
})
|
|
};
|
|
}
|
|
throw new Error(`Unhandled mock GET url: ${url}`);
|
|
}
|
|
} as any;
|
|
}
|
|
|
|
describe("tenant-diagnose", () => {
|
|
test("diagnoses a healthy test tenant with sufficient quota and bindings", async () => {
|
|
const mockClient = createMockClient({});
|
|
const testCorp: CorpConfig = {
|
|
id: "test-corp",
|
|
name: "测试企业",
|
|
environment: "test",
|
|
prodCorpId: "prod-corp",
|
|
source: { type: "seal" },
|
|
seal: { url: "https://test.sealai.cc" }
|
|
} as any;
|
|
|
|
const result = await diagnoseTenantContract(mockClient, testCorp);
|
|
|
|
expect(result.ok).toBe(true);
|
|
expect(result.verdict).toBe("HEALTHY");
|
|
expect(result.quota.balance).toBe(5000);
|
|
expect(result.quota.estimatedRuns).toBe(50);
|
|
expect(result.quota.status).toBe("sufficient");
|
|
expect(result.rules.totalCount).toBe(2);
|
|
expect(result.rules.activeCount).toBe(1);
|
|
expect(result.webhooks.hasReplayChannel).toBe(true);
|
|
expect(result.summary.failedChecks).toBe(0);
|
|
|
|
const dashboard = formatTenantDoctorDashboard(result);
|
|
expect(dashboard).toContain("HEALTHY");
|
|
expect(dashboard).toContain("5000");
|
|
});
|
|
|
|
test("flags warning for low quota and unbound prod corp", async () => {
|
|
const mockClient = createMockClient({
|
|
billing: {
|
|
features: [
|
|
{ code: "ai-approval", balance: 500, deductionAmount: 100 }
|
|
]
|
|
},
|
|
rules: { rules: [] }
|
|
});
|
|
|
|
const unboundProdCorp: CorpConfig = {
|
|
id: "prod-corp",
|
|
name: "未绑定生产企业",
|
|
environment: "production",
|
|
source: { type: "seal" },
|
|
seal: { url: "https://prod.sealai.cc" }
|
|
} as any;
|
|
|
|
const result = await diagnoseTenantContract(mockClient, unboundProdCorp);
|
|
|
|
expect(result.ok).toBe(true);
|
|
expect(result.verdict).toBe("DEGRADED");
|
|
expect(result.quota.estimatedRuns).toBe(5);
|
|
expect(result.quota.status).toBe("low");
|
|
expect(result.rules.totalCount).toBe(0);
|
|
expect(result.summary.warningChecks).toBeGreaterThan(0);
|
|
});
|
|
|
|
test("flags fail when quota is exhausted or auth fails", async () => {
|
|
const mockClient = createMockClient({
|
|
billing: {
|
|
features: [
|
|
{ code: "ai-approval", balance: 0, deductionAmount: 100 }
|
|
]
|
|
}
|
|
});
|
|
|
|
const testCorp: CorpConfig = {
|
|
id: "test-corp",
|
|
name: "额度耗尽企业",
|
|
environment: "test",
|
|
prodCorpId: "prod-corp",
|
|
source: { type: "seal" },
|
|
seal: { url: "https://test.sealai.cc" }
|
|
} as any;
|
|
|
|
const result = await diagnoseTenantContract(mockClient, testCorp);
|
|
|
|
expect(result.ok).toBe(false);
|
|
expect(result.verdict).toBe("BLOCKED");
|
|
expect(result.quota.status).toBe("exhausted");
|
|
expect(result.summary.failedChecks).toBe(1);
|
|
});
|
|
});
|