Initial reliable release of consent engine
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Cookie Consent Integration Test Lab</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: system-ui, sans-serif;
|
||||
max-width: 900px;
|
||||
margin: 40px auto;
|
||||
padding: 0 20px;
|
||||
line-height: 1.5;
|
||||
}
|
||||
button {
|
||||
padding: 10px 16px;
|
||||
cursor: pointer;
|
||||
}
|
||||
#summary {
|
||||
font-weight: 700;
|
||||
margin: 20px 0;
|
||||
}
|
||||
.pass { color: #176b2c; }
|
||||
.fail { color: #a11; }
|
||||
pre {
|
||||
background: #f5f5f5;
|
||||
padding: 16px;
|
||||
overflow: auto;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Cookie Consent Integration Test Lab</h1>
|
||||
<p>Browser regression suite for the Jurisdiction Engine + Consent Core boundary.</p>
|
||||
|
||||
<button id="run">Run tests</button>
|
||||
<div id="summary">Not run.</div>
|
||||
<pre id="output"></pre>
|
||||
|
||||
<script type="module">
|
||||
import {
|
||||
resolveConsentContext,
|
||||
createIntegratedConsentEngine
|
||||
} from "../../integration/index.js";
|
||||
|
||||
const output = document.getElementById("output");
|
||||
const summary = document.getElementById("summary");
|
||||
const runButton = document.getElementById("run");
|
||||
|
||||
function equal(actual, expected) {
|
||||
if (actual !== expected) {
|
||||
throw new Error(`Expected ${expected}, got ${actual}`);
|
||||
}
|
||||
}
|
||||
|
||||
function deepEqual(actual, expected) {
|
||||
if (JSON.stringify(actual) !== JSON.stringify(expected)) {
|
||||
throw new Error("Values are not deeply equal.");
|
||||
}
|
||||
}
|
||||
|
||||
function throws(fn, pattern) {
|
||||
try {
|
||||
fn();
|
||||
} catch (error) {
|
||||
if (pattern.test(error.message)) return;
|
||||
throw error;
|
||||
}
|
||||
throw new Error("Expected function to throw.");
|
||||
}
|
||||
|
||||
function test(name, fn) {
|
||||
try {
|
||||
fn();
|
||||
output.textContent += `PASS: ${name}\n`;
|
||||
return true;
|
||||
} catch (error) {
|
||||
output.textContent += `FAIL: ${name}\n${error.stack || error}\n`;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
runButton.addEventListener("click", () => {
|
||||
output.textContent = "";
|
||||
let passed = 0;
|
||||
const total = 9;
|
||||
|
||||
if (test("fixed us-ma resolves into Core context", () => {
|
||||
const context = resolveConsentContext({
|
||||
mode: "fixed", jurisdiction: "us-ma"
|
||||
});
|
||||
equal(context.resolution.jurisdiction.id, "us-ma");
|
||||
equal(context.resolution.policy.id, "us-state-privacy");
|
||||
})) passed++;
|
||||
|
||||
if (test("fixed eu resolves into Core context", () => {
|
||||
const context = resolveConsentContext({
|
||||
mode: "fixed", jurisdiction: "eu"
|
||||
});
|
||||
equal(context.resolution.jurisdiction.id, "eu");
|
||||
equal(context.resolution.policy.id, "eu-privacy");
|
||||
})) passed++;
|
||||
|
||||
if (test("global resolves into global baseline", () => {
|
||||
const context = resolveConsentContext({ mode: "global" });
|
||||
equal(context.resolution.jurisdiction.id, "global");
|
||||
equal(context.resolution.policy.id, "global-baseline");
|
||||
})) passed++;
|
||||
|
||||
if (test("integration configures Core jurisdiction", () => {
|
||||
const consent = createIntegratedConsentEngine({
|
||||
jurisdiction: { mode: "fixed", jurisdiction: "us-ma" }
|
||||
});
|
||||
const state = consent.initialize();
|
||||
equal(state.jurisdiction, "us-ma");
|
||||
equal(state.hasDecision, false);
|
||||
})) passed++;
|
||||
|
||||
if (test("jurisdiction does not grant consent", () => {
|
||||
const consent = createIntegratedConsentEngine({
|
||||
jurisdiction: { mode: "fixed", jurisdiction: "us-ma" }
|
||||
});
|
||||
const state = consent.initialize();
|
||||
equal(state.categories.necessary, "granted");
|
||||
equal(state.categories.preferences, "unknown");
|
||||
equal(state.categories.analytics, "unknown");
|
||||
equal(state.categories.marketing, "unknown");
|
||||
equal(state.hasDecision, false);
|
||||
})) passed++;
|
||||
|
||||
if (test("resolved context is exposed as frozen data", () => {
|
||||
const consent = createIntegratedConsentEngine({
|
||||
jurisdiction: { mode: "fixed", jurisdiction: "eu" }
|
||||
});
|
||||
const context = consent.getJurisdictionContext();
|
||||
equal(context.resolution.jurisdiction.id, "eu");
|
||||
equal(Object.isFrozen(context), true);
|
||||
})) passed++;
|
||||
|
||||
if (test("Core persistence uses resolved jurisdiction", () => {
|
||||
const consent = createIntegratedConsentEngine({
|
||||
jurisdiction: { mode: "fixed", jurisdiction: "us-ma" }
|
||||
});
|
||||
consent.initialize();
|
||||
consent.acceptAll();
|
||||
equal(consent.getState().jurisdiction, "us-ma");
|
||||
equal(consent.getState().hasDecision, true);
|
||||
})) passed++;
|
||||
|
||||
if (test("integration is deterministic", () => {
|
||||
const input = {
|
||||
mode: "auto",
|
||||
request: { jurisdiction: "us-ma", source: "server" },
|
||||
visitor: { jurisdiction: "eu", source: "browser" }
|
||||
};
|
||||
deepEqual(
|
||||
resolveConsentContext(input),
|
||||
resolveConsentContext(input)
|
||||
);
|
||||
})) passed++;
|
||||
|
||||
if (test("invalid jurisdiction remains an Engine error", () => {
|
||||
throws(() => resolveConsentContext({
|
||||
mode: "fixed", jurisdiction: "not-a-jurisdiction"
|
||||
}), /Unknown jurisdiction/);
|
||||
})) passed++;
|
||||
|
||||
summary.textContent = `${passed}/${total} tests passed`;
|
||||
summary.className = passed === total ? "pass" : "fail";
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,136 @@
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
import {
|
||||
resolveConsentContext,
|
||||
createIntegratedConsentEngine
|
||||
} from "../../integration/index.js";
|
||||
|
||||
function test(name, fn) {
|
||||
try {
|
||||
fn();
|
||||
console.log(`PASS ${name}`);
|
||||
} catch (error) {
|
||||
console.error(`FAIL ${name}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
test("fixed us-ma resolves into Core context", () => {
|
||||
const context = resolveConsentContext({
|
||||
mode: "fixed",
|
||||
jurisdiction: "us-ma"
|
||||
});
|
||||
|
||||
assert.equal(context.resolution.jurisdiction.id, "us-ma");
|
||||
assert.equal(context.resolution.policy.id, "us-state-privacy");
|
||||
});
|
||||
|
||||
test("fixed eu resolves into Core context", () => {
|
||||
const context = resolveConsentContext({
|
||||
mode: "fixed",
|
||||
jurisdiction: "eu"
|
||||
});
|
||||
|
||||
assert.equal(context.resolution.jurisdiction.id, "eu");
|
||||
assert.equal(context.resolution.policy.id, "eu-privacy");
|
||||
});
|
||||
|
||||
test("global resolves into global baseline", () => {
|
||||
const context = resolveConsentContext({ mode: "global" });
|
||||
|
||||
assert.equal(context.resolution.jurisdiction.id, "global");
|
||||
assert.equal(context.resolution.policy.id, "global-baseline");
|
||||
});
|
||||
|
||||
test("integration configures Core jurisdiction", () => {
|
||||
const consent = createIntegratedConsentEngine({
|
||||
jurisdiction: { mode: "fixed", jurisdiction: "us-ma" }
|
||||
});
|
||||
|
||||
const state = consent.initialize();
|
||||
|
||||
assert.equal(state.jurisdiction, "us-ma");
|
||||
assert.equal(state.hasDecision, false);
|
||||
});
|
||||
|
||||
test("jurisdiction does not grant consent", () => {
|
||||
const consent = createIntegratedConsentEngine({
|
||||
jurisdiction: { mode: "fixed", jurisdiction: "us-ma" }
|
||||
});
|
||||
|
||||
const state = consent.initialize();
|
||||
|
||||
assert.equal(state.categories.necessary, "granted");
|
||||
assert.equal(state.categories.preferences, "unknown");
|
||||
assert.equal(state.categories.analytics, "unknown");
|
||||
assert.equal(state.categories.marketing, "unknown");
|
||||
assert.equal(state.hasDecision, false);
|
||||
});
|
||||
|
||||
test("resolved context is exposed without mutable internals", () => {
|
||||
const consent = createIntegratedConsentEngine({
|
||||
jurisdiction: { mode: "fixed", jurisdiction: "eu" }
|
||||
});
|
||||
|
||||
const context = consent.getJurisdictionContext();
|
||||
|
||||
assert.equal(context.resolution.jurisdiction.id, "eu");
|
||||
assert.equal(Object.isFrozen(context), true);
|
||||
assert.equal(Object.isFrozen(context.resolution), true);
|
||||
});
|
||||
|
||||
test("Core configuration preserves resolved jurisdiction", () => {
|
||||
const consent = createIntegratedConsentEngine({
|
||||
jurisdiction: { mode: "fixed", jurisdiction: "us-ma" }
|
||||
});
|
||||
|
||||
const config = consent.getConfig();
|
||||
|
||||
assert.equal(config.jurisdiction, "us-ma");
|
||||
});
|
||||
|
||||
test("Node environment does not fake successful persistence", () => {
|
||||
const consent = createIntegratedConsentEngine({
|
||||
jurisdiction: { mode: "fixed", jurisdiction: "us-ma" }
|
||||
});
|
||||
|
||||
consent.initialize();
|
||||
const state = consent.acceptAll();
|
||||
|
||||
assert.equal(state.jurisdiction, "us-ma");
|
||||
assert.equal(state.hasDecision, false);
|
||||
assert.equal(state.categories.preferences, "unknown");
|
||||
assert.equal(state.categories.analytics, "unknown");
|
||||
assert.equal(state.categories.marketing, "unknown");
|
||||
});
|
||||
|
||||
test("integration remains deterministic", () => {
|
||||
const input = {
|
||||
mode: "auto",
|
||||
request: {
|
||||
jurisdiction: "us-ma",
|
||||
source: "server"
|
||||
},
|
||||
visitor: {
|
||||
jurisdiction: "eu",
|
||||
source: "browser"
|
||||
}
|
||||
};
|
||||
|
||||
const first = resolveConsentContext(input);
|
||||
const second = resolveConsentContext(input);
|
||||
|
||||
assert.deepEqual(first, second);
|
||||
});
|
||||
|
||||
test("invalid jurisdiction remains an Engine error", () => {
|
||||
assert.throws(
|
||||
() => resolveConsentContext({
|
||||
mode: "fixed",
|
||||
jurisdiction: "not-a-jurisdiction"
|
||||
}),
|
||||
/Unknown jurisdiction/
|
||||
);
|
||||
});
|
||||
|
||||
console.log("10 integration tests passed.");
|
||||
Reference in New Issue
Block a user