import assert from "node:assert/strict" import { HEAD_DOMAIN, POLICY_DOMAIN, sha } from "./application-writer-profile-format.js" import { stagedModelClient, syntheticFixture } from "./application-writer-profile-registry-read-model.test.js" import { approvedRuntimeWriterProfile } from "./database-runtime-writer-profile.js" import { POLL_WRITER_PROFILE_DEFINITION_SHA256, POLL_WRITER_PROFILE_MANIFEST_SHA256 } from "./server-poll-writer-profile.js" const { subject, policy: basePolicy, state, signBody } = syntheticFixture("1") const policy = { ...basePolicy, definitionSha256: POLL_WRITER_PROFILE_DEFINITION_SHA256, manifestSha256: POLL_WRITER_PROFILE_MANIFEST_SHA256 } const expected = { installationId: subject.installationId, source: policy.source, roles: policy.roles } function publish(next: typeof policy, previous = state.latest[0]) { const policyBytes = signBody(next, POLICY_DOMAIN) const policyHash = sha(policyBytes) const headBytes = signBody({ format: "application-writer-head-v1", installationId: next.installationId, epoch: next.epoch, policySha256: policyHash, previousHeadSha256: next.epoch === "1" ? null : previous.head_sha256 }, HEAD_DOMAIN) const top = { ...previous, epoch: next.epoch, head_sha256: sha(headBytes), head_bytes: headBytes, policy_sha256: policyHash, previous_head_sha256: next.epoch === "1" ? null : previous.head_sha256, previous_epoch: next.epoch === "1" ? null : previous.epoch, previous_installation_id: next.epoch === "1" ? null : previous.installation_id } const current = { installation_id: top.installation_id, epoch: top.epoch, head_sha256: top.head_sha256 } return { ...state, current: [current], latest: next.epoch === "1" ? [top] : [top, previous], policy: [{ policy_sha256: policyHash, policy_bytes: policyBytes }] } } const initial = publish(policy) const approval = Buffer.from(JSON.stringify({ format: "database-runtime-writer-approval-v1", publicKeyPem: subject.publicKeyPem, publicKeySpkiSha256: subject.publicKeySpkiSha256, minimumEpoch: "1", minimumHeadSha256: initial.current[0].head_sha256 })) const guard = approvedRuntimeWriterProfile(approval, expected) assert.throws(guard.assertLive, /истёк/) await guard.check(stagedModelClient(initial).client) assert.doesNotThrow(guard.assertLive) await assert.rejects(guard.check(stagedModelClient(publish({ ...policy, definitionSha256: basePolicy.definitionSha256 })).client), /не подтверждён/) await assert.rejects(guard.check(stagedModelClient(publish({ ...policy, catalogSha256: "e".repeat(64) })).client), /откатилось/) await assert.rejects(guard.check(stagedModelClient(publish({ ...policy, epoch: "2", roles: { ...policy.roles, capture: "99" } }, initial.latest[0])).client), /не подтверждён/) await guard.check(stagedModelClient(publish({ ...policy, epoch: "2" }, initial.latest[0])).client) await assert.rejects(guard.check(stagedModelClient(initial).client), /откатилось/) console.log("Runtime writer profile: pinned genesis, role binding, renewal and rollback refusal passed")