Files
0pt/tests/p0/quality-service.test.ts
T
admin 2d34cb1629
CI (SIAX Cloud) / security (pull_request) Successful in 16s
CI (SIAX Cloud) / security (push) Successful in 16s
Security Scan (SIAX Cloud) / security (push) Successful in 17s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 23s
CI (SIAX Cloud) / integration (push) Failing after 27s
CI (SIAX Cloud) / integration (pull_request) Failing after 31s
CI (SIAX Cloud) / quality (pull_request) Successful in 42s
CI (SIAX Cloud) / quality (push) Successful in 42s
feat(P-001, P-002): 0pt→N0D quality delegation + internal OpenAPI
2026-09-08 22:38:47 +02:00

248 lines
9.5 KiB
TypeScript

/**
* QualityService + ExpectationEngine unit tests (Master-DoD §10 Data
* quality): versioned rule registry, runs, results, issues, score, trend,
* GE delegation (never faked), cross-tenant isolation.
*/
import { describe, it, expect } from "vitest";
import { readFileSync } from "node:fs";
import { resolve, dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { QualityService } from "@/src/quality/quality-service";
import { ExpectationEngine } from "@/src/quality/expectation-engine";
import { SiaxError } from "@/src/platform/errors";
const ROWS = [
{ org_nr: "5560360793", name: "Volvo", employees: 1000 },
{ org_nr: "5560125790", name: "Ericsson", employees: 90000 },
{ org_nr: null, name: "Broken", employees: -5 },
];
async function withRules() {
const q = new QualityService();
await q.createRule({
rule_id: "r_orgnr_notnull",
tenant_id: "t_a",
project_id: "p_1",
dataset_id: "ds_companies",
name: "org_nr must be present",
expectation: { expectation_type: "expect_column_values_to_not_be_null", kwargs: { column: "org_nr" } },
severity: "critical",
});
await q.createRule({
rule_id: "r_employees_range",
tenant_id: "t_a",
project_id: "p_1",
dataset_id: "ds_companies",
name: "employees in sane range",
expectation: {
expectation_type: "expect_column_values_to_be_between",
kwargs: { column: "employees", min_value: 0, max_value: 2_000_000 },
},
severity: "warning",
supplied_by: "vehiq",
});
return q;
}
describe("ExpectationEngine — GE core subset", () => {
const engine = new ExpectationEngine();
it("executes the supported expectation types with GE semantics", () => {
expect(
engine.execute({ expectation_type: "expect_column_values_to_not_be_null", kwargs: { column: "org_nr" } }, ROWS)
).toMatchObject({ success: false, unexpected_count: 1, evaluated_count: 3 });
expect(
engine.execute({ expectation_type: "expect_column_values_to_be_unique", kwargs: { column: "name" } }, ROWS)
).toMatchObject({ success: true });
// Nulls do not fail range checks (GE semantics); -5 does.
expect(
engine.execute(
{ expectation_type: "expect_column_values_to_be_between", kwargs: { column: "employees", min_value: 0 } },
ROWS
)
).toMatchObject({ success: false, unexpected_count: 1 });
expect(
engine.execute(
{ expectation_type: "expect_column_values_to_match_regex", kwargs: { column: "org_nr", regex: "^\\d{10}$" } },
ROWS
)
).toMatchObject({ success: true }); // null skipped per GE semantics
expect(
engine.execute(
{ expectation_type: "expect_table_row_count_to_be_between", kwargs: { min_value: 1, max_value: 2 } },
ROWS
)
).toMatchObject({ success: false });
expect(
engine.execute({ expectation_type: "expect_column_to_exist", kwargs: { column: "employees" } }, ROWS)
).toMatchObject({ success: true });
});
it("NEVER silently passes an unsupported expectation type", () => {
expect(() =>
engine.execute({ expectation_type: "expect_column_kl_divergence_to_be_less_than", kwargs: {} }, ROWS)
).toThrowError(SiaxError);
});
});
describe("QualityService — rule registry with versioning (§10)", () => {
it("createRule v1, updateRule bumps version and records the superseded version", async () => {
const q = await withRules();
const updated = await q.updateRule("r_orgnr_notnull", "t_a", { severity: "error" });
expect(updated.version).toBe(2);
const versions = q.getRuleVersions("r_orgnr_notnull", "t_a");
expect(versions.map((v) => [v.version, v.status])).toEqual([
[1, "superseded"],
[2, "active"],
]);
});
it("duplicate rule_id is CONFLICT; cross-tenant reads are NOT_FOUND (§50)", async () => {
const q = await withRules();
await expect(
q.createRule({
rule_id: "r_orgnr_notnull",
tenant_id: "t_a",
project_id: "p_1",
dataset_id: "ds_companies",
name: "dup",
expectation: { expectation_type: "expect_column_to_exist", kwargs: { column: "x" } },
})
).rejects.toMatchObject({ code: "CONFLICT" });
expect(() => q.getRule("r_orgnr_notnull", "t_b")).toThrowError(/not found/);
expect(q.listRules({ tenant_id: "t_b" })).toEqual([]);
});
it("products supply rules (supplied_by is preserved)", async () => {
const q = await withRules();
expect(q.getRule("r_employees_range", "t_a").supplied_by).toBe("vehiq");
});
});
describe("QualityService — runs, results, issues, score, trend", () => {
it("runs all active rules locally, opens issues for failures, computes a severity-weighted score", async () => {
const q = await withRules();
const run = await q.runQuality({
tenant_id: "t_a",
project_id: "p_1",
dataset_id: "ds_companies",
rows: ROWS,
correlation_id: "corr-q1",
});
expect(run.status).toBe("failed"); // both rules have failures
expect(run.results).toHaveLength(2);
expect(run.results.every((r) => r.executed_by === "0pt-local")).toBe(true);
expect(run.score).toBeGreaterThan(0);
expect(run.score).toBeLessThan(100);
const issues = q.listIssues({ tenant_id: "t_a", dataset_id: "ds_companies", status: "open" });
expect(issues).toHaveLength(2);
expect(issues.find((i) => i.rule_id === "r_orgnr_notnull")?.severity).toBe("critical");
// Resolve one issue.
const resolved = await q.resolveIssue(issues[0].issue_id, "t_a", "resolved");
expect(resolved.status).toBe("resolved");
expect(q.listIssues({ tenant_id: "t_a", status: "open" })).toHaveLength(1);
});
it("a clean dataset yields status succeeded, score 100 and no issues", async () => {
const q = await withRules();
const run = await q.runQuality({
tenant_id: "t_a",
project_id: "p_1",
dataset_id: "ds_companies",
rows: ROWS.slice(0, 2),
});
expect(run.status).toBe("succeeded");
expect(run.score).toBe(100);
expect(q.listIssues({ tenant_id: "t_a" })).toHaveLength(0);
});
it("trend is the chronological score series for the dataset (§10 trend)", async () => {
const q = await withRules();
await q.runQuality({ tenant_id: "t_a", project_id: "p_1", dataset_id: "ds_companies", rows: ROWS });
await q.runQuality({ tenant_id: "t_a", project_id: "p_1", dataset_id: "ds_companies", rows: ROWS.slice(0, 2) });
const trend = q.getTrend("t_a", "ds_companies");
expect(trend).toHaveLength(2);
expect(trend[1].score).toBe(100);
expect(trend[0].score).toBeLessThan(trend[1].score);
});
it("running with zero active rules is NOT_FOUND, not a fake success", async () => {
const q = new QualityService();
await expect(
q.runQuality({ tenant_id: "t_a", project_id: "p_1", dataset_id: "ds_empty", rows: [] })
).rejects.toMatchObject({ code: "NOT_FOUND" });
});
});
describe("QualityService — GE delegation (§11 DQ, never faked)", () => {
it("delegates the WHOLE run when any rule's expectation is not locally executable", async () => {
const q = await withRules();
await q.createRule({
rule_id: "r_kl_divergence",
tenant_id: "t_a",
project_id: "p_1",
dataset_id: "ds_companies",
name: "distribution check (GE only)",
expectation: { expectation_type: "expect_column_kl_divergence_to_be_less_than", kwargs: { column: "employees" } },
});
const run = await q.runQuality({
tenant_id: "t_a",
project_id: "p_1",
dataset_id: "ds_companies",
rows: ROWS,
});
expect(run.status).toBe("delegated");
expect(run.delegated_job_id).toContain("mock_job_"); // test runtime: N0D mock
expect(run.results).toEqual([]); // NOTHING fabricated
expect(run.score).toBeUndefined();
});
it("delegates when no rows are supplied (engine cannot run on nothing)", async () => {
const q = await withRules();
const run = await q.runQuality({ tenant_id: "t_a", project_id: "p_1", dataset_id: "ds_companies" });
expect(run.status).toBe("delegated");
});
it("recordDelegatedResults finishes a delegated run; a finished run cannot be finished again", async () => {
const q = await withRules();
const run = await q.runQuality({ tenant_id: "t_a", project_id: "p_1", dataset_id: "ds_companies" });
const finished = await q.recordDelegatedResults(run.run_id, "t_a", [
{
rule_id: "r_orgnr_notnull",
rule_version: 1,
success: true,
pass_ratio: 1,
evaluated_count: 3,
unexpected_count: 0,
unexpected_samples: [],
executed_by: "great_expectations",
},
]);
expect(finished.status).toBe("succeeded");
expect(finished.results[0].executed_by).toBe("great_expectations");
await expect(q.recordDelegatedResults(run.run_id, "t_a", [])).rejects.toMatchObject({ code: "CONFLICT" });
});
it("P-002: n0dGetJobResult is registered in the 0pt OpenAPI catalog (0pt.n0dGetJobResult)", () => {
// Verify that the n0dGetJobResult operationId is present in the 0pt internal
// OpenAPI contract (contracts/openapi.yaml) — it is the P-001 owner-seam
// retrieval endpoint for delegated quality jobs.
const catalog = readFileSync(
resolve(dirname(fileURLToPath(import.meta.url)), "../../contracts/openapi.yaml"),
"utf8"
);
expect(catalog).toContain("operationId: 0pt.n0dGetJobResult");
expect(catalog).toContain("/api/0pt/n0d/results/{jobId}");
expect(catalog).toContain("operationId: 0pt.quality.run");
expect(catalog).toContain("operationId: 0pt.quality.checks");
expect(catalog).toContain("operationId: 0pt.quality.reports");
});
});