NODEDC_PLATFORM/packages/external-provider-contract/test/l2-execution-plan.test.mjs

122 lines
4.9 KiB
JavaScript

import assert from "node:assert/strict";
import {
L2_EXECUTION_PLAN_SCHEMA_VERSION,
L2_MATERIALIZATION_RECEIPT_SCHEMA_VERSION,
attestL2ExecutionPlanMaterialization,
compileL2ExecutionPlan,
instantiateL2Connection,
validateL2ExecutionPlan,
} from "../src/index.mjs";
import {
geliosProviderPackageV8,
geliosTelemetryFieldRegistryV1,
} from "../providers/gelios/v8/index.mjs";
const positionsConnection = instantiateL2Connection(geliosProviderPackageV8, {
tenantId: "tenant-compiler-fixture",
connectionId: "gelios-compiler-fixture",
collectionProfileId: "gelios.positions.current.realtime.v7",
providerCredentialRef: "ndc-credref:provider-compiler-fixture-0001",
});
const positionsPlan = compileL2ExecutionPlan(
geliosProviderPackageV8,
positionsConnection,
{ telemetryFieldRegistry: geliosTelemetryFieldRegistryV1 },
);
assert.equal(positionsPlan.schemaVersion, L2_EXECUTION_PLAN_SCHEMA_VERSION);
assert.deepEqual(validateL2ExecutionPlan(positionsPlan), { ok: true, errors: [] });
assert.match(positionsPlan.executionPlanDigest, /^sha256:[a-f0-9]{64}$/);
assert.match(positionsPlan.artifacts.packageDigest, /^sha256:[a-f0-9]{64}$/);
assert.match(positionsPlan.artifacts.mappingContractDigest, /^sha256:[a-f0-9]{64}$/);
assert.match(positionsPlan.artifacts.telemetryRegistryDigest, /^sha256:[a-f0-9]{64}$/);
assert.deepEqual(positionsPlan.steps.map((step) => step.kind), [
"collection_trigger",
"provider_request",
"provider_request",
"extract_items",
"semantic_mapping",
"data_product_publish",
]);
const unitsRequest = positionsPlan.steps.find(
(step) => step.kind === "provider_request" && step.config.capabilityId === "gelios.units.current.read",
);
assert.deepEqual(unitsRequest.config.request.query, {
incltrip: "true",
inclcntrs: "true",
inclsnsrs: "true",
incllsv: "true",
});
const semanticMapping = positionsPlan.steps.find((step) => step.kind === "semantic_mapping");
assert.deepEqual(
semanticMapping.config.telemetryProjection.entries.map((entry) => entry.reading.id),
["sensor.param.in0", "sensor.param.in1"],
);
assert.equal(Object.isFrozen(positionsPlan), true);
assert.equal(Object.isFrozen(positionsPlan.steps), true);
const clonedPlan = compileL2ExecutionPlan(
structuredClone(geliosProviderPackageV8),
structuredClone(positionsConnection),
{ telemetryFieldRegistry: structuredClone(geliosTelemetryFieldRegistryV1) },
);
assert.equal(clonedPlan.executionPlanDigest, positionsPlan.executionPlanDigest);
assert.throws(
() => compileL2ExecutionPlan(geliosProviderPackageV8, positionsConnection),
/telemetry_registry_required/,
);
const profileConnection = instantiateL2Connection(geliosProviderPackageV8, {
tenantId: "tenant-compiler-fixture",
connectionId: "gelios-profile-compiler-fixture",
collectionProfileId: "gelios.units.profile.cold.v1",
providerCredentialRef: "ndc-credref:provider-compiler-fixture-0001",
});
const profilePlan = compileL2ExecutionPlan(geliosProviderPackageV8, profileConnection);
assert.deepEqual(validateL2ExecutionPlan(profilePlan), { ok: true, errors: [] });
assert.equal(profilePlan.artifacts.telemetryRegistryDigest, undefined);
assert.notEqual(profilePlan.executionPlanDigest, positionsPlan.executionPlanDigest);
const secondConnection = instantiateL2Connection(geliosProviderPackageV8, {
tenantId: "tenant-compiler-fixture",
connectionId: "gelios-compiler-fixture-two",
collectionProfileId: "gelios.positions.current.realtime.v7",
providerCredentialRef: "ndc-credref:provider-compiler-fixture-0002",
});
const secondPlan = compileL2ExecutionPlan(
geliosProviderPackageV8,
secondConnection,
{ telemetryFieldRegistry: geliosTelemetryFieldRegistryV1 },
);
assert.notEqual(secondPlan.executionPlanDigest, positionsPlan.executionPlanDigest);
const receipt = attestL2ExecutionPlanMaterialization(positionsPlan, {
graphRevision: "engine-revision-fixture-0001",
graphDigest: "a".repeat(64),
materializedStepIds: positionsPlan.steps.map((step) => step.id),
});
assert.equal(receipt.schemaVersion, L2_MATERIALIZATION_RECEIPT_SCHEMA_VERSION);
assert.equal(receipt.executionPlanDigest, positionsPlan.executionPlanDigest);
assert.equal(receipt.graphDigest, `sha256:${"a".repeat(64)}`);
assert.match(receipt.receiptDigest, /^sha256:[a-f0-9]{64}$/);
assert.equal(Object.isFrozen(receipt), true);
assert.throws(() => attestL2ExecutionPlanMaterialization(positionsPlan, {
graphRevision: "engine-revision-fixture-0001",
graphDigest: "a".repeat(64),
materializedStepIds: positionsPlan.steps.slice(1).map((step) => step.id),
}), /materializedStepIds_must_exactly_match/);
const tamperedPlan = structuredClone(positionsPlan);
tamperedPlan.steps[0].config.mode = "history";
assert.equal(
validateL2ExecutionPlan(tamperedPlan).errors.includes("executionPlan.executionPlanDigest_mismatch"),
true,
);
const serialized = JSON.stringify(positionsPlan);
assert.equal(serialized.includes("Bearer "), false);
assert.equal(serialized.includes("access_token"), false);
assert.equal(serialized.includes("refresh_token"), false);