Files
0pt/tests/p0/iceberg-write-service.test.ts
T
adminandClaude Sonnet 5 35b6ec63bf Add 0pt Iceberg write-path ownership: dataset<->table mapping + source rate policy
Real, tested implementation of docs/11-lakehouse-write-plan.md's items 2-8
(the "future implementation session" that doc named): 0pt is now the
canonical writer of Iceberg tables through ST0RE's real, shipped
/v1/st0re/lakehouse/* table registry (sax3l/st0re commit 9ea0abb), never
persisting lakehouse table/catalog/snapshot state itself.

- src/types + src/services for 0pt.dataset_iceberg_mapping.schema.v1
  (dataset -> ST0RE tableId reference) and 0pt.source_rate_policy.schema.v1
  (the sourceRatePolicy ownership repair, moved out of N0D per ADR-0030).
- src/clients/st0re-lakehouse-client.ts: new client matching ST0RE's actual
  camelCase route contract, with the same mock/HTTP boundary convention as
  this repo's existing cl0ud/n0d/aud0/st0re clients. Honest caveat in its
  module doc comment: not verified against a live ST0RE instance in this
  pass (no reachable ST0RE_URL); every test exercises the deterministic mock.
- src/services/iceberg-write-service.ts: registers tables/snapshots through
  ST0RE, requests N0D jobs only for heavy writes (never executes them
  in-process), emits all four AUD0 event names
  (dataset_iceberg_table_mapped / iceberg_write_requested /
  iceberg_snapshot_created / source_rate_policy_created).
- sql/004, packages/schemas/*.json, app/api/0pt/lakehouse + sources/rate-policies
  routes, tests/p0/*.test.ts (154 passed + 1 honest it.todo for the live-ST0RE
  case, up from 142 baseline, 0 failures).
- Reconciled a real collision: an earlier untracked draft schema/test pair
  (tests/p0/enterprise-data-os-expansion.test.ts) asserted a different,
  now-superseded source_rate_policy shape; updated that one test block only.
- docs/ICEBERG_WRITE_BOUNDARY.md states the ownership boundary plainly and
  is explicit throughout about what's real-and-tested vs. contract-only.
  docs/11-lakehouse-write-plan.md gets a status-update section, not a rewrite.
- Data quality rules (quality-check-service.ts, quality-report-service.ts)
  were not touched -- confirmed still 0pt-owned.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-07-12 19:46:47 +02:00

178 lines
7.5 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { DatasetService } from "@/src/services/dataset-service";
import { DatasetIcebergMappingService } from "@/src/services/dataset-iceberg-mapping-service";
import { IcebergWriteService } from "@/src/services/iceberg-write-service";
/**
* "Write first test dataset to Iceberg through ST0RE's table registry" proof
* (task item 2). This exercises the REAL request/response shapes and control
* flow of IcebergWriteService -> St0reLakehouseClient against that client's
* deterministic MOCK implementation, the same convention this repo already
* uses for every other cross-product boundary (see
* tests/p0/cross-product-boundary.test.ts, quality-check-service.ts's own
* CL0UD/N0D/ST0RE/AUD0 usage). No ST0RE_URL was available to this pass, so
* this is NOT verified against a live, running ST0RE lakehouse API --
* src/clients/st0re-lakehouse-client.ts's module doc comment states that
* caveat explicitly and it applies here too. What IS real: the table gets
* registered, a DatasetIcebergMapping row is created referencing ST0RE's
* (mock) tableId, a snapshot gets registered against that table, and all
* four AUD0 events fire in the right order with the right payload shape.
*/
describe("IcebergWriteService", () => {
it("mapDatasetToIcebergTable registers a table in ST0RE and records the mapping", async () => {
const datasetService = new DatasetService();
const mappingService = new DatasetIcebergMappingService({ datasetService });
const service = new IcebergWriteService({ datasetService, mappingService });
await datasetService.registerDataset({
dataset_id: "ds_first_iceberg_write",
tenant_id: "t1",
project_id: "p1",
name: "First Iceberg Pilot Dataset",
dataset_type: "enriched",
});
const result = await service.mapDatasetToIcebergTable({
tenant_id: "t1",
project_id: "p1",
dataset_id: "ds_first_iceberg_write",
dataset_iceberg_mapping_id: "dim_first_write",
namespace: "0pt_pilot",
table_name: "dim_company_se_pilot",
storage_uri: "s3://siax-p0/lakehouse/0pt_pilot/dim_company_se_pilot",
catalog_ref: "0pt_pilot.dim_company_se_pilot",
actor_id: "user_123",
correlation_id: "corr_first_write",
});
expect(result.iceberg_table_id).toContain("mock_table_");
expect(result.policy_decision_id).toBeDefined();
expect(result.mapping.dataset_id).toBe("ds_first_iceberg_write");
expect(result.mapping.iceberg_table_id).toBe(result.iceberg_table_id);
expect(result.mapping.mapping_status).toBe("active");
const lookedUp = await mappingService.getMappingForDataset("ds_first_iceberg_write");
expect(lookedUp?.dataset_iceberg_mapping_id).toBe("dim_first_write");
});
it("requestIcebergWrite registers a snapshot through ST0RE for an existing mapping", async () => {
const datasetService = new DatasetService();
const mappingService = new DatasetIcebergMappingService({ datasetService });
const service = new IcebergWriteService({ datasetService, mappingService });
await datasetService.registerDataset({
dataset_id: "ds_snapshot_write",
tenant_id: "t1",
project_id: "p1",
name: "Snapshot Write Dataset",
dataset_type: "enriched",
});
const mapped = await service.mapDatasetToIcebergTable({
tenant_id: "t1",
project_id: "p1",
dataset_id: "ds_snapshot_write",
dataset_iceberg_mapping_id: "dim_snapshot_write",
namespace: "0pt_pilot",
table_name: "dim_company_se_snapshot",
storage_uri: "s3://siax-p0/lakehouse/0pt_pilot/dim_company_se_snapshot",
catalog_ref: "0pt_pilot.dim_company_se_snapshot",
actor_id: "user_123",
correlation_id: "corr_snapshot_setup",
});
const result = await service.requestIcebergWrite({
dataset_iceberg_mapping_id: mapped.mapping.dataset_iceberg_mapping_id,
actor_id: "user_123",
correlation_id: "corr_snapshot_write",
schema_version: "1",
manifest_uri: "s3://siax-p0/lakehouse/0pt_pilot/dim_company_se_snapshot/metadata/manifest-1.avro",
record_count: 1000,
size_bytes: 204800,
});
expect(result.snapshot_id).toContain("mock_snapshot_");
expect(result.iceberg_table_id).toBe(mapped.iceberg_table_id);
expect(result.n0d_job_id).toBeUndefined(); // not a heavy write
});
it("requestIcebergWrite routes heavy writes through an N0D job request, not in-process execution", async () => {
const datasetService = new DatasetService();
const mappingService = new DatasetIcebergMappingService({ datasetService });
const service = new IcebergWriteService({ datasetService, mappingService });
await datasetService.registerDataset({
dataset_id: "ds_heavy_write",
tenant_id: "t1",
project_id: "p1",
name: "Heavy Backfill Dataset",
dataset_type: "raw",
});
const mapped = await service.mapDatasetToIcebergTable({
tenant_id: "t1",
project_id: "p1",
dataset_id: "ds_heavy_write",
dataset_iceberg_mapping_id: "dim_heavy_write",
namespace: "0pt_pilot",
table_name: "fact_financials_backfill",
storage_uri: "s3://siax-p0/lakehouse/0pt_pilot/fact_financials_backfill",
catalog_ref: "0pt_pilot.fact_financials_backfill",
actor_id: "user_123",
correlation_id: "corr_heavy_setup",
});
const result = await service.requestIcebergWrite({
dataset_iceberg_mapping_id: mapped.mapping.dataset_iceberg_mapping_id,
actor_id: "user_123",
correlation_id: "corr_heavy_write",
schema_version: "1",
manifest_uri: "s3://siax-p0/lakehouse/0pt_pilot/fact_financials_backfill/metadata/manifest-backfill.avro",
heavy: true,
});
expect(result.n0d_job_id).toBeDefined();
expect(result.n0d_job_id).toContain("mock_job_");
});
it("requestIcebergWrite rejects writes against a non-active mapping", async () => {
const datasetService = new DatasetService();
const mappingService = new DatasetIcebergMappingService({ datasetService });
const service = new IcebergWriteService({ datasetService, mappingService });
await datasetService.registerDataset({
dataset_id: "ds_deprecated_mapping",
tenant_id: "t1",
project_id: "p1",
name: "Deprecated Mapping Dataset",
dataset_type: "raw",
});
await mappingService.createMapping({
dataset_iceberg_mapping_id: "dim_deprecated",
tenant_id: "t1",
project_id: "p1",
dataset_id: "ds_deprecated_mapping",
iceberg_table_id: "st0re_table_deprecated",
mapping_status: "deprecated",
});
await expect(
service.requestIcebergWrite({
dataset_iceberg_mapping_id: "dim_deprecated",
actor_id: "user_123",
correlation_id: "corr_deprecated_write",
schema_version: "1",
manifest_uri: "s3://siax-p0/lakehouse/deprecated/manifest.avro",
})
).rejects.toThrow("is not active");
});
// TODO(future session): once ST0RE_URL points at a live, reachable ST0RE
// deployment (sax3l/st0re, /v1/st0re/lakehouse/*, port 8090 per that repo's
// STATUS.md), add a live-integration test here that constructs
// IcebergWriteService with { st0reBaseUrl: process.env.ST0RE_URL } and
// asserts the same flow against the real HTTP API. Not built in this pass
// -- no reachable ST0RE endpoint was available to verify against. See
// src/clients/st0re-lakehouse-client.ts's module doc comment and
// docs/ICEBERG_WRITE_BOUNDARY.md "Verification status" section.
it.todo("mapDatasetToIcebergTable against a LIVE ST0RE deployment (ST0RE_URL) -- not live-verified in this pass");
});