Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c134527509 | ||
|
|
1c87b82e70 | ||
|
|
2419b76eb2 | ||
|
|
cf46fe6ac8 | ||
|
|
8c562ad37b | ||
|
|
c4356e83c7 | ||
|
|
4cd7d7a5e5 | ||
|
|
cb018d9898 | ||
|
|
04813c34b0 | ||
|
|
a32a64e91d | ||
|
|
c1648c04a7 | ||
|
|
c9a05be47e | ||
|
|
09b9cc80bf | ||
|
|
f44dd3e621 | ||
|
|
286f636ca6 | ||
|
|
b0a3db79e7 | ||
|
|
5a165f3478 | ||
|
|
a8cb0149ea | ||
|
|
0386d82c49 | ||
|
|
24912b735f | ||
|
|
18007ef0cb | ||
|
|
a8806c32aa | ||
|
|
76a77eefac | ||
|
|
9a14f65121 | ||
|
|
de7a706d7f | ||
|
|
51076ffe69 | ||
|
|
9ca82443be | ||
|
|
3dcc46c064 | ||
|
|
7a84899b37 | ||
|
|
e7d5ee3574 | ||
|
|
9d28b26490 | ||
|
|
0b7bda840e | ||
|
|
bcc5c7feb5 | ||
|
|
b382e3b8ba | ||
|
|
c3582838aa | ||
|
|
43bc27703a | ||
|
|
bc76ebc5e5 | ||
|
|
98c6ec459f | ||
|
|
a2377215e7 | ||
|
|
3e122572b3 | ||
|
|
cc363a790f | ||
|
|
5ffd41a461 | ||
|
|
e24f8e8af4 | ||
|
|
3279fa0533 | ||
|
|
1b6140a16a | ||
|
|
a49bc148fc | ||
|
|
f606c4a7d7 | ||
|
|
bdca3c3305 | ||
|
|
b2fcebe2b5 | ||
|
|
8b338f8c07 | ||
|
|
9f34dd880c | ||
|
|
b205406ff6 | ||
|
|
b5a94c3dc5 | ||
|
|
d3afc40ef4 | ||
|
|
1ff689c7e7 | ||
|
|
c9af814cbe | ||
|
|
67ee239c84 | ||
|
|
4c47e151f3 | ||
|
|
2d757435e4 | ||
|
|
e3930aa633 | ||
|
|
9f52ceb1f7 | ||
|
|
6b733f7b9c | ||
|
|
b89b1e81de | ||
|
|
65ee88a01c | ||
|
|
ff7c5cdd66 | ||
|
|
b982a1a643 | ||
|
|
a2d6352e09 | ||
|
|
125fc0ff66 | ||
|
|
65594902bd | ||
|
|
f4f660715b | ||
|
|
410339e697 | ||
|
|
07158099ee | ||
|
|
189d51fb62 | ||
|
|
ec487746fd | ||
|
|
0062edbec3 | ||
|
|
ccbf5878bd | ||
|
|
6f9034ff1d |
@@ -21,6 +21,7 @@ on:
|
||||
jobs:
|
||||
deploy:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 20
|
||||
env:
|
||||
COOLIFY_APP_ID: ${{ vars.COOLIFY_APP_ID || 'TODO-no-coolify-app-provisioned-yet' }}
|
||||
IMAGE_REPO: git.cloud.siax.io/sax3l/0pt
|
||||
|
||||
@@ -24,6 +24,7 @@ on:
|
||||
jobs:
|
||||
quality:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 30
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-node@v4
|
||||
@@ -37,6 +38,13 @@ jobs:
|
||||
run: pnpm install --frozen-lockfile
|
||||
- name: Typecheck (tsc over src/ + tests/ -- blocking)
|
||||
run: pnpm run typecheck
|
||||
# UI-09: 0pt carried 82 axe color-contrast violations (impact=serious), the
|
||||
# estate's largest single a11y defect, and every one came from a Tailwind
|
||||
# alpha modifier on a text colour. The unmodified token measures 4.73:1
|
||||
# light / 7.63:1 dark and passes AA; every faded variant fails. Blocking,
|
||||
# because a contrast regression is invisible in review.
|
||||
- name: Text contrast shape (WCAG 2.2 AA 1.4.3 -- blocking)
|
||||
run: pnpm run check:contrast
|
||||
- name: Test (vitest -- blocking)
|
||||
run: pnpm run test
|
||||
- name: Lint (eslint -- advisory; legacy tree has pre-existing noise)
|
||||
@@ -69,6 +77,7 @@ jobs:
|
||||
# ordinary branch pushes still run blocking quality/security.
|
||||
if: github.event_name == 'pull_request' || github.ref == 'refs/heads/main' || github.event_name == 'workflow_dispatch'
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 45
|
||||
services:
|
||||
opt-ci-opensearch:
|
||||
image: opensearchproject/opensearch:2.11.1
|
||||
@@ -219,6 +228,7 @@ jobs:
|
||||
# measurable blind spot into declared policy.
|
||||
security:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 15
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install Trivy
|
||||
@@ -227,3 +237,31 @@ jobs:
|
||||
- name: Trivy scan (vuln + secrets -- BLOCKING, fails on HIGH/CRITICAL)
|
||||
run: |
|
||||
trivy fs --scanners vuln,secret --severity HIGH,CRITICAL --exit-code 1 .
|
||||
|
||||
sonar:
|
||||
# SonarQube Community Build har inget separat PR-analysläge (en enda "main"-gren
|
||||
# per projekt) — en analys från en PR-branch eller en tagg-push som inte pekar på
|
||||
# main skulle skriva över kvalitetshistoriken. Kör därför bara
|
||||
# vid push till main.
|
||||
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 20
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: SonarQube analysis (self-hosted sonar.siax.io)
|
||||
env:
|
||||
SONAR_HOST_URL: https://sonar.siax.io
|
||||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
test -n "$SONAR_TOKEN" || { echo "::error::SONAR_TOKEN repo secret missing"; exit 1; }
|
||||
# Jobbet kör i en container på act_runner; "docker run -v \"\$PWD:...\""
|
||||
# skulle montera VÄRDENS (tomma) sökväg. Kopiera in källträdet istället.
|
||||
cid=$(docker create -e SONAR_HOST_URL -e SONAR_TOKEN -w /usr/src \
|
||||
sonarsource/sonar-scanner-cli:latest \
|
||||
sonar-scanner -Dsonar.projectKey=0pt -Dsonar.host.url="$SONAR_HOST_URL")
|
||||
trap 'docker rm -f "$cid" >/dev/null 2>&1 || true' EXIT
|
||||
docker cp . "$cid":/usr/src
|
||||
docker start -a "$cid"
|
||||
|
||||
@@ -15,6 +15,7 @@ on:
|
||||
jobs:
|
||||
check:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 10
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Masterplan lock check
|
||||
|
||||
@@ -50,41 +50,75 @@ env:
|
||||
# G17_LICENSE_ALLOW policy in .siax/hooks/G17.sh) and #59 (G20 retry
|
||||
# logic -- not used by 0pt yet, but the pin picks up main's current tip
|
||||
# rather than an older commit, matching what a fresh migration reviews).
|
||||
SIAX_TEMPLATES_REF: main
|
||||
SIAX_TEMPLATES_PINNED_SHA: 9a3a0aa93c28a4538ecdb98658158f19cb23a6f9
|
||||
#
|
||||
# Re-pin 2026-09-25 (medveten, granskad): 9a3a0aa -> 09504e3a. Deltaet ar
|
||||
# EN merge, siax-templates #138 "Upload complete release evidence and
|
||||
# validate artifact receipt". Enda berorda filer under ci/v2 ar
|
||||
# reference-hooks/{G17.sh, G17-ADR-W16-003.md, lib/emit-release-evidence.py,
|
||||
# lib/test_emit_release_evidence.py}. Andringen ar bakatkompatibel by
|
||||
# design: ST0RE-uppladdningen ar OPT-IN via G17_REQUIRE_ST0RE (default 0),
|
||||
# sa en AUD0-only-konsument som 0pt kor exakt samma vag som forut.
|
||||
#
|
||||
# Deliberately NO branch name here: the pinned commit is checked out
|
||||
# DIRECTLY (see the step below). The previous shape -- clone `main`, then
|
||||
# require its HEAD to equal this pin -- turned every unrelated merge in
|
||||
# siax-templates into a red release chain here (CD-03 / CI-RED-02) while
|
||||
# nothing was actually unsafe. Checking the pin out directly keeps the pin
|
||||
# a real supply-chain control (the gate runs exactly this reviewed commit,
|
||||
# or it fails) without coupling it to someone else's branch tip.
|
||||
SIAX_TEMPLATES_PINNED_SHA: 09504e3a593ee8ce0eaba606df31c349a3d52277
|
||||
SIAX_TEMPLATES_DIR: /tmp/siax-templates-g17-ref
|
||||
|
||||
jobs:
|
||||
release:
|
||||
name: Build, push, G17 (SBOM/scan/attest/verify/evidence)
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 45
|
||||
steps:
|
||||
- uses: https://git.siax.io/actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4, mirrored + SHA-pinned (SUP-06, same pin n0d/cl0ud/st0re/h0st already use)
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
# --- fetch the shared G17 reference implementation -----------------
|
||||
# Same authenticated-clone-at-runtime mechanism this repo's OLD
|
||||
# Same authenticated-fetch-at-runtime mechanism this repo's OLD
|
||||
# release.yml already used SIAX_CI_TOKEN for (the evidence-commit
|
||||
# push this PR deletes) -- see .siax/hooks/G17.sh's header for why
|
||||
# push a prior PR deleted) -- see .siax/hooks/G17.sh's header for why
|
||||
# NOT a `uses:` reusable workflow / submodule / vendored copy. Fails
|
||||
# closed if the branch has moved past the exact commit this adapter
|
||||
# was reviewed against.
|
||||
- name: Fetch siax-templates G17 reference (pinned)
|
||||
# closed if the exact commit this adapter was reviewed against cannot
|
||||
# be checked out (removed or rewritten upstream); an unrelated merge
|
||||
# on siax-templates' own main is NOT a failure and never was a real
|
||||
# signal.
|
||||
- name: Fetch siax-templates G17 reference (pinned commit)
|
||||
env:
|
||||
SIAX_CI_TOKEN: ${{ secrets.SIAX_CI_TOKEN }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
rm -rf "$SIAX_TEMPLATES_DIR"
|
||||
git clone --quiet --branch "$SIAX_TEMPLATES_REF" \
|
||||
"https://oauth2:${SIAX_CI_TOKEN}@git.siax.io/${SIAX_TEMPLATES_REPO}.git" "$SIAX_TEMPLATES_DIR"
|
||||
mkdir -p "$SIAX_TEMPLATES_DIR"
|
||||
git -C "$SIAX_TEMPLATES_DIR" init --quiet
|
||||
git -C "$SIAX_TEMPLATES_DIR" remote add origin \
|
||||
"https://oauth2:${SIAX_CI_TOKEN}@git.siax.io/${SIAX_TEMPLATES_REPO}.git"
|
||||
# Ask for the pinned commit by SHA -- one shallow commit, no branch
|
||||
# involved. A server with uploadpack.allowReachableSHA1InWant off
|
||||
# refuses that, so fall back to fetching the branches and taking the
|
||||
# same commit out of them. Either way the work tree ends up at
|
||||
# exactly $SIAX_TEMPLATES_PINNED_SHA, never at a branch tip.
|
||||
if ! git -C "$SIAX_TEMPLATES_DIR" fetch --quiet --depth 1 origin "$SIAX_TEMPLATES_PINNED_SHA"; then
|
||||
echo "note: server refused a by-SHA fetch; falling back to fetching all branches"
|
||||
git -C "$SIAX_TEMPLATES_DIR" fetch --quiet origin
|
||||
fi
|
||||
if ! git -C "$SIAX_TEMPLATES_DIR" -c advice.detachedHead=false \
|
||||
checkout --quiet --detach "$SIAX_TEMPLATES_PINNED_SHA"; then
|
||||
echo "::error::siax-templates commit $SIAX_TEMPLATES_PINNED_SHA not found in $SIAX_TEMPLATES_REPO -- the reviewed commit was removed or rewritten upstream. Re-review siax-templates and update SIAX_TEMPLATES_PINNED_SHA deliberately."
|
||||
exit 1
|
||||
fi
|
||||
actual_sha="$(git -C "$SIAX_TEMPLATES_DIR" rev-parse HEAD)"
|
||||
if [ "$actual_sha" != "$SIAX_TEMPLATES_PINNED_SHA" ]; then
|
||||
echo "::error::siax-templates $SIAX_TEMPLATES_REF moved (now $actual_sha, pinned $SIAX_TEMPLATES_PINNED_SHA) -- refusing to run an unreviewed version of the gate. Re-review and update SIAX_TEMPLATES_PINNED_SHA deliberately."
|
||||
echo "::error::checked-out siax-templates HEAD is $actual_sha, expected the pinned $SIAX_TEMPLATES_PINNED_SHA -- refusing to run an unreviewed version of the gate."
|
||||
exit 1
|
||||
fi
|
||||
chmod +x "$SIAX_TEMPLATES_DIR/ci/v2/reference-hooks/"*.sh "$SIAX_TEMPLATES_DIR/ci/v2/reference-hooks/lib/"*.sh
|
||||
echo "siax-templates G17 reference @ $actual_sha (pinned, verified)"
|
||||
echo "siax-templates G17 reference @ $actual_sha (pinned commit, checked out directly)"
|
||||
|
||||
- name: Resolve SHA + tag (never :latest)
|
||||
id: sha
|
||||
@@ -142,3 +176,4 @@ jobs:
|
||||
# submission (bar item 7, step above) is already the durable evidence
|
||||
# record; a convenience local copy is not worth re-triggering that
|
||||
# known breakage.
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ on:
|
||||
jobs:
|
||||
security:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 15
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Install Trivy
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
name: VehIQ OLTP extractor (0pt ingestion)
|
||||
|
||||
# P070 — scheduled poll of app.dealer_stock_entry/app.deal (VehIQ, server10)
|
||||
# into opt_dataset_projection (ClickHouse), via the watermark extractor in
|
||||
# src/ingestion/vehiq-oltp-ingest-runner.ts. See docs/P070_VEHIQ_OLTP_
|
||||
# EXTRACTOR.md for the full design and "what activating this requires".
|
||||
#
|
||||
# SAFE BY DEFAULT: VEHIQ_INGEST_MODE defaults to "preview" (read-only, never
|
||||
# commits a checkpoint, never writes) below. With no VEHIQ_SOURCE_DATABASE_URL
|
||||
# secret configured on this repo, the script logs "not_configured" and exits
|
||||
# 0 — this workflow can be left enabled indefinitely with zero effect on
|
||||
# anything until BOTH the secret is added AND VEHIQ_INGEST_MODE is changed
|
||||
# to "run". Neither is done by this workflow file itself.
|
||||
on:
|
||||
workflow_dispatch:
|
||||
schedule:
|
||||
- cron: "*/15 * * * *"
|
||||
|
||||
jobs:
|
||||
extract:
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
VEHIQ_SOURCE_DATABASE_URL: ${{ secrets.VEHIQ_SOURCE_DATABASE_URL }}
|
||||
DATABASE_URL: ${{ secrets.DATABASE_URL }}
|
||||
OPT_ANALYTICS_CLICKHOUSE_BASE_URL: ${{ secrets.OPT_ANALYTICS_CLICKHOUSE_BASE_URL }}
|
||||
OPT_ANALYTICS_CLICKHOUSE_BASIC_AUTH: ${{ secrets.OPT_ANALYTICS_CLICKHOUSE_BASIC_AUTH }}
|
||||
OPT_ANALYTICS_CLICKHOUSE_DATABASE: opt_analytics
|
||||
# Activation gate — see docs/P070_VEHIQ_OLTP_EXTRACTOR.md. Flip to
|
||||
# "run" only once steps 1-4 in that doc's "What activating this
|
||||
# requires" section are actually done.
|
||||
VEHIQ_INGEST_MODE: preview
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: "22"
|
||||
- name: Enable corepack + pin pnpm@9
|
||||
run: |
|
||||
corepack enable
|
||||
corepack prepare pnpm@9 --activate
|
||||
- name: Install (frozen lockfile)
|
||||
run: pnpm install --frozen-lockfile
|
||||
- name: Run the extractor (preview mode unless activated)
|
||||
run: pnpm run vehiq:oltp-ingest
|
||||
+4
-3
@@ -70,9 +70,10 @@
|
||||
# picks up main's current tip rather than an older commit, matching what a
|
||||
# fresh migration reviews). Pinned at
|
||||
# 384e35060b91ea78ec045e14af114abf5d5eb23b -- the exact ref lives in
|
||||
# `.gitea/workflows/release.yml` (SIAX_TEMPLATES_REF=main +
|
||||
# SIAX_TEMPLATES_PINNED_SHA, which refuses to run if main has moved past
|
||||
# the pin).
|
||||
# `.gitea/workflows/release.yml` (SIAX_TEMPLATES_PINNED_SHA, which is
|
||||
# fetched and checked out DIRECTLY by SHA -- no branch is consulted, so an
|
||||
# unrelated merge on siax-templates' main does not fail this gate; a pinned
|
||||
# commit that no longer exists upstream does).
|
||||
set -euo pipefail
|
||||
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
|
||||
|
||||
|
||||
@@ -45,10 +45,15 @@ Pinned to `sax3l/siax-templates` main at
|
||||
(`feat/g17-signed-deploy-gate-adr-w16-003`) merged, plus follow-up fixes
|
||||
#56/#57 (SPDX compound-license-expression evaluation, exercised live by
|
||||
this adapter's own `G17_LICENSE_ALLOW` policy) and #59 (G20 retry logic;
|
||||
not used by 0pt yet, see below). `.gitea/workflows/release.yml`'s own
|
||||
`SIAX_TEMPLATES_PINNED_SHA` step refuses to run if `siax-templates` main
|
||||
has moved past this exact commit -- bumping the pin is a reviewed,
|
||||
source-controlled diff to that one line, never a silent re-resolution.
|
||||
not used by 0pt yet, see below). `.gitea/workflows/release.yml` fetches
|
||||
and checks out `SIAX_TEMPLATES_PINNED_SHA` **directly by SHA** and refuses
|
||||
to run if that exact commit cannot be checked out (removed or rewritten
|
||||
upstream) -- bumping the pin is a reviewed, source-controlled diff to that
|
||||
one line, never a silent re-resolution. It deliberately does **not**
|
||||
compare the pin against `main`: that branch tip moves whenever anything
|
||||
unrelated merges in `siax-templates`, and failing this chain on that said
|
||||
nothing about whether the reviewed gate was safe (CD-03 / CI-RED-02). The
|
||||
authoritative pin value is the one in `release.yml`.
|
||||
|
||||
## G20 (live-digest-equality) -- deliberately not wired yet
|
||||
|
||||
|
||||
@@ -0,0 +1,223 @@
|
||||
"use client";
|
||||
|
||||
// C8: "Importera ordrar" — the first real product host of the @siax/ui
|
||||
// ImportWizard + @siax/import-export runtime. The wizard renders the frozen
|
||||
// store snapshot; THIS host owns every pipeline step's execution and all
|
||||
// server I/O. The server (POST /api/orders/import) is the only authority:
|
||||
// it re-parses and re-validates the raw CSV, so the client-side parse below
|
||||
// exists purely to give the wizard rows to render.
|
||||
|
||||
import { ImportWizard } from "@siax/ui";
|
||||
import { createImportRunStore, type ImportRunStore, type ImportRunSnapshot } from "@siax/import-export";
|
||||
import { createImportJob } from "@siax/contracts/import-export";
|
||||
import { useCallback, useState } from "react";
|
||||
|
||||
const IMPORT_COLUMNS = ["flow", "service_type", "type", "volume", "frequency", "markets"] as const;
|
||||
|
||||
interface ServerReport {
|
||||
imported?: number;
|
||||
totalRows?: number;
|
||||
validRows?: number;
|
||||
errorCount?: number;
|
||||
errors?: { row: number; field: string; code: string; message?: string }[];
|
||||
order_ids?: string[];
|
||||
}
|
||||
|
||||
/** Client-side RFC 4180 parse — presentation only; the server re-parses the raw CSV. */
|
||||
function clientParse(text: string): Record<string, string>[] {
|
||||
const rows: string[][] = [];
|
||||
let field = "";
|
||||
let row: string[] = [];
|
||||
let inQuotes = false;
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
const ch = text[i];
|
||||
if (inQuotes) {
|
||||
if (ch === '"') {
|
||||
if (text[i + 1] === '"') {
|
||||
field += '"';
|
||||
i++;
|
||||
} else inQuotes = false;
|
||||
} else field += ch;
|
||||
} else if (ch === '"') inQuotes = true;
|
||||
else if (ch === ",") {
|
||||
row.push(field);
|
||||
field = "";
|
||||
} else if (ch === "\n") {
|
||||
row.push(field);
|
||||
field = "";
|
||||
rows.push(row);
|
||||
row = [];
|
||||
} else if (ch !== "\r") field += ch;
|
||||
}
|
||||
if (field.length > 0 || row.length > 0) {
|
||||
row.push(field);
|
||||
rows.push(row);
|
||||
}
|
||||
const nonEmpty = rows.filter((r) => r.length > 1 || (r[0] ?? "").trim() !== "");
|
||||
if (nonEmpty.length === 0) return [];
|
||||
const header = nonEmpty[0].map((h) => h.trim().toLowerCase());
|
||||
return nonEmpty.slice(1).map((cells) => {
|
||||
const obj: Record<string, string> = {};
|
||||
header.forEach((h, idx) => {
|
||||
if (h) obj[h] = (cells[idx] ?? "").trim();
|
||||
});
|
||||
return obj;
|
||||
});
|
||||
}
|
||||
|
||||
async function postImport(mode: "validate" | "dry_run" | "execute", csv: string): Promise<{ ok: boolean; status: number; data: ServerReport & { error?: { message?: string } } }> {
|
||||
const res = await fetch("/api/orders/import", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ mode, csv }),
|
||||
});
|
||||
let data: ServerReport & { error?: { message?: string } } = {};
|
||||
try {
|
||||
data = await res.json();
|
||||
} catch {
|
||||
data = { error: { message: `HTTP ${res.status}` } };
|
||||
}
|
||||
return { ok: res.ok, status: res.status, data };
|
||||
}
|
||||
|
||||
export default function AdminImportPage() {
|
||||
const [rawCsv, setRawCsv] = useState<string | null>(null);
|
||||
const [store, setStore] = useState<ImportRunStore | null>(null);
|
||||
const [serverReport, setServerReport] = useState<ServerReport | null>(null);
|
||||
const [serverError, setServerError] = useState<string | null>(null);
|
||||
const [recentOrders, setRecentOrders] = useState<{ order_id: string; flow: string; status: string }[] | null>(null);
|
||||
|
||||
const onFile = useCallback(async (file: File) => {
|
||||
const text = await file.text();
|
||||
setRawCsv(text);
|
||||
setServerReport(null);
|
||||
setServerError(null);
|
||||
setRecentOrders(null);
|
||||
const rows = clientParse(text);
|
||||
const s = createImportRunStore({
|
||||
job: createImportJob({
|
||||
id: globalThis.crypto?.randomUUID?.() ?? `imp-${Date.now()}`,
|
||||
tenantId: "current",
|
||||
entity: "orders",
|
||||
format: "csv",
|
||||
source: { system: "0pt-admin", channel: "web" },
|
||||
createdAt: new Date().toISOString(),
|
||||
}),
|
||||
rows,
|
||||
});
|
||||
s.detect();
|
||||
setStore(s);
|
||||
}, []);
|
||||
|
||||
const onValidate = useCallback(async () => {
|
||||
if (!store || !rawCsv) return;
|
||||
setServerError(null);
|
||||
const { ok, status, data } = await postImport("validate", rawCsv);
|
||||
setServerReport(data);
|
||||
store.validate(); // local snapshot for the wizard report (same frozen kernel)
|
||||
if (!ok) setServerError(`Servervalidering avvisade filen (HTTP ${status}): ${data.error?.message ?? "okänt fel"}`);
|
||||
}, [store, rawCsv]);
|
||||
|
||||
const onDryRun = useCallback(async () => {
|
||||
if (!store || !rawCsv) return;
|
||||
setServerError(null);
|
||||
const { ok, status, data } = await postImport("dry_run", rawCsv);
|
||||
setServerReport(data);
|
||||
store.dryRun();
|
||||
if (!ok) setServerError(`Servern nekade generalrepet (HTTP ${status}): ${data.error?.message ?? "okänt fel"}`);
|
||||
}, [store, rawCsv]);
|
||||
|
||||
const onExecute = useCallback(async () => {
|
||||
if (!store || !rawCsv) return;
|
||||
setServerError(null);
|
||||
const { ok, status, data } = await postImport("execute", rawCsv);
|
||||
setServerReport(data);
|
||||
if (!ok) {
|
||||
// Server is the authority — a failed execute is terminal for this run.
|
||||
store.fail(`Servern avvisade exekveringen (HTTP ${status}): ${data.error?.message ?? "okänt fel"}`);
|
||||
return;
|
||||
}
|
||||
// Host executor proxies the server result into the store's hard gates.
|
||||
const snapshot: ImportRunSnapshot = await store.execute(async () => ({ imported: data.imported ?? 0 }));
|
||||
// Reload-persistence proof: re-read the REAL order list after import.
|
||||
try {
|
||||
const res = await fetch("/api/orders", { cache: "no-store" });
|
||||
if (res.ok) {
|
||||
const orders = (await res.json()) as { order_id: string; flow: string; status: string }[];
|
||||
const importedIds = new Set(data.order_ids ?? []);
|
||||
setRecentOrders(orders.filter((o) => importedIds.has(o.order_id)));
|
||||
}
|
||||
} catch {
|
||||
/* panel stays null — server report above still shows the truth */
|
||||
}
|
||||
void snapshot;
|
||||
}, [store, rawCsv]);
|
||||
|
||||
return (
|
||||
<main style={{ maxWidth: 720, margin: "0 auto", padding: "2rem 1rem" }}>
|
||||
<h1 style={{ fontSize: "1.5rem", marginBottom: "0.5rem" }}>Importera ordrar</h1>
|
||||
<p style={{ marginBottom: "1rem", opacity: 0.8 }}>
|
||||
CSV med kolumnerna: {IMPORT_COLUMNS.join(", ")}. Endast <code>flow</code> är obligatorisk
|
||||
(catalog | custom | subscription). Parsering och validering sker på servern —
|
||||
filen laddas inte upp förrän ett steg körs.
|
||||
</p>
|
||||
<input
|
||||
type="file"
|
||||
accept=".csv,text/csv"
|
||||
aria-label="Välj CSV-fil"
|
||||
onChange={(e) => {
|
||||
const f = e.target.files?.[0];
|
||||
if (f) void onFile(f);
|
||||
}}
|
||||
/>
|
||||
{store && (
|
||||
<div style={{ marginTop: "1.5rem" }}>
|
||||
<ImportWizard
|
||||
store={store}
|
||||
columns={IMPORT_COLUMNS.map((c) => ({ source: c, target: c }))}
|
||||
onMap={() => {
|
||||
store.map({ columns: IMPORT_COLUMNS.map((c) => ({ source: c, target: c, required: c === "flow" })) });
|
||||
}}
|
||||
onValidate={onValidate}
|
||||
onDryRun={onDryRun}
|
||||
onExecute={onExecute}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
{serverError && (
|
||||
<p role="alert" style={{ marginTop: "1rem", color: "crimson" }}>
|
||||
{serverError}
|
||||
</p>
|
||||
)}
|
||||
{serverReport && (serverReport.errorCount ?? 0) > 0 && (
|
||||
<section style={{ marginTop: "1.5rem" }} aria-label="Felrapport från servern">
|
||||
<h2 style={{ fontSize: "1.1rem" }}>Felrader (serverns rapport)</h2>
|
||||
<ul>
|
||||
{(serverReport.errors ?? []).map((e, i) => (
|
||||
<li key={i}>
|
||||
Rad {e.row} · {e.field} · {e.code}
|
||||
{e.message ? ` — ${e.message}` : ""}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</section>
|
||||
)}
|
||||
{recentOrders && (
|
||||
<section style={{ marginTop: "1.5rem" }} aria-label="Importerade ordrar efter reload">
|
||||
<h2 style={{ fontSize: "1.1rem" }}>Importerade ordrar (lästa från /api/orders efter import)</h2>
|
||||
{recentOrders.length === 0 ? (
|
||||
<p>Inga importerade ordrar hittades vid reload.</p>
|
||||
) : (
|
||||
<ul>
|
||||
{recentOrders.map((o) => (
|
||||
<li key={o.order_id}>
|
||||
<code>{o.order_id}</code> — {o.flow} — {o.status}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</section>
|
||||
)}
|
||||
</main>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
"use client";
|
||||
|
||||
// Admin-yta: anteckningar på order — @siax/collaboration-kernelns första
|
||||
// produktkonsument (spår C). Allt nätverk går via lib/order-notes-client.ts
|
||||
// (kernel-transport över host-API): offline-läge = medvetet no-op med varning
|
||||
// (utkastet bevaras, ingen tyst förlust), stale skrivning = 409 -> banner +
|
||||
// reload av serverns aktuella version.
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
|
||||
import { PortalShell } from "@/components/portal-shell";
|
||||
import { EmptyState, ErrorState, LoadingSkeleton } from "@/components/states";
|
||||
import {
|
||||
ConflictError,
|
||||
OfflineError,
|
||||
OrderNotesClient,
|
||||
type OrderNoteDto,
|
||||
} from "@/lib/order-notes-client";
|
||||
|
||||
interface Order {
|
||||
order_id: string;
|
||||
flow: string;
|
||||
status: string;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export default function OrderNotesPage() {
|
||||
const [orders, setOrders] = useState<Order[] | null>(null);
|
||||
const [orderId, setOrderId] = useState<string>("");
|
||||
const [notes, setNotes] = useState<OrderNoteDto[] | null>(null);
|
||||
const [loadError, setLoadError] = useState(false);
|
||||
const [offlineMode, setOfflineMode] = useState(false);
|
||||
const [draft, setDraft] = useState("");
|
||||
const [editing, setEditing] = useState<{ note: OrderNoteDto; body: string } | null>(null);
|
||||
|
||||
const client = useMemo(() => new OrderNotesClient("admin-simon"), []);
|
||||
|
||||
useEffect(() => {
|
||||
client.transport.setOffline(offlineMode);
|
||||
}, [client, offlineMode]);
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
try {
|
||||
const res = await fetch("/api/orders", { cache: "no-store" });
|
||||
if (!res.ok) throw new Error("orders fetch failed");
|
||||
const body = await res.json();
|
||||
setOrders(Array.isArray(body) ? body : []);
|
||||
} catch {
|
||||
setLoadError(true);
|
||||
}
|
||||
})();
|
||||
}, []);
|
||||
|
||||
const loadNotes = useCallback(
|
||||
async (id: string) => {
|
||||
setNotes(null);
|
||||
setLoadError(false);
|
||||
try {
|
||||
const { notes: loaded } = await client.load(id);
|
||||
setNotes(loaded);
|
||||
} catch {
|
||||
setLoadError(true);
|
||||
}
|
||||
},
|
||||
[client],
|
||||
);
|
||||
|
||||
const addNote = useCallback(async () => {
|
||||
if (!orderId || !draft.trim()) return;
|
||||
if (offlineMode) {
|
||||
// Offline = klient-varning + no-op. Utkastet ligger kvar i rutan —
|
||||
// INGET skickas, inget försvinner tyst.
|
||||
toast.warning("Offline-läge: anteckningen SKICKAS EJ. Utkastet är bevarat i rutan.");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const note = await client.add(orderId, draft);
|
||||
setDraft("");
|
||||
setNotes((prev) => [...(prev ?? []), note]);
|
||||
toast.success("Anteckning sparad");
|
||||
} catch (err) {
|
||||
if (err instanceof OfflineError) {
|
||||
toast.warning("Offline: inget skickades — utkastet är bevarat.");
|
||||
return;
|
||||
}
|
||||
setLoadError(true);
|
||||
}
|
||||
}, [client, draft, offlineMode, orderId]);
|
||||
|
||||
const saveEdit = useCallback(async () => {
|
||||
if (!editing) return;
|
||||
if (offlineMode) {
|
||||
toast.warning("Offline-läge: ändringen SKICKAS EJ. Texten är bevarad i redigeringsrutan.");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const updated = await client.update(editing.note, editing.body, editing.note.version);
|
||||
setNotes((prev) => (prev ?? []).map((n) => (n.note_id === updated.note_id ? updated : n)));
|
||||
setEditing(null);
|
||||
toast.success("Anteckning uppdaterad");
|
||||
} catch (err) {
|
||||
if (err instanceof ConflictError) {
|
||||
// Någon annan skrev samtidigt (stale version -> 409). Ladda om
|
||||
// serverns aktuella version — förlora ALDRIG den lokala texten tyst.
|
||||
toast.error(`Konflikt: anteckningen ändrades av någon annan (serverversion ${err.currentVersion}). Laddar om — din text finns kvar nedan.`);
|
||||
setDraft((prev) => (prev ? prev : editing.body));
|
||||
await loadNotes(orderId);
|
||||
setEditing(null);
|
||||
return;
|
||||
}
|
||||
if (err instanceof OfflineError) {
|
||||
toast.warning("Offline: inget skickades — ändringen är bevarad.");
|
||||
return;
|
||||
}
|
||||
setLoadError(true);
|
||||
}
|
||||
}, [client, editing, loadNotes, offlineMode, orderId]);
|
||||
|
||||
return (
|
||||
<PortalShell variant="admin" title="Orderanteckningar">
|
||||
<label className="flex items-center gap-2 text-sm">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={offlineMode}
|
||||
onChange={(e) => setOfflineMode(e.target.checked)}
|
||||
/>
|
||||
Offline-läge (blockera utskick — varning + no-op, utkast bevaras)
|
||||
</label>
|
||||
{offlineMode && (
|
||||
<div role="alert" className="rounded border border-amber-400 bg-amber-50 p-2 text-sm">
|
||||
Offline-läge aktivt: inga anteckningar skickas till servern. Utkast bevaras lokalt —
|
||||
ingen tyst förlust.
|
||||
</div>
|
||||
)}
|
||||
|
||||
{loadError ? (
|
||||
<ErrorState onRetry={() => orderId && loadNotes(orderId)} />
|
||||
) : orders === null ? (
|
||||
<LoadingSkeleton />
|
||||
) : orders.length === 0 ? (
|
||||
<EmptyState title="Inga ordrar" line="Inga ordrar att annotera ännu." />
|
||||
) : (
|
||||
<div className="space-y-4">
|
||||
<label className="flex items-center gap-2 text-sm">
|
||||
Order:{" "}
|
||||
<select
|
||||
aria-label="Välj order"
|
||||
value={orderId}
|
||||
onChange={(e) => {
|
||||
setOrderId(e.target.value);
|
||||
void loadNotes(e.target.value);
|
||||
}}
|
||||
className="rounded border px-2 py-1"
|
||||
>
|
||||
<option value="">— välj —</option>
|
||||
{orders.map((o) => (
|
||||
<option key={o.order_id} value={o.order_id}>
|
||||
{o.order_id} ({o.flow}, {o.status})
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
|
||||
{orderId === "" ? (
|
||||
<EmptyState title="Välj order" line="Välj en order för att se och skriva anteckningar." />
|
||||
) : notes === null ? (
|
||||
<LoadingSkeleton />
|
||||
) : notes.length === 0 ? (
|
||||
<EmptyState title="Tomt" line="Inga anteckningar på den här ordern ännu." />
|
||||
) : (
|
||||
<ul className="space-y-2">
|
||||
{notes.map((n) => (
|
||||
<li key={n.note_id} className="rounded border p-3 text-sm">
|
||||
<div className="mb-1 flex justify-between text-xs opacity-70">
|
||||
<span>
|
||||
{n.author_id} · v{n.version} · klocka {n.clock}
|
||||
</span>
|
||||
<span>{new Date(n.updated_at).toLocaleString("sv-SE")}</span>
|
||||
</div>
|
||||
{editing?.note.note_id === n.note_id ? (
|
||||
<div className="space-y-2">
|
||||
<textarea
|
||||
aria-label="Redigera anteckning"
|
||||
value={editing.body}
|
||||
onChange={(e) => setEditing({ ...editing, body: e.target.value })}
|
||||
className="w-full rounded border p-2"
|
||||
rows={3}
|
||||
/>
|
||||
<div className="flex gap-2">
|
||||
<button className="rounded bg-black px-3 py-1 text-white" onClick={() => void saveEdit()}>
|
||||
Spara (v{editing.note.version} → v{editing.note.version + 1})
|
||||
</button>
|
||||
<button className="rounded border px-3 py-1" onClick={() => setEditing(null)}>
|
||||
Avbryt
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
<p className="whitespace-pre-wrap">{n.body}</p>
|
||||
<button
|
||||
className="mt-2 text-xs underline"
|
||||
onClick={() => setEditing({ note: n, body: n.body })}
|
||||
>
|
||||
Redigera
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
|
||||
{orderId !== "" && (
|
||||
<div className="space-y-2">
|
||||
<textarea
|
||||
aria-label="Ny anteckning"
|
||||
placeholder="Skriv en anteckning på ordern…"
|
||||
value={draft}
|
||||
onChange={(e) => setDraft(e.target.value)}
|
||||
className="w-full rounded border p-2"
|
||||
rows={3}
|
||||
/>
|
||||
<button
|
||||
className="rounded bg-black px-4 py-2 text-sm text-white disabled:opacity-40"
|
||||
disabled={!draft.trim() || notes === null}
|
||||
onClick={() => void addNote()}
|
||||
>
|
||||
Lägg till anteckning
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</PortalShell>
|
||||
);
|
||||
}
|
||||
+34
-14
@@ -1,5 +1,8 @@
|
||||
"use client";
|
||||
|
||||
import { ActionGate, InboxList } from "@siax/ui";
|
||||
import { nextOrderStatus } from "@/lib/order-flow";
|
||||
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
import { useT } from "@/components/locale-provider";
|
||||
@@ -25,13 +28,6 @@ interface Metrics {
|
||||
pipeline_ore: number;
|
||||
}
|
||||
|
||||
const NEXT_STATUS: Record<string, string> = {
|
||||
draft: "quoted",
|
||||
quoted: "in_production",
|
||||
in_production: "review",
|
||||
review: "accepted",
|
||||
accepted: "delivered",
|
||||
};
|
||||
|
||||
export default function AdminPage() {
|
||||
const t = useT();
|
||||
@@ -58,7 +54,7 @@ export default function AdminPage() {
|
||||
useEffect(() => { void load(); }, [load]);
|
||||
|
||||
async function advance(id: string, status: string) {
|
||||
const next = NEXT_STATUS[status];
|
||||
const next = nextOrderStatus(status);
|
||||
if (!next) return;
|
||||
try {
|
||||
const r = await fetch(`/api/orders/${id}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ status: next }) });
|
||||
@@ -96,8 +92,34 @@ export default function AdminPage() {
|
||||
))}
|
||||
</div>
|
||||
|
||||
{orders.some((o) => o.inbox_item) ? (
|
||||
<section className="mt-10">
|
||||
<h2 className="font-mono text-[11px] uppercase tracking-[0.14em] text-muted-foreground">{t({ en: "Inbox", sv: "Inkorg" })}</h2>
|
||||
<div className="mt-4">
|
||||
<InboxList
|
||||
items={orders.map((o) => o.inbox_item).filter(Boolean)}
|
||||
labels={{
|
||||
resolve: t({ en: "Advance", sv: "Flytta fram" }),
|
||||
emptyTitle: t({ en: "Inbox is empty", sv: "Inkorgen är tom" }),
|
||||
}}
|
||||
onResolve={(item) => {
|
||||
const order = orders.find((o) => `order:${o.order_id}` === item.id);
|
||||
if (order?.advance_action?.state === "available" && nextOrderStatus(order.status)) {
|
||||
advance(order.order_id, order.status);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</section>
|
||||
) : null}
|
||||
<section className="mt-10">
|
||||
<h2 className="font-mono text-[11px] uppercase tracking-[0.14em] text-muted-foreground">{t({ en: "Order queue", sv: "Orderkö" })}</h2>
|
||||
<div className="flex items-center justify-between">
|
||||
<h2 className="font-mono text-[11px] uppercase tracking-[0.14em] text-muted-foreground">{t({ en: "Order queue", sv: "Orderkö" })}</h2>
|
||||
{/* DoD §65: real anchor, not a JS-handler button — the download is a plain GET of the CSV endpoint. */}
|
||||
<a href="/api/orders/export" download className="border border-border px-3 py-1.5 font-mono text-[10px] uppercase tracking-[0.08em] text-muted-foreground transition-colors hover:bg-foreground hover:text-background">
|
||||
{t({ en: "Export CSV", sv: "Exportera CSV" })}
|
||||
</a>
|
||||
</div>
|
||||
{orders.length === 0 ? (
|
||||
<div className="mt-6"><EmptyState title={t({ en: "Empty queue.", sv: "Tom kö." })} line={t({ en: "No orders yet.", sv: "Inga ordrar än." })} /></div>
|
||||
) : (
|
||||
@@ -112,16 +134,14 @@ export default function AdminPage() {
|
||||
<StatusPill status={o.status} />
|
||||
<span className="text-right font-mono text-[12px] tabular-nums">{formatSEK(o.total_ore / 100)}</span>
|
||||
<span className="text-right">
|
||||
{NEXT_STATUS[o.status] ? (
|
||||
<ActionGate action={o.advance_action} className="inline-block text-right">
|
||||
<button
|
||||
onClick={() => advance(o.order_id, o.status)}
|
||||
className="border border-foreground px-3 py-1.5 font-mono text-[10px] uppercase tracking-[0.08em] transition-colors hover:bg-foreground hover:text-background"
|
||||
>
|
||||
→ {NEXT_STATUS[o.status]}
|
||||
→ {nextOrderStatus(o.status) ?? t({ en: "done", sv: "klar" })}
|
||||
</button>
|
||||
) : (
|
||||
<span className="font-mono text-[10px] uppercase tracking-[0.08em] text-muted-foreground">{t({ en: "done", sv: "klar" })}</span>
|
||||
)}
|
||||
</ActionGate>
|
||||
</span>
|
||||
</div>
|
||||
))}
|
||||
|
||||
@@ -88,7 +88,7 @@ function ScreenTool() {
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
<p className="mt-3 text-[10.5px] text-muted-foreground/70">{t({ en: "Requires verified OpenSanctions terms/key for full matches.", sv: "Kräver verifierade OpenSanctions-villkor/nyckel för fullständiga träffar." })}</p>
|
||||
<p className="mt-3 text-[10.5px] text-muted-foreground">{t({ en: "Requires verified OpenSanctions terms/key for full matches.", sv: "Kräver verifierade OpenSanctions-villkor/nyckel för fullständiga träffar." })}</p>
|
||||
</div>
|
||||
</section>
|
||||
);
|
||||
|
||||
@@ -0,0 +1,183 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
import { requireRouteAuth } from "@/src/auth/require-route-auth";
|
||||
import { createAud0Client } from "@/src/clients/aud0-client";
|
||||
import {
|
||||
addNote,
|
||||
findNote,
|
||||
listNotes,
|
||||
NoteNotFoundError,
|
||||
NoteVersionConflictError,
|
||||
updateNote,
|
||||
} from "@/lib/order-notes-store";
|
||||
import { getOrder } from "@/lib/orders-store";
|
||||
import { isSiaxError, siaxErrorResponseForLegacy } from "../../siax-error-mapping";
|
||||
|
||||
// GET/POST/PATCH /api/admin/order-notes — persisted collaboration state på
|
||||
// admin-orders (kernel: @siax/collaboration). Ligger utanför middleware.ts:s
|
||||
// matcher (/api/0pt/*), så auth läggs på direkt via requireRouteAuth, precis
|
||||
// som /api/admin/metrics — fail-closed Zitadel-verifiering, 401 utan token.
|
||||
//
|
||||
// Tenant-guard: tenant_id hämtas SERVER-SIDE från ordern (aldrig från
|
||||
// klienten) och jämförs mot den verifierade kontextens tenant — mismatch är
|
||||
// 403. Conflict semantics: PATCH med stale `version` -> 409 + aktuell version
|
||||
// (optimistisk samtidighet; last-write-wins först EFTER en medveten reload).
|
||||
|
||||
const AUDIT_CAPABILITY = "admin.order_notes";
|
||||
|
||||
async function audit(
|
||||
tenantId: string | null,
|
||||
actorId: string,
|
||||
resourceId: string,
|
||||
eventType: string,
|
||||
payload: unknown,
|
||||
): Promise<void> {
|
||||
// Best effort: AUD0 är evidence, inte en skriv-buffert — ett audit-fel får
|
||||
// aldrig rulla tillbaka en lyckad note-skrivning.
|
||||
try {
|
||||
await createAud0Client().ingestEvent({
|
||||
event_id: `aud_${crypto.randomUUID()}`,
|
||||
event_type: eventType,
|
||||
tenant_id: tenantId ?? "anonymous",
|
||||
project_id: "0pt",
|
||||
actor_id: actorId,
|
||||
app_id: "0pt",
|
||||
capability_id: AUDIT_CAPABILITY,
|
||||
resource_type: "order_note",
|
||||
resource_id: resourceId,
|
||||
risk_level: "L1",
|
||||
payload,
|
||||
created_at: new Date().toISOString(),
|
||||
});
|
||||
} catch {
|
||||
// swallow — see comment above
|
||||
}
|
||||
}
|
||||
|
||||
function tenantGuard(ctxTenant: string, orderTenant: string | null | undefined): NextResponse | null {
|
||||
if (!orderTenant) return null; // legacy anonymous draft — admin-ytan får annotera
|
||||
if (ctxTenant !== orderTenant) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: "OPT_ACCESS_DENIED",
|
||||
message: "order belongs to a different tenant than the authenticated token",
|
||||
details: { order_tenant: orderTenant },
|
||||
},
|
||||
{ status: 403 },
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const auth = await requireRouteAuth(req);
|
||||
if (auth.response) return auth.response;
|
||||
const orderId = req.nextUrl.searchParams.get("order_id");
|
||||
if (!orderId) {
|
||||
return NextResponse.json({ error: "order_id query parameter is required" }, { status: 400 });
|
||||
}
|
||||
const order = await getOrder(orderId);
|
||||
if (!order) {
|
||||
return NextResponse.json({ error: "order_not_found", order_id: orderId }, { status: 404 });
|
||||
}
|
||||
const denied = tenantGuard(auth.ctx.tenantId, order.tenant_id);
|
||||
if (denied) return denied;
|
||||
const notes = await listNotes(orderId);
|
||||
return NextResponse.json({ order_id: orderId, notes });
|
||||
}
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const auth = await requireRouteAuth(req);
|
||||
if (auth.response) return auth.response;
|
||||
let body: { order_id?: string; body?: string };
|
||||
try {
|
||||
body = await req.json();
|
||||
} catch {
|
||||
return NextResponse.json({ error: "invalid JSON body" }, { status: 400 });
|
||||
}
|
||||
if (!body.order_id || !body.body || !String(body.body).trim()) {
|
||||
return NextResponse.json(
|
||||
{ error: "order_id and non-empty body are required" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
const order = await getOrder(body.order_id);
|
||||
if (!order) {
|
||||
return NextResponse.json({ error: "order_not_found", order_id: body.order_id }, { status: 404 });
|
||||
}
|
||||
const denied = tenantGuard(auth.ctx.tenantId, order.tenant_id);
|
||||
if (denied) return denied;
|
||||
try {
|
||||
const note = await addNote({
|
||||
order_id: body.order_id,
|
||||
tenant_id: order.tenant_id ?? null,
|
||||
author_id: auth.ctx.actorId,
|
||||
body: String(body.body),
|
||||
});
|
||||
await audit(note.tenant_id, auth.ctx.actorId, note.note_id, "order_note.created", {
|
||||
order_id: note.order_id,
|
||||
clock: note.clock,
|
||||
});
|
||||
return NextResponse.json(note, { status: 201 });
|
||||
} catch (err: unknown) {
|
||||
if (isSiaxError(err)) return siaxErrorResponseForLegacy(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
export async function PATCH(req: NextRequest) {
|
||||
const auth = await requireRouteAuth(req);
|
||||
if (auth.response) return auth.response;
|
||||
let body: { note_id?: string; body?: string; version?: number };
|
||||
try {
|
||||
body = await req.json();
|
||||
} catch {
|
||||
return NextResponse.json({ error: "invalid JSON body" }, { status: 400 });
|
||||
}
|
||||
if (!body.note_id || !body.body || !Number.isInteger(body.version)) {
|
||||
return NextResponse.json(
|
||||
{ error: "note_id, body and integer version are required" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
// Server-authoritative tenant check: slå upp noten (och dess order) INNAN
|
||||
// någon skrivning — klienten får aldrig säga vilken tenant en note tillhör.
|
||||
const existing = await findNote(body.note_id);
|
||||
if (!existing) {
|
||||
return NextResponse.json({ error: "note_not_found" }, { status: 404 });
|
||||
}
|
||||
const order = await getOrder(existing.order_id);
|
||||
const denied = tenantGuard(auth.ctx.tenantId, order?.tenant_id ?? existing.tenant_id);
|
||||
if (denied) return denied;
|
||||
try {
|
||||
const note = await updateNote({
|
||||
note_id: body.note_id,
|
||||
author_id: auth.ctx.actorId,
|
||||
body: String(body.body),
|
||||
expected_version: body.version as number,
|
||||
});
|
||||
await audit(note.tenant_id, auth.ctx.actorId, note.note_id, "order_note.updated", {
|
||||
order_id: note.order_id,
|
||||
version: note.version,
|
||||
clock: note.clock,
|
||||
});
|
||||
return NextResponse.json(note);
|
||||
} catch (err: unknown) {
|
||||
if (err instanceof NoteVersionConflictError) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: "version_conflict",
|
||||
message: err.message,
|
||||
expected_version: err.expectedVersion,
|
||||
current_version: err.currentVersion,
|
||||
},
|
||||
{ status: 409 },
|
||||
);
|
||||
}
|
||||
if (err instanceof NoteNotFoundError) {
|
||||
return NextResponse.json({ error: "note_not_found" }, { status: 404 });
|
||||
}
|
||||
if (isSiaxError(err)) return siaxErrorResponseForLegacy(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { listOwnedOrders } from "@/lib/orders-store";
|
||||
import { requireOrderOwner } from "@/src/auth/require-order-owner";
|
||||
import { isSiaxError, siaxErrorResponseForLegacy } from "../../siax-error-mapping";
|
||||
|
||||
/**
|
||||
* GET /api/orders/export (våg 3.4, DoD §56) — CSV-export of the authenticated
|
||||
* owner's orders. Same fail-closed auth as GET/POST /api/orders (this path is
|
||||
* outside middleware.ts's /api/0pt matcher, so the Zitadel verifier runs here).
|
||||
*
|
||||
* RFC 4180: CRLF line endings, fields containing comma, quote or newline are
|
||||
* quoted with doubled quotes, header row always present.
|
||||
*
|
||||
* Locale note: deposit_ore / total_ore are exported as RAW ÖRE — öre is the
|
||||
* canonical unit in the order contract; formatting (SEK, thousand separators)
|
||||
* happens in the UI layer, never in the export.
|
||||
*
|
||||
* Cap note (DoD §56): NO row truncation/limiting — an export must be complete.
|
||||
*/
|
||||
|
||||
function csvEscape(value: string): string {
|
||||
if (/[",\r\n]/.test(value)) return `"${value.replace(/"/g, '""')}"`;
|
||||
return value;
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const auth = await requireOrderOwner(req);
|
||||
if (auth.response) return auth.response;
|
||||
try {
|
||||
const orders = await listOwnedOrders(auth.owner.tenantId);
|
||||
const header = ["order_id", "flow", "status", "deposit_ore", "total_ore"];
|
||||
const rows = orders.map((o) =>
|
||||
[o.order_id, o.flow, o.status, String(o.deposit_ore), String(o.total_ore)].map(csvEscape).join(","),
|
||||
);
|
||||
const csv = [header.join(","), ...rows].join("\r\n") + "\r\n";
|
||||
const date = new Date().toISOString().slice(0, 10);
|
||||
return new NextResponse(csv, {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "text/csv; charset=utf-8",
|
||||
"Content-Disposition": `attachment; filename="orders-${date}.csv"`,
|
||||
},
|
||||
});
|
||||
} catch (err: unknown) {
|
||||
if (isSiaxError(err)) return siaxErrorResponseForLegacy(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,270 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { createOrder, type OrderConfig, type OrderOwner } from "@/lib/orders-store";
|
||||
import { requireOrderOwner } from "@/src/auth/require-order-owner";
|
||||
import { isSiaxError, siaxErrorResponseForLegacy } from "../../siax-error-mapping";
|
||||
import { createAud0Client } from "@/src/clients/aud0-client";
|
||||
import { validateRows, defineMapping, type RowError } from "@siax/contracts/import-export";
|
||||
import { serviceById, type Flow } from "@/lib/services";
|
||||
import { IMPORT_DISPATCH_POLICY, evaluateImportDispatch } from "@/lib/import-dispatch-policy";
|
||||
import { ENTITY_TYPES, FREQUENCIES, type EntityType, type Frequency } from "@/lib/data";
|
||||
|
||||
// C8: CSV-import of orders — the symmetric counterpart of GET /api/orders/export
|
||||
// (våg 3.4). The server is the ONLY authority: the raw CSV is parsed here
|
||||
// (RFC 4180, no eval), re-validated here via the frozen @siax/contracts kernel
|
||||
// (validateRows) plus host field rules, and executed through the EXISTING order
|
||||
// creation logic (createOrder) so imported rows are real tenant-owned orders.
|
||||
// Client-side wizard parsing is presentation entropy, never trusted.
|
||||
//
|
||||
// Fail-closed properties:
|
||||
// - No principal (requireOrderOwner) → 401, no parse, no writes.
|
||||
// - Unknown header column → 400 (never silently dropped).
|
||||
// - More than MAX_IMPORT_ROWS data rows → 413 (cap, never truncate silently).
|
||||
// - execute() re-validates and imports only rows that pass; failing rows are
|
||||
// reported per row (row/field/code) and never created. Each order insert is
|
||||
// individually atomic via createOrder (per-row transactional semantics);
|
||||
// a failing row can never corrupt a passing one.
|
||||
// - Audit: every successful execute emits an AUD0 event via 0pt's sole producer
|
||||
// boundary (durable outbox when DATABASE_URL is configured). Emission failure
|
||||
// is logged, never blocks the import (the outbox pattern owns delivery).
|
||||
|
||||
// DES1GN ADR D3 (§31): taken härstammar från ImportDispatchPolicy — ett objekt,
|
||||
// en sanning. Inline-taket är oförändrat (500); rader > policy.maxRows (5000)
|
||||
// är N0D-dispatch-signalen (descriptor-gate: svarar 413 + hint tills N0D-workern
|
||||
// är verifierad — aldrig tyst trunkering, aldrig tyst dispatch).
|
||||
export const MAX_IMPORT_ROWS = IMPORT_DISPATCH_POLICY.maxInlineRows;
|
||||
const MAX_CSV_CHARS = IMPORT_DISPATCH_POLICY.maxBytes;
|
||||
const IMPORT_COLUMNS = ["flow", "service_type", "type", "volume", "frequency", "markets"] as const;
|
||||
const FLOWS: readonly Flow[] = ["catalog", "custom", "subscription"];
|
||||
const MAX_VOLUME = 1_000_000;
|
||||
|
||||
interface ImportRowError extends RowError {
|
||||
message: string;
|
||||
}
|
||||
|
||||
interface ParsedCsv {
|
||||
rows: Record<string, string>[];
|
||||
headerError?: string;
|
||||
}
|
||||
|
||||
/** Minimal RFC 4180 parser: quoted fields, doubled quotes, CRLF or LF. No eval. */
|
||||
export function parseCsv(text: string): ParsedCsv {
|
||||
const rows: string[][] = [];
|
||||
let field = "";
|
||||
let row: string[] = [];
|
||||
let inQuotes = false;
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
const ch = text[i];
|
||||
if (inQuotes) {
|
||||
if (ch === '"') {
|
||||
if (text[i + 1] === '"') {
|
||||
field += '"';
|
||||
i++;
|
||||
} else {
|
||||
inQuotes = false;
|
||||
}
|
||||
} else {
|
||||
field += ch;
|
||||
}
|
||||
} else if (ch === '"') {
|
||||
inQuotes = true;
|
||||
} else if (ch === ",") {
|
||||
row.push(field);
|
||||
field = "";
|
||||
} else if (ch === "\n") {
|
||||
row.push(field);
|
||||
field = "";
|
||||
rows.push(row);
|
||||
row = [];
|
||||
} else if (ch === "\r") {
|
||||
// consumed by the following \n (or ignored for bare CR)
|
||||
} else {
|
||||
field += ch;
|
||||
}
|
||||
}
|
||||
if (field.length > 0 || row.length > 0) {
|
||||
row.push(field);
|
||||
rows.push(row);
|
||||
}
|
||||
const nonEmpty = rows.filter((r) => r.length > 1 || (r[0] ?? "").trim() !== "");
|
||||
if (nonEmpty.length === 0) return { rows: [] };
|
||||
const header = nonEmpty[0].map((h) => h.trim().toLowerCase());
|
||||
const unknown = header.filter((h) => h !== "" && !(IMPORT_COLUMNS as readonly string[]).includes(h));
|
||||
if (unknown.length > 0) {
|
||||
return { rows: [], headerError: `Okänd kolumn: ${unknown.join(", ")} (tillåtna: ${IMPORT_COLUMNS.join(", ")})` };
|
||||
}
|
||||
return {
|
||||
rows: nonEmpty.slice(1).map((cells) => {
|
||||
const obj: Record<string, string> = {};
|
||||
header.forEach((h, idx) => {
|
||||
if (h) obj[h] = (cells[idx] ?? "").trim();
|
||||
});
|
||||
return obj;
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
/** Host field rules on top of the kernel's required/type checks. */
|
||||
function hostValidate(rows: Record<string, string>[]): ImportRowError[] {
|
||||
const errors: ImportRowError[] = [];
|
||||
rows.forEach((r, idx) => {
|
||||
const rowNo = idx + 1;
|
||||
const flow = (r.flow ?? "").trim() as Flow;
|
||||
if (flow && !FLOWS.includes(flow)) {
|
||||
errors.push({ row: rowNo, field: "flow", code: "bad_type", message: `Ogiltigt flow "${r.flow}" (tillåtna: ${FLOWS.join(", ")})` });
|
||||
}
|
||||
const st = (r.service_type ?? "").trim();
|
||||
if (st && !serviceById(st)) {
|
||||
errors.push({ row: rowNo, field: "service_type", code: "bad_type", message: `Okänd service_type "${st}"` });
|
||||
}
|
||||
const type = (r.type ?? "").trim();
|
||||
if (type && !ENTITY_TYPES.some((t) => t.id === type)) {
|
||||
errors.push({ row: rowNo, field: "type", code: "bad_type", message: `Ogiltig typ "${type}" (tillåtna: ${ENTITY_TYPES.map((t) => t.id).join(", ")})` });
|
||||
}
|
||||
const volume = (r.volume ?? "").trim();
|
||||
if (volume === "") {
|
||||
// optional — order logic applies flow defaults
|
||||
} else if (!/^\d+$/.test(volume) || Number(volume) < 1 || Number(volume) > MAX_VOLUME) {
|
||||
errors.push({ row: rowNo, field: "volume", code: "bad_type", message: `volume måste vara ett positivt heltal ≤ ${MAX_VOLUME}` });
|
||||
}
|
||||
const freq = (r.frequency ?? "").trim();
|
||||
if (freq && !FREQUENCIES.includes(freq as Frequency)) {
|
||||
errors.push({ row: rowNo, field: "frequency", code: "bad_type", message: `Ogiltig frequency "${freq}" (tillåtna: ${FREQUENCIES.join(", ")})` });
|
||||
}
|
||||
});
|
||||
return errors;
|
||||
}
|
||||
|
||||
function buildConfig(r: Record<string, string>): OrderConfig {
|
||||
const config: OrderConfig = {};
|
||||
if ((r.service_type ?? "").trim()) config.service_type = r.service_type.trim();
|
||||
if ((r.type ?? "").trim()) config.type = r.type.trim() as EntityType;
|
||||
if ((r.volume ?? "").trim()) config.volume = Number(r.volume.trim());
|
||||
if ((r.frequency ?? "").trim()) config.frequency = r.frequency.trim() as Frequency;
|
||||
if ((r.markets ?? "").trim()) {
|
||||
config.markets = r.markets.split(";").map((m) => m.trim()).filter(Boolean);
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
export interface ImportReport {
|
||||
totalRows: number;
|
||||
validRows: number;
|
||||
errorCount: number;
|
||||
errors: ImportRowError[];
|
||||
}
|
||||
|
||||
/** Server-authoritative validation: kernel required/type checks + host rules. */
|
||||
export function validateImport(csv: string): { report: ImportReport; rows: Record<string, string>[]; headerError?: string } {
|
||||
const { rows, headerError } = parseCsv(csv);
|
||||
if (headerError) return { report: { totalRows: 0, validRows: 0, errorCount: 1, errors: [{ row: 0, field: "header", code: "format", message: headerError }] }, rows: [], headerError };
|
||||
const mapping = defineMapping({ columns: IMPORT_COLUMNS.map((c) => ({ source: c, target: c, required: c === "flow" })) });
|
||||
const kernel = validateRows(rows, mapping);
|
||||
const host = hostValidate(rows);
|
||||
const kernelErrors: ImportRowError[] = kernel.errors.map((e) => ({ ...e, message: e.message ?? `Valideringsfel (${e.code})` }));
|
||||
const errors = [...kernelErrors, ...host];
|
||||
return {
|
||||
report: {
|
||||
totalRows: rows.length,
|
||||
validRows: rows.length - new Set(errors.map((e) => e.row)).size,
|
||||
errorCount: errors.length,
|
||||
errors,
|
||||
},
|
||||
rows,
|
||||
};
|
||||
}
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const auth = await requireOrderOwner(req);
|
||||
if (auth.response) return auth.response;
|
||||
const owner: OrderOwner = auth.owner;
|
||||
|
||||
let body: { mode?: string; csv?: string };
|
||||
try {
|
||||
body = await req.json();
|
||||
} catch {
|
||||
return NextResponse.json({ error: { code: "bad_json", message: "Invalid JSON" } }, { status: 400 });
|
||||
}
|
||||
const mode = body.mode;
|
||||
const csv = body.csv;
|
||||
if (!mode || !["validate", "dry_run", "execute"].includes(mode)) {
|
||||
return NextResponse.json({ error: { code: "bad_mode", message: "mode måste vara validate | dry_run | execute" } }, { status: 400 });
|
||||
}
|
||||
if (typeof csv !== "string" || csv.trim() === "") {
|
||||
return NextResponse.json({ error: { code: "bad_csv", message: "csv saknas eller är tom" } }, { status: 400 });
|
||||
}
|
||||
const csvBytes = Buffer.byteLength(csv, "utf8");
|
||||
if (evaluateImportDispatch({ format: "csv", bytes: csvBytes, rows: 0 }).verdict === "byte_cap_exceeded") {
|
||||
return NextResponse.json({ error: { code: "csv_too_large", message: `CSV större än ${MAX_CSV_CHARS} bytes` } }, { status: 413 });
|
||||
}
|
||||
try {
|
||||
const { report, rows, headerError } = validateImport(csv);
|
||||
if (headerError) {
|
||||
return NextResponse.json({ error: { code: "bad_header", message: headerError }, report }, { status: 400 });
|
||||
}
|
||||
const dispatchVerdict = evaluateImportDispatch({ format: "csv", bytes: csvBytes, rows: rows.length });
|
||||
if (dispatchVerdict.verdict === "dispatch_n0d") {
|
||||
// DES1GN D3-signalen: den här volymen ÄR ett N0D-jobb (0pt.import_execute_job).
|
||||
// Descriptor-gaten är stängd (N0D-worker UNVERIFIED) -> fail-closed 413 med hint.
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: {
|
||||
code: "n0d_dispatch_required",
|
||||
message: `Import med ${rows.length} rader ska köras som N0D-jobb (0pt.import_execute_job); dispatch är ännu inte aktiverad`,
|
||||
hint: { capability_id: "0pt.import_execute", job_type: "0pt.import_execute_job", status: "UNVERIFIED" },
|
||||
},
|
||||
},
|
||||
{ status: 413 },
|
||||
);
|
||||
}
|
||||
if (dispatchVerdict.verdict === "row_cap_exceeded") {
|
||||
return NextResponse.json({ error: { code: "row_cap_exceeded", message: `Max ${MAX_IMPORT_ROWS} rader per import` } }, { status: 413 });
|
||||
}
|
||||
|
||||
if (mode === "validate" || mode === "dry_run") {
|
||||
// No writes — dry_run is exactly validate plus an explicit rehearsal count.
|
||||
return NextResponse.json({ mode, ...report, would_import: report.validRows });
|
||||
}
|
||||
|
||||
// mode === "execute": server re-validates and imports ONLY passing rows.
|
||||
const failedRows = new Set(report.errors.map((e) => e.row));
|
||||
const orderIds: string[] = [];
|
||||
let imported = 0;
|
||||
for (let idx = 0; idx < rows.length; idx++) {
|
||||
if (failedRows.has(idx + 1)) continue;
|
||||
const r = rows[idx];
|
||||
try {
|
||||
const { order } = await createOrder((r.flow ?? "").trim(), buildConfig(r), undefined, owner);
|
||||
orderIds.push(order.order_id);
|
||||
imported++;
|
||||
} catch (err: unknown) {
|
||||
report.errors.push({ row: idx + 1, field: "row", code: "format", message: `Skapning misslyckades: ${err instanceof Error ? err.message : "okänt fel"}` });
|
||||
}
|
||||
}
|
||||
|
||||
// AUD0 audit — 0pt's sole producer boundary; durable outbox under DATABASE_URL.
|
||||
try {
|
||||
const aud0 = createAud0Client();
|
||||
await aud0.ingestEvent({
|
||||
event_id: (globalThis.crypto?.randomUUID?.() ?? `evt-${Date.now()}`),
|
||||
event_type: "0pt.orders.import",
|
||||
tenant_id: owner.tenantId,
|
||||
project_id: "0pt-orders",
|
||||
actor_id: owner.subject,
|
||||
app_id: "0pt",
|
||||
capability_id: "orders.import",
|
||||
resource_type: "order",
|
||||
resource_id: orderIds[0] ?? "none",
|
||||
risk_level: "L2",
|
||||
payload: { imported, total_rows: report.totalRows, error_count: report.errors.length, order_ids: orderIds },
|
||||
created_at: new Date().toISOString(),
|
||||
});
|
||||
} catch (auditErr: unknown) {
|
||||
console.error("[0pt-orders-import] AUD0 emission failed (import not rolled back):", auditErr);
|
||||
}
|
||||
|
||||
return NextResponse.json({ mode, imported, totalRows: report.totalRows, validRows: report.validRows, errorCount: report.errors.length, errors: report.errors.slice(0, 100), order_ids: orderIds });
|
||||
} catch (err: unknown) {
|
||||
if (isSiaxError(err)) return siaxErrorResponseForLegacy(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { createOrder, listOwnedOrders, IdempotencyKeyConflictError, type OrderConfig } from "@/lib/orders-store";
|
||||
import { requireOrderOwner } from "@/src/auth/require-order-owner";
|
||||
import { resolveAdvanceAction, buildOrderInboxItem } from "@/lib/order-flow";
|
||||
import { isSiaxError, siaxErrorResponseForLegacy } from "../siax-error-mapping";
|
||||
|
||||
// POST /api/orders (docs/04) — create a draft order from a configurator config.
|
||||
@@ -56,7 +57,10 @@ export async function GET(req: NextRequest) {
|
||||
if (auth.response) return auth.response;
|
||||
try {
|
||||
const orders = await listOwnedOrders(auth.owner.tenantId);
|
||||
return NextResponse.json(orders);
|
||||
// ADR-0001: servern resolvér advance-aktionen per order — klienten
|
||||
// renderar bara (ActionGate). Policy ägs här, inte i UI-lagret.
|
||||
const withActions = orders.map((o) => ({ ...o, advance_action: resolveAdvanceAction(o.status), inbox_item: buildOrderInboxItem(o) }));
|
||||
return NextResponse.json(withActions);
|
||||
} catch (err: unknown) {
|
||||
if (isSiaxError(err)) return siaxErrorResponseForLegacy(err);
|
||||
throw err;
|
||||
|
||||
@@ -10,9 +10,11 @@ import { checkAllDependencies, isReady } from "@/lib/health-checks";
|
||||
// readiness probe at this route, not /api/health, to stop routing traffic to
|
||||
// an instance whose database is down without restarting it.
|
||||
//
|
||||
// Today the only load-bearing dependency is the database (see
|
||||
// Load-bearing by absence: only the database (see
|
||||
// `lib/health-checks.ts#isReady`) -- engine/stripe/queue are optional
|
||||
// integrations 0pt is designed to run without.
|
||||
// integrations 0pt is designed to run without. Load-bearing by FAILURE: all
|
||||
// of them. A dependency somebody configured and that then fails a live round
|
||||
// trip makes this instance not-ready, whichever one it is.
|
||||
export async function GET() {
|
||||
const deps = await checkAllDependencies();
|
||||
const ready = isReady(deps);
|
||||
|
||||
@@ -79,7 +79,7 @@ export default function DescribePage() {
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
{svc ? <p className="mt-3 text-sm text-muted-foreground">{t(svc.covers)} <span className="text-muted-foreground/60">→ {t(svc.deliverable)}</span></p> : null}
|
||||
{svc ? <p className="mt-3 text-sm text-muted-foreground">{t(svc.covers)} <span className="text-muted-foreground">→ {t(svc.deliverable)}</span></p> : null}
|
||||
</div>
|
||||
|
||||
<div className="mt-8">
|
||||
|
||||
+2
-2
@@ -273,7 +273,7 @@ export default function SourcesPage() {
|
||||
>
|
||||
<td className="px-3 py-3 align-top">
|
||||
<span className="text-[15px] tracking-tight">{s.name}</span>
|
||||
<span className="ml-2 font-mono text-[10px] uppercase tracking-[0.08em] text-muted-foreground/60">
|
||||
<span className="ml-2 font-mono text-[10px] uppercase tracking-[0.08em] text-muted-foreground">
|
||||
{s.source_id}
|
||||
</span>
|
||||
</td>
|
||||
@@ -301,7 +301,7 @@ export default function SourcesPage() {
|
||||
<p className="font-mono text-[10.5px] uppercase tracking-[0.08em] text-muted-foreground">
|
||||
{rows.length} {t({ en: "sources · every field carries its provenance", sv: "källor · varje fält bär sin härkomst" })}
|
||||
</p>
|
||||
<p className="font-mono text-[10px] uppercase tracking-[0.08em] text-muted-foreground/70">
|
||||
<p className="font-mono text-[10px] uppercase tracking-[0.08em] text-muted-foreground">
|
||||
{t({ en: "Licence terms verified before use", sv: "Licensvillkor verifieras innan användning" })}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import { useParams } from "next/navigation";
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import { useParams, useRouter, useSearchParams } from "next/navigation";
|
||||
import { Suspense, useCallback, useEffect, useRef, useState } from "react";
|
||||
import { Header } from "@/components/header";
|
||||
import { useT } from "@/components/locale-provider";
|
||||
import { DataArtifact } from "@/components/data-artifact";
|
||||
@@ -142,14 +142,43 @@ function findDataset(raw: string | undefined): Dataset | undefined {
|
||||
return CATALOG.find((d) => d.id === slug);
|
||||
}
|
||||
|
||||
const TAB_KEYS = TABS.map((t) => t.key);
|
||||
function isTabKey(value: string | null): value is TabKey {
|
||||
return value !== null && (TAB_KEYS as string[]).includes(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Flikvalet lever i URL:en (?tab=…, kernel-konventionen: URL state är kanoniskt)
|
||||
* — "Tillbaka" återställer flik, och en delad länk öppnar samma flik. Komponenten
|
||||
* wrappas i Suspense eftersom useSearchParams kräver det vid prerender.
|
||||
*/
|
||||
export default function DatasetDetailPage() {
|
||||
return (
|
||||
<Suspense fallback={null}>
|
||||
<DatasetDetailInner />
|
||||
</Suspense>
|
||||
);
|
||||
}
|
||||
|
||||
function DatasetDetailInner() {
|
||||
const t = useT();
|
||||
const { id } = useParams<{ id: string }>();
|
||||
const reveal = useReveal<HTMLDivElement>();
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
|
||||
const tabParam = searchParams.get("tab");
|
||||
const tab: TabKey = isTabKey(tabParam) ? tabParam : "overview";
|
||||
function setTabNext(next: TabKey) {
|
||||
const params = new URLSearchParams(searchParams.toString());
|
||||
if (next === "overview") params.delete("tab");
|
||||
else params.set("tab", next);
|
||||
const qs = params.toString();
|
||||
router.replace(qs ? `?${qs}` : window.location.pathname, { scroll: false });
|
||||
}
|
||||
|
||||
const [status, setStatus] = useState<"loading" | "ready" | "empty" | "error">("loading");
|
||||
const [dataset, setDataset] = useState<Dataset | null>(null);
|
||||
const [tab, setTab] = useState<TabKey>("overview");
|
||||
const attempt = useRef(0);
|
||||
|
||||
const load = useCallback(() => {
|
||||
@@ -195,7 +224,7 @@ export default function DatasetDetailPage() {
|
||||
<section className="py-24">
|
||||
<ErrorState title={t({ en: "Couldn't load the dataset", sv: "Kunde inte hämta datasetet" })} onRetry={load} />
|
||||
<div className="mt-10">
|
||||
<Link href="/#katalog" className="font-mono text-[11px] uppercase tracking-[0.1em] text-muted-foreground underline-offset-4 hover:text-foreground hover:underline">
|
||||
<Link href="/katalog" className="font-mono text-[11px] uppercase tracking-[0.1em] text-muted-foreground underline-offset-4 hover:text-foreground hover:underline">
|
||||
{t({ en: "← Back to the catalog", sv: "← Till katalogen" })}
|
||||
</Link>
|
||||
</div>
|
||||
@@ -211,14 +240,14 @@ export default function DatasetDetailPage() {
|
||||
ctaLabel={t({ en: "Compose a job", sv: "Komponera ett jobb" })}
|
||||
/>
|
||||
<div className="mt-10">
|
||||
<Link href="/#katalog" className="font-mono text-[11px] uppercase tracking-[0.1em] text-muted-foreground underline-offset-4 hover:text-foreground hover:underline">
|
||||
<Link href="/katalog" className="font-mono text-[11px] uppercase tracking-[0.1em] text-muted-foreground underline-offset-4 hover:text-foreground hover:underline">
|
||||
{t({ en: "← Back to the catalog", sv: "← Till katalogen" })}
|
||||
</Link>
|
||||
</div>
|
||||
</section>
|
||||
) : null}
|
||||
|
||||
{status === "ready" && dataset ? <DatasetView d={dataset} tab={tab} setTab={setTab} /> : null}
|
||||
{status === "ready" && dataset ? <DatasetView d={dataset} tab={tab} setTab={setTabNext} /> : null}
|
||||
</div>
|
||||
</main>
|
||||
);
|
||||
@@ -249,7 +278,7 @@ function DatasetView({ d, tab, setTab }: { d: Dataset; tab: TabKey; setTab: (k:
|
||||
<div ref={revealRef}>
|
||||
{/* breadcrumb */}
|
||||
<div className="reveal flex items-center gap-2 border-b border-border py-4 font-mono text-[11px] uppercase tracking-[0.12em] text-muted-foreground">
|
||||
<Link href="/#katalog" className="underline-offset-4 transition-colors hover:text-foreground hover:underline">
|
||||
<Link href="/katalog" className="underline-offset-4 transition-colors hover:text-foreground hover:underline">
|
||||
{t({ en: "Catalog", sv: "Katalog" })}
|
||||
</Link>
|
||||
<span className="text-border">/</span>
|
||||
|
||||
+49
-7
@@ -1,8 +1,8 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { useRouter, useSearchParams } from "next/navigation";
|
||||
import { Suspense, useEffect, useMemo, useRef, useState } from "react";
|
||||
import { useT } from "@/components/locale-provider";
|
||||
import { Header } from "@/components/header";
|
||||
import { useReveal } from "@/hooks/use-reveal";
|
||||
@@ -37,14 +37,56 @@ function level(c: number): ProvLevel {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter + sök lever i URL:en (kernel-konventionen: URL state är kanoniskt och
|
||||
* återställbart) — "Tillbaka" från en detaljsida återställer exakt vy, och en
|
||||
* delad länk öppnar samma filtrering. pages-komponenten wrappar i Suspense
|
||||
* eftersom useSearchParams kräver det vid prerender.
|
||||
*/
|
||||
export default function CatalogPage() {
|
||||
return (
|
||||
<Suspense fallback={null}>
|
||||
<CatalogInner />
|
||||
</Suspense>
|
||||
);
|
||||
}
|
||||
|
||||
function CatalogInner() {
|
||||
const t = useT();
|
||||
const ref = useReveal<HTMLDivElement>();
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
const [items, setItems] = useState<CatalogItem[] | null>(null);
|
||||
const [err, setErr] = useState(false);
|
||||
const [filter, setFilter] = useState("all");
|
||||
const [q, setQ] = useState("");
|
||||
const filter = searchParams.get("filter") ?? "all";
|
||||
const q = searchParams.get("q") ?? "";
|
||||
// Lokalt utkast för sökfältet (skrivtick), synkat debouncerat till URL:en.
|
||||
const [qDraft, setQDraft] = useState(q);
|
||||
const urlSync = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
useEffect(() => { setQDraft(q); }, [q]);
|
||||
|
||||
function applyState(next: { q?: string; filter?: string }) {
|
||||
const params = new URLSearchParams();
|
||||
const nq = (next.q ?? q).trim().slice(0, 120);
|
||||
const nf = next.filter ?? filter;
|
||||
if (nq) params.set("q", nq);
|
||||
if (nf && nf !== "all") params.set("filter", nf);
|
||||
const qs = params.toString();
|
||||
router.replace(qs ? `/katalog?${qs}` : "/katalog", { scroll: false });
|
||||
}
|
||||
|
||||
function setFilterNext(next: string) {
|
||||
if (urlSync.current) clearTimeout(urlSync.current);
|
||||
applyState({ filter: next });
|
||||
}
|
||||
|
||||
function setQNext(next: string) {
|
||||
setQDraft(next);
|
||||
if (urlSync.current) clearTimeout(urlSync.current);
|
||||
urlSync.current = setTimeout(() => applyState({ q: next }), 250);
|
||||
}
|
||||
|
||||
useEffect(() => () => { if (urlSync.current) clearTimeout(urlSync.current); }, []);
|
||||
|
||||
function load() {
|
||||
setErr(false);
|
||||
@@ -89,7 +131,7 @@ export default function CatalogPage() {
|
||||
{FILTERS.map((f) => (
|
||||
<button
|
||||
key={f.key}
|
||||
onClick={() => setFilter(f.key)}
|
||||
onClick={() => setFilterNext(f.key)}
|
||||
aria-pressed={filter === f.key}
|
||||
className="border px-3 py-1.5 font-mono text-[11px] uppercase tracking-[0.08em] transition-colors"
|
||||
style={{
|
||||
@@ -103,8 +145,8 @@ export default function CatalogPage() {
|
||||
))}
|
||||
</div>
|
||||
<input
|
||||
value={q}
|
||||
onChange={(e) => setQ(e.target.value)}
|
||||
value={qDraft}
|
||||
onChange={(e) => setQNext(e.target.value)}
|
||||
placeholder={t({ en: "Search datasets…", sv: "Sök dataset…" })}
|
||||
aria-label={t({ en: "Search datasets", sv: "Sök dataset" })}
|
||||
className="w-full max-w-xs border border-border bg-input px-3 py-2 font-mono text-xs outline-none focus:border-foreground sm:w-auto"
|
||||
|
||||
+1
-1
@@ -286,7 +286,7 @@ function CopyId({ id }: { id: string }) {
|
||||
setTimeout(() => setCopied(false), 2000);
|
||||
});
|
||||
}}
|
||||
className="text-foreground/20 transition-colors hover:text-foreground/60"
|
||||
className="text-muted-foreground transition-colors hover:text-foreground"
|
||||
>
|
||||
{copied ? "✓" : "⧉"}
|
||||
</button>
|
||||
|
||||
@@ -40,7 +40,7 @@ export default function ServicesPage() {
|
||||
<section key={flow} className="mt-16">
|
||||
<div className="flex items-baseline justify-between border-b border-border pb-4">
|
||||
<h2 className="font-mono text-[11px] uppercase tracking-[0.16em] text-muted-foreground">{t(FLOW_LABEL[flow])}</h2>
|
||||
<span className="font-mono text-[11px] tabular-nums text-muted-foreground/60">{String(items.length).padStart(2, "0")}</span>
|
||||
<span className="font-mono text-[11px] tabular-nums text-muted-foreground">{String(items.length).padStart(2, "0")}</span>
|
||||
</div>
|
||||
<div className="grid grid-cols-1 gap-px border border-border bg-border md:grid-cols-2 lg:grid-cols-3">
|
||||
{items.map((s, i) => {
|
||||
@@ -50,7 +50,7 @@ export default function ServicesPage() {
|
||||
<h3 className="text-lg font-semibold tracking-[-0.01em]">{t(s.name)}</h3>
|
||||
<p className="mt-1 font-mono text-[11px] uppercase tracking-[0.06em] text-muted-foreground">{t(s.tagline)}</p>
|
||||
<p className="mt-4 flex-1 text-sm text-muted-foreground">{t(s.covers)}</p>
|
||||
<p className="mt-4 font-mono text-[10.5px] uppercase tracking-[0.08em] text-muted-foreground/70">→ {t(s.deliverable)}</p>
|
||||
<p className="mt-4 font-mono text-[10.5px] uppercase tracking-[0.08em] text-muted-foreground">→ {t(s.deliverable)}</p>
|
||||
<Link href={cta.href} className="mt-5 inline-flex items-center gap-2 font-mono text-[11px] uppercase tracking-[0.1em] underline-offset-4 hover:underline">
|
||||
{t(cta.label)}
|
||||
<span aria-hidden className="transition-transform duration-300 group-hover:translate-x-1">→</span>
|
||||
|
||||
@@ -123,7 +123,7 @@ export function Header() {
|
||||
className="flex items-baseline justify-between border-b border-border py-4 font-mono text-[12px] uppercase tracking-[0.12em] text-muted-foreground transition-colors hover:text-foreground"
|
||||
>
|
||||
<span>{t(n.label)}</span>
|
||||
<span className="text-[10px] tabular-nums text-muted-foreground/50">{String(i + 1).padStart(2, "0")}</span>
|
||||
<span className="text-[10px] tabular-nums text-muted-foreground">{String(i + 1).padStart(2, "0")}</span>
|
||||
</Link>
|
||||
))}
|
||||
<div className="flex items-center justify-between py-4">
|
||||
|
||||
@@ -44,7 +44,7 @@ export function CollectionSection() {
|
||||
>
|
||||
<div className="flex items-baseline justify-between">
|
||||
<p className="font-mono text-sm font-semibold tracking-tight">{m.code}</p>
|
||||
<span className="font-mono text-[9px] text-muted-foreground/60">{String(i + 1).padStart(2, "0")}</span>
|
||||
<span className="font-mono text-[9px] text-muted-foreground">{String(i + 1).padStart(2, "0")}</span>
|
||||
</div>
|
||||
<p className="mt-1 font-mono text-[10px] uppercase tracking-[0.08em] text-muted-foreground">
|
||||
{m.live ? `${m.coverage}%` : t({ en: "coming", sv: "kommande" })}
|
||||
|
||||
@@ -91,7 +91,7 @@ export function FeaturedProductsSection() {
|
||||
<span className="absolute left-0 top-0 h-px w-0 transition-all duration-300 group-hover:w-full" style={{ backgroundColor: "var(--accent-red)" }} />
|
||||
<div className="flex items-baseline justify-between">
|
||||
<p className="font-mono text-[10px] uppercase tracking-[0.14em] text-muted-foreground">{d.markets}</p>
|
||||
<span className="font-mono text-[10px] tabular-nums text-foreground/20">{String(i + 1).padStart(2, "0")}</span>
|
||||
<span className="font-mono text-[10px] tabular-nums text-muted-foreground">{String(i + 1).padStart(2, "0")}</span>
|
||||
</div>
|
||||
<h3 className="mt-5 text-xl font-semibold tracking-[-0.01em] transition-transform duration-300 group-hover:translate-x-1">{t(d.title)}</h3>
|
||||
<p className="mt-2 text-sm text-muted-foreground">{t(d.desc)}</p>
|
||||
|
||||
@@ -33,7 +33,7 @@ export function PhilosophySection() {
|
||||
className={`reveal delay-${i + 1} group relative grid grid-cols-[3.5rem_1fr] items-baseline gap-8 border-b border-border py-8 transition-[padding] duration-300 ease-out hover:pl-2`}
|
||||
>
|
||||
<span className="absolute -left-2 top-0 h-px w-0 bg-foreground transition-all duration-500 ease-out group-hover:w-full group-hover:opacity-100" aria-hidden />
|
||||
<span className="font-mono text-[clamp(1.5rem,2vw,2.25rem)] font-medium leading-none tracking-[-0.02em] text-muted-foreground/40 transition-colors duration-300 group-hover:text-foreground">
|
||||
<span className="font-mono text-[clamp(1.5rem,2vw,2.25rem)] font-medium leading-none tracking-[-0.02em] text-muted-foreground transition-colors duration-300 group-hover:text-foreground">
|
||||
{n}
|
||||
</span>
|
||||
<div>
|
||||
|
||||
@@ -64,7 +64,7 @@ export function PricingSection() {
|
||||
{MODELS.map((m, i) => (
|
||||
<div key={m.index} className={`reveal delay-${i + 1} group relative flex flex-col bg-background p-8`}>
|
||||
<div className="flex items-start justify-between">
|
||||
<span className="font-mono text-[11px] uppercase tracking-[0.18em] text-muted-foreground/60">{m.index}</span>
|
||||
<span className="font-mono text-[11px] uppercase tracking-[0.18em] text-muted-foreground">{m.index}</span>
|
||||
{m.featured ? <span className="mt-1 inline-block h-2 w-2" style={{ backgroundColor: "var(--accent-red)" }} /> : null}
|
||||
</div>
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ export function TestimonialsSection() {
|
||||
key={l.sv}
|
||||
className={`reveal delay-${i + 2} group relative border-b border-border py-12 sm:border-b-0 sm:border-l sm:py-0 sm:pl-8 sm:pr-4 ${i === 0 ? "sm:border-l-0 sm:pl-0" : ""}`}
|
||||
>
|
||||
<p className="font-mono text-[11px] tabular-nums text-muted-foreground/60">0{i + 1}</p>
|
||||
<p className="font-mono text-[11px] tabular-nums text-muted-foreground">0{i + 1}</p>
|
||||
<p
|
||||
className="mt-3 text-[clamp(3.2rem,8vw,6rem)] font-semibold leading-[0.9] tracking-[-0.04em] transition-transform duration-300 group-hover:-translate-y-0.5"
|
||||
style={{ color: red ? "var(--accent-red)" : "var(--foreground)" }}
|
||||
|
||||
@@ -46,7 +46,7 @@ export function LoadingSkeleton({ rows = 5 }: { rows?: number }) {
|
||||
))}
|
||||
<span className="sr-only">{t({ en: "Loading content", sv: "Laddar innehåll" })}</span>
|
||||
|
||||
<style jsx>{`
|
||||
<style>{`
|
||||
.opt-shimmer {
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
|
||||
@@ -1,353 +0,0 @@
|
||||
'use client'
|
||||
|
||||
import * as React from 'react'
|
||||
import * as RechartsPrimitive from 'recharts'
|
||||
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
// Format: { THEME_NAME: CSS_SELECTOR }
|
||||
const THEMES = { light: '', dark: '.dark' } as const
|
||||
|
||||
export type ChartConfig = {
|
||||
[k in string]: {
|
||||
label?: React.ReactNode
|
||||
icon?: React.ComponentType
|
||||
} & (
|
||||
| { color?: string; theme?: never }
|
||||
| { color?: never; theme: Record<keyof typeof THEMES, string> }
|
||||
)
|
||||
}
|
||||
|
||||
type ChartContextProps = {
|
||||
config: ChartConfig
|
||||
}
|
||||
|
||||
const ChartContext = React.createContext<ChartContextProps | null>(null)
|
||||
|
||||
function useChart() {
|
||||
const context = React.useContext(ChartContext)
|
||||
|
||||
if (!context) {
|
||||
throw new Error('useChart must be used within a <ChartContainer />')
|
||||
}
|
||||
|
||||
return context
|
||||
}
|
||||
|
||||
function ChartContainer({
|
||||
id,
|
||||
className,
|
||||
children,
|
||||
config,
|
||||
...props
|
||||
}: React.ComponentProps<'div'> & {
|
||||
config: ChartConfig
|
||||
children: React.ComponentProps<
|
||||
typeof RechartsPrimitive.ResponsiveContainer
|
||||
>['children']
|
||||
}) {
|
||||
const uniqueId = React.useId()
|
||||
const chartId = `chart-${id || uniqueId.replace(/:/g, '')}`
|
||||
|
||||
return (
|
||||
<ChartContext.Provider value={{ config }}>
|
||||
<div
|
||||
data-slot="chart"
|
||||
data-chart={chartId}
|
||||
className={cn(
|
||||
"[&_.recharts-cartesian-axis-tick_text]:fill-muted-foreground [&_.recharts-cartesian-grid_line[stroke='#ccc']]:stroke-border/50 [&_.recharts-curve.recharts-tooltip-cursor]:stroke-border [&_.recharts-polar-grid_[stroke='#ccc']]:stroke-border [&_.recharts-radial-bar-background-sector]:fill-muted [&_.recharts-rectangle.recharts-tooltip-cursor]:fill-muted [&_.recharts-reference-line_[stroke='#ccc']]:stroke-border flex aspect-video justify-center text-xs [&_.recharts-dot[stroke='#fff']]:stroke-transparent [&_.recharts-layer]:outline-hidden [&_.recharts-sector]:outline-hidden [&_.recharts-sector[stroke='#fff']]:stroke-transparent [&_.recharts-surface]:outline-hidden",
|
||||
className,
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
<ChartStyle id={chartId} config={config} />
|
||||
<RechartsPrimitive.ResponsiveContainer>
|
||||
{children}
|
||||
</RechartsPrimitive.ResponsiveContainer>
|
||||
</div>
|
||||
</ChartContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
const ChartStyle = ({ id, config }: { id: string; config: ChartConfig }) => {
|
||||
const colorConfig = Object.entries(config).filter(
|
||||
([, config]) => config.theme || config.color,
|
||||
)
|
||||
|
||||
if (!colorConfig.length) {
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
<style
|
||||
dangerouslySetInnerHTML={{
|
||||
__html: Object.entries(THEMES)
|
||||
.map(
|
||||
([theme, prefix]) => `
|
||||
${prefix} [data-chart=${id}] {
|
||||
${colorConfig
|
||||
.map(([key, itemConfig]) => {
|
||||
const color =
|
||||
itemConfig.theme?.[theme as keyof typeof itemConfig.theme] ||
|
||||
itemConfig.color
|
||||
return color ? ` --color-${key}: ${color};` : null
|
||||
})
|
||||
.join('\n')}
|
||||
}
|
||||
`,
|
||||
)
|
||||
.join('\n'),
|
||||
}}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
const ChartTooltip = RechartsPrimitive.Tooltip
|
||||
|
||||
function ChartTooltipContent({
|
||||
active,
|
||||
payload,
|
||||
className,
|
||||
indicator = 'dot',
|
||||
hideLabel = false,
|
||||
hideIndicator = false,
|
||||
label,
|
||||
labelFormatter,
|
||||
labelClassName,
|
||||
formatter,
|
||||
color,
|
||||
nameKey,
|
||||
labelKey,
|
||||
}: React.ComponentProps<typeof RechartsPrimitive.Tooltip> &
|
||||
React.ComponentProps<'div'> & {
|
||||
hideLabel?: boolean
|
||||
hideIndicator?: boolean
|
||||
indicator?: 'line' | 'dot' | 'dashed'
|
||||
nameKey?: string
|
||||
labelKey?: string
|
||||
}) {
|
||||
const { config } = useChart()
|
||||
|
||||
const tooltipLabel = React.useMemo(() => {
|
||||
if (hideLabel || !payload?.length) {
|
||||
return null
|
||||
}
|
||||
|
||||
const [item] = payload
|
||||
const key = `${labelKey || item?.dataKey || item?.name || 'value'}`
|
||||
const itemConfig = getPayloadConfigFromPayload(config, item, key)
|
||||
const value =
|
||||
!labelKey && typeof label === 'string'
|
||||
? config[label as keyof typeof config]?.label || label
|
||||
: itemConfig?.label
|
||||
|
||||
if (labelFormatter) {
|
||||
return (
|
||||
<div className={cn('font-medium', labelClassName)}>
|
||||
{labelFormatter(value, payload)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (!value) {
|
||||
return null
|
||||
}
|
||||
|
||||
return <div className={cn('font-medium', labelClassName)}>{value}</div>
|
||||
}, [
|
||||
label,
|
||||
labelFormatter,
|
||||
payload,
|
||||
hideLabel,
|
||||
labelClassName,
|
||||
config,
|
||||
labelKey,
|
||||
])
|
||||
|
||||
if (!active || !payload?.length) {
|
||||
return null
|
||||
}
|
||||
|
||||
const nestLabel = payload.length === 1 && indicator !== 'dot'
|
||||
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
'border-border/50 bg-background grid min-w-[8rem] items-start gap-1.5 rounded-lg border px-2.5 py-1.5 text-xs shadow-xl',
|
||||
className,
|
||||
)}
|
||||
>
|
||||
{!nestLabel ? tooltipLabel : null}
|
||||
<div className="grid gap-1.5">
|
||||
{payload.map((item, index) => {
|
||||
const key = `${nameKey || item.name || item.dataKey || 'value'}`
|
||||
const itemConfig = getPayloadConfigFromPayload(config, item, key)
|
||||
const indicatorColor = color || item.payload.fill || item.color
|
||||
|
||||
return (
|
||||
<div
|
||||
key={item.dataKey}
|
||||
className={cn(
|
||||
'[&>svg]:text-muted-foreground flex w-full flex-wrap items-stretch gap-2 [&>svg]:h-2.5 [&>svg]:w-2.5',
|
||||
indicator === 'dot' && 'items-center',
|
||||
)}
|
||||
>
|
||||
{formatter && item?.value !== undefined && item.name ? (
|
||||
formatter(item.value, item.name, item, index, item.payload)
|
||||
) : (
|
||||
<>
|
||||
{itemConfig?.icon ? (
|
||||
<itemConfig.icon />
|
||||
) : (
|
||||
!hideIndicator && (
|
||||
<div
|
||||
className={cn(
|
||||
'shrink-0 rounded-[2px] border-(--color-border) bg-(--color-bg)',
|
||||
{
|
||||
'h-2.5 w-2.5': indicator === 'dot',
|
||||
'w-1': indicator === 'line',
|
||||
'w-0 border-[1.5px] border-dashed bg-transparent':
|
||||
indicator === 'dashed',
|
||||
'my-0.5': nestLabel && indicator === 'dashed',
|
||||
},
|
||||
)}
|
||||
style={
|
||||
{
|
||||
'--color-bg': indicatorColor,
|
||||
'--color-border': indicatorColor,
|
||||
} as React.CSSProperties
|
||||
}
|
||||
/>
|
||||
)
|
||||
)}
|
||||
<div
|
||||
className={cn(
|
||||
'flex flex-1 justify-between leading-none',
|
||||
nestLabel ? 'items-end' : 'items-center',
|
||||
)}
|
||||
>
|
||||
<div className="grid gap-1.5">
|
||||
{nestLabel ? tooltipLabel : null}
|
||||
<span className="text-muted-foreground">
|
||||
{itemConfig?.label || item.name}
|
||||
</span>
|
||||
</div>
|
||||
{item.value && (
|
||||
<span className="text-foreground font-mono font-medium tabular-nums">
|
||||
{item.value.toLocaleString()}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const ChartLegend = RechartsPrimitive.Legend
|
||||
|
||||
function ChartLegendContent({
|
||||
className,
|
||||
hideIcon = false,
|
||||
payload,
|
||||
verticalAlign = 'bottom',
|
||||
nameKey,
|
||||
}: React.ComponentProps<'div'> &
|
||||
Pick<RechartsPrimitive.LegendProps, 'payload' | 'verticalAlign'> & {
|
||||
hideIcon?: boolean
|
||||
nameKey?: string
|
||||
}) {
|
||||
const { config } = useChart()
|
||||
|
||||
if (!payload?.length) {
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
className={cn(
|
||||
'flex items-center justify-center gap-4',
|
||||
verticalAlign === 'top' ? 'pb-3' : 'pt-3',
|
||||
className,
|
||||
)}
|
||||
>
|
||||
{payload.map((item) => {
|
||||
const key = `${nameKey || item.dataKey || 'value'}`
|
||||
const itemConfig = getPayloadConfigFromPayload(config, item, key)
|
||||
|
||||
return (
|
||||
<div
|
||||
key={item.value}
|
||||
className={
|
||||
'[&>svg]:text-muted-foreground flex items-center gap-1.5 [&>svg]:h-3 [&>svg]:w-3'
|
||||
}
|
||||
>
|
||||
{itemConfig?.icon && !hideIcon ? (
|
||||
<itemConfig.icon />
|
||||
) : (
|
||||
<div
|
||||
className="h-2 w-2 shrink-0 rounded-[2px]"
|
||||
style={{
|
||||
backgroundColor: item.color,
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
{itemConfig?.label}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// Helper to extract item config from a payload.
|
||||
function getPayloadConfigFromPayload(
|
||||
config: ChartConfig,
|
||||
payload: unknown,
|
||||
key: string,
|
||||
) {
|
||||
if (typeof payload !== 'object' || payload === null) {
|
||||
return undefined
|
||||
}
|
||||
|
||||
const payloadPayload =
|
||||
'payload' in payload &&
|
||||
typeof payload.payload === 'object' &&
|
||||
payload.payload !== null
|
||||
? payload.payload
|
||||
: undefined
|
||||
|
||||
let configLabelKey: string = key
|
||||
|
||||
if (
|
||||
key in payload &&
|
||||
typeof payload[key as keyof typeof payload] === 'string'
|
||||
) {
|
||||
configLabelKey = payload[key as keyof typeof payload] as string
|
||||
} else if (
|
||||
payloadPayload &&
|
||||
key in payloadPayload &&
|
||||
typeof payloadPayload[key as keyof typeof payloadPayload] === 'string'
|
||||
) {
|
||||
configLabelKey = payloadPayload[
|
||||
key as keyof typeof payloadPayload
|
||||
] as string
|
||||
}
|
||||
|
||||
return configLabelKey in config
|
||||
? config[configLabelKey]
|
||||
: config[key as keyof typeof config]
|
||||
}
|
||||
|
||||
export {
|
||||
ChartContainer,
|
||||
ChartTooltip,
|
||||
ChartTooltipContent,
|
||||
ChartLegend,
|
||||
ChartLegendContent,
|
||||
ChartStyle,
|
||||
}
|
||||
@@ -77,7 +77,7 @@ const ToastClose = React.forwardRef<
|
||||
<ToastPrimitives.Close
|
||||
ref={ref}
|
||||
className={cn(
|
||||
'absolute right-2 top-2 rounded-md p-1 text-foreground/50 opacity-0 transition-opacity hover:text-foreground focus:opacity-100 focus:outline-none focus:ring-2 group-hover:opacity-100 group-[.destructive]:text-red-300 group-[.destructive]:hover:text-red-50 group-[.destructive]:focus:ring-red-400 group-[.destructive]:focus:ring-offset-red-600',
|
||||
'absolute right-2 top-2 rounded-md p-1 text-muted-foreground opacity-0 transition-opacity hover:text-foreground focus:opacity-100 focus:outline-none focus:ring-2 group-hover:opacity-100 group-[.destructive]:text-red-300 group-[.destructive]:hover:text-red-50 group-[.destructive]:focus:ring-red-400 group-[.destructive]:focus:ring-offset-red-600',
|
||||
className,
|
||||
)}
|
||||
toast-close=""
|
||||
|
||||
@@ -367,6 +367,60 @@ paths:
|
||||
$ref: "#/components/schemas/ProvenanceRecord"
|
||||
"401":
|
||||
$ref: "#/components/responses/Unauthorized"
|
||||
/v1/0pt/parsing/route:
|
||||
post:
|
||||
operationId: 0pt.route_parsing
|
||||
summary: >
|
||||
(TARGET) Parser-routing/selektiv OCR-beslut (BTI-DOC-02/03/04/05).
|
||||
Kontrakt-first — real kod i 0pt-motor/motor/parsing/routing.py
|
||||
(route_parser m.fl.), INTEGRATION_TESTED men ej driftsatt bakom
|
||||
denna route.
|
||||
x-siax-status: target
|
||||
security: []
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/RouteParsingRequest"
|
||||
responses:
|
||||
"200":
|
||||
description: Routingbeslut (parser/OCR-gren) + ev. nyckeltalskonflikter mellan parsers
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/RouteParsingResponse"
|
||||
"400":
|
||||
$ref: "#/components/responses/BadRequest"
|
||||
"401":
|
||||
$ref: "#/components/responses/Unauthorized"
|
||||
/v1/0pt/normalize:
|
||||
post:
|
||||
operationId: 0pt.normalize_value
|
||||
summary: >
|
||||
(TARGET) Lokal-medveten normalisering — belopp/skala/datum (BTI-NLP-03)
|
||||
och identifierare/checksummor (BTI-NLP-02). Kontrakt-first — real kod
|
||||
i 0pt-motor/motor/normalization/{amounts,identifiers}.py,
|
||||
INTEGRATION_TESTED men ej driftsatt bakom denna route.
|
||||
x-siax-status: target
|
||||
security: []
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/NormalizeValueRequest"
|
||||
responses:
|
||||
"200":
|
||||
description: Normaliserat värde — format_valid/review, aldrig en påhittad korrigering
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/NormalizeValueResponse"
|
||||
"400":
|
||||
$ref: "#/components/responses/BadRequest"
|
||||
"401":
|
||||
$ref: "#/components/responses/Unauthorized"
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Internal service-to-service (Python-engine bridge) — P-002 internal OpenAPI.
|
||||
@@ -811,3 +865,91 @@ components:
|
||||
type: object
|
||||
additionalProperties:
|
||||
$ref: "#/components/schemas/FieldProvenance"
|
||||
RouteParsingRequest:
|
||||
type: object
|
||||
required: [digital_text_page]
|
||||
properties:
|
||||
digital_text_page:
|
||||
type: boolean
|
||||
description: True om sidan har ett digitalt textlager (ej scannad).
|
||||
ocr_available:
|
||||
type: boolean
|
||||
default: false
|
||||
parser_result:
|
||||
type: object
|
||||
description: Föregående parser-försök, om något (tom text = kandidat för OCR-gren).
|
||||
properties:
|
||||
parser:
|
||||
type: string
|
||||
text:
|
||||
type: string
|
||||
key_figures:
|
||||
type: object
|
||||
additionalProperties:
|
||||
type: number
|
||||
compare_with:
|
||||
type: object
|
||||
description: (Valfritt) ett andra parser-resultat att stämma av mot — avvikande nyckeltal blir en konflikt, aldrig en tyst ersättning.
|
||||
properties:
|
||||
parser:
|
||||
type: string
|
||||
text:
|
||||
type: string
|
||||
key_figures:
|
||||
type: object
|
||||
additionalProperties:
|
||||
type: number
|
||||
RouteParsingResponse:
|
||||
type: object
|
||||
required: [routing]
|
||||
properties:
|
||||
routing:
|
||||
type: string
|
||||
enum: [text_parser, ocr_branch, explicit_unavailable]
|
||||
conflicts:
|
||||
type: object
|
||||
description: Fält där de två parser-resultatens nyckeltal skiljer sig (present endast om compare_with skickades).
|
||||
additionalProperties: true
|
||||
NormalizeValueRequest:
|
||||
type: object
|
||||
required: [kind, value]
|
||||
properties:
|
||||
kind:
|
||||
type: string
|
||||
enum: [amount, percent, date, identifier]
|
||||
description: amount/percent/date = motor/normalization/amounts.py. identifier = motor/normalization/identifiers.py.
|
||||
value:
|
||||
type: string
|
||||
description: Rå textsträng att normalisera (belopp, procentsats, datum eller identifierare).
|
||||
locale:
|
||||
type: string
|
||||
description: (kind=amount, valfritt) comma_decimal|dot_decimal — auto-detekteras annars.
|
||||
unit:
|
||||
type: string
|
||||
description: (kind=amount|percent) t.ex. SEK/KSEK/MSEK/GSEK eller "%"/"pp".
|
||||
identifier_kind:
|
||||
type: string
|
||||
enum: [orgnr, cvr, vat, ocr]
|
||||
description: (kind=identifier, krävs) vilken identifierartyp som ska valideras.
|
||||
country:
|
||||
type: string
|
||||
description: (kind=identifier, krävs) ISO-3166 alpha-2, t.ex. SE/NO/DK.
|
||||
description: >
|
||||
Ett fält, en normalisering. amount/percent/date har alltid en explicit
|
||||
precision/skala (aldrig en påhittad dag eller sammanblandad enhet).
|
||||
identifier ger format_valid eller review — aldrig en tyst korrigerad
|
||||
checksumma; existens i register verifieras inte här.
|
||||
NormalizeValueResponse:
|
||||
type: object
|
||||
required: [kind, status]
|
||||
properties:
|
||||
kind:
|
||||
type: string
|
||||
enum: [amount, percent, date, identifier]
|
||||
status:
|
||||
type: string
|
||||
enum: [ok, format_valid, format_invalid, review, unparseable]
|
||||
result:
|
||||
type: object
|
||||
description: Normaliserat resultat, formen beror på kind (se motor/normalization/{amounts,identifiers}.py).
|
||||
additionalProperties: true
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
# P070 — VehIQ OLTP -> 0pt watermark extractor
|
||||
|
||||
Status: built, tested, verified read-only against production. **Not scheduled/activated in production.** Read "What activating this requires" before turning the workflow on.
|
||||
|
||||
## What this is
|
||||
|
||||
A controlled, incremental extractor implementing exactly the pattern Simon specified (`admin/vehiq` 2026-09-25 pass, "Locked architecture decisions from Simon" / `docs/adr/0035-bi-analytics-architecture-product-firewall.md`):
|
||||
|
||||
```
|
||||
OLTP operational tables -> read-only analytical extraction -> watermark ->
|
||||
0pt ingestion -> ClickHouse (opt_dataset_projection)
|
||||
```
|
||||
|
||||
Source: `app.dealer_stock_entry` and `app.deal` on production Postgres (`vehiq-db`, server10) — read-only `SELECT`, never a schema/config change, never touching `wal_level` or replication, never a permanent replica. Sink: `SiaxAnalyticsEngine.ingestProjection` (BI-P009, `opt_dataset_projection` in ClickHouse). Vehicle dimension: a narrow, keyed lookup against `vehiq.vehicle` (98M rows / 85GB) scoped to exactly the `vehicle_id`s referenced by the `dealer_stock_entry` batch just read — never `SELECT * FROM vehicle`, never a full scan.
|
||||
|
||||
## Why this lives in `admin/0pt`, not `admin/vehiq`
|
||||
|
||||
The task that produced this work described building it as a new package inside `admin/vehiq` (e.g. `packages/opt-ingest`). Before writing any code, this repo's own `docs/adr/0035-bi-analytics-architecture-product-firewall.md` (decisions 1, 2, 10 — decided the same day, one-way-door, CI-enforced by `tools/validate_engine_firewall.py`) was read and is unambiguous:
|
||||
|
||||
> VehIQ's own codebase must **never** get its own ClickHouse or DuckDB credentials, and must never import a ClickHouse/DuckDB client directly. ... Product code must never hammer the live OLTP Postgres with analytical queries directly.
|
||||
|
||||
And `admin/vehiq`'s `docs/CURRENT_STATE.md` ("Connection details for a future API0 connector", same pass) says explicitly:
|
||||
|
||||
> VehIQ's own codebase must never see any of this. This is 0pt's data plane; only a future `admin/api0` connector should hold these details.
|
||||
|
||||
Building the extractor inside `admin/vehiq` would have failed `validate_engine_firewall.py` the moment it imported a ClickHouse client or held a `CLICKHOUSE_*` env var, and would have been the exact "temporary VehIQ-owned data-access surface" ADR-0035 names as the failure mode it exists to prevent. So this extractor was built here instead — `admin/0pt`, 0pt's own data plane, a sibling of `admin/api0` (the mechanics belong next to the P060-P068 ingestion plane and the BI-P009/P010 ClickHouse engine that already live in this repo). **`admin/vehiq` was not modified by this work.**
|
||||
|
||||
This is a deliberate deviation from the task's literal repo suggestion, made because the suggestion conflicted with Simon's own same-day, explicit, locked architecture decision discovered while reading the target repo — not a judgment call made against that decision.
|
||||
|
||||
## Real production schema does not match the task's assumed column names
|
||||
|
||||
The task (and `admin/vehiq`'s `docs/CURRENT_STATE.md`) assumed all three OLTP tables carry a generic `updated_at` ("the columns already exist"). Verified live against production (server10, `vehiq-db`, read-only `information_schema.columns`, 2026-09-25) — **this is not true**:
|
||||
|
||||
| table | live columns | usable watermark |
|
||||
|---|---|---|
|
||||
| `app.dealer_stock_entry` | `id, tenant_id, vehicle_id, lot_location, floorplan_status, listed_at, sold_at` | `listed_at` (NOT NULL, insert-time only) |
|
||||
| `app.deal` | `id, tenant_id, party_id, vehicle_id, sale_price_minor, currency, status, created_at` | `created_at` (NOT NULL DEFAULT now(), insert-time only) |
|
||||
| `app.listing` | `id, tenant_id, vehicle_id, asking_price_minor, currency, channels, status` | **none** |
|
||||
|
||||
**`app.listing` has no timestamp column of any kind today.** `admin/vehiq` migration `0084_listing_deal_lifecycle_timestamps.sql` (already written, adds `published_at`/`unpublished_at` to `listing` and `closed_at`/`listing_id` to `deal`) exists in the repo but is **not yet applied to production** (verified: those columns are absent live). Until it lands, there is no honest way to build a watermark connector for `app.listing` — one is not built here. **This extractor covers `dealer_stock_entry` and `deal` only, not `listing`.**
|
||||
|
||||
**Caveat on the two sources that ARE built:** `listed_at`/`created_at` are set once, at INSERT. Neither table has a column that advances when an *existing* row changes (`sold_at`/`floorplan_status` on `dealer_stock_entry`; `status` on `deal`). This extractor reliably sees every new row; it does **not** see an in-place UPDATE to a row it already read, because that row has already passed the watermark. Closing this gap needs either `listing_id`/`closed_at` (migration 0084, once applied) plus an equivalent for `dealer_stock_entry`, or a generic `updated_at` + trigger — a schema decision for Simon, not something this pass made unilaterally (per the hard "never a schema/config change" rule).
|
||||
|
||||
Also found and corrected while building: `vehiq.vehicle`'s primary key column is `vehicle_id`, not `id` (verified via schema introspection only — no vehicle data rows were read to establish this).
|
||||
|
||||
## Where the code lives
|
||||
|
||||
- `src/connectors/postgres-watermark-connector.ts` — the P061 `Connector` implementing `WHERE watermark_col > $1 OR (watermark_col = $1 AND key_col > $2) ORDER BY watermark_col, key_col LIMIT $3` (the `OR` tuple form, not a bare `>`, so a same-timestamp tie at a page boundary is not silently dropped — see the file's own header for the bug this avoids). Explicit column allow-list, never `SELECT *`. `validate()` structurally refuses any config pointed at `vehiq.vehicle`.
|
||||
- `src/connectors/postgres-keyed-lookup-connector.ts` — the narrow vehicle-dimension connector. Config carries an already-resolved, finite `keys` array; `connect()` refuses an empty list rather than defaulting to a full read; every query is `WHERE vehicle_id = ANY($1)` chunked to `keys_per_query` (default 500).
|
||||
- `src/connectors/postgres-query-impl.ts` — the real `postgres` (postgres.js) implementation of the connectors' injectable `QueryImpl`, used only by the orchestration script. Refuses to execute anything whose first keyword isn't `SELECT`, independent of the DB role's own privileges.
|
||||
- `src/ingestion/projection-sink.ts` — adapts the P062 runner's `IngestionSink` onto `SiaxAnalyticsEngine.ingestProjection` (writes `opt_dataset_projection`). No ClickHouse import — only the `SiaxAnalyticsEngine` port type.
|
||||
- `src/ingestion/vehiq-oltp-ingest-runner.ts` — the orchestration: `previewVehiqOltpSource` (one bounded read-only read, never commits, safe against production at any time) and `runVehiqOltpIngestion` (the real P062-runner path).
|
||||
- `scripts/vehiq-oltp-ingest/run.ts` — thin CLI (`pnpm run vehiq:oltp-ingest`), env-driven, defaults to `preview` mode.
|
||||
- Tests: `tests/connectors/postgres-watermark-connector.test.ts`, `tests/connectors/postgres-keyed-lookup-connector.test.ts`, `tests/ingestion/vehiq-oltp-ingest-runner.test.ts`.
|
||||
|
||||
## Checkpoint field mapping
|
||||
|
||||
Simon's own specified checkpoint columns — `source, table, last_updated_at, last_id, batch_count, checksum, ingested_at` — mapped onto the **existing** P062 `IngestionCheckpoint` (reused, not duplicated, per the "no double truths" rule):
|
||||
|
||||
| Simon's field | Canonical field |
|
||||
|---|---|
|
||||
| `source` | `IngestionCheckpoint.source_id` |
|
||||
| `table` | `IngestionCheckpoint.partition_key` |
|
||||
| `last_updated_at` | decoded from `IngestionCheckpoint.cursor` (JSON `WatermarkCursor`) |
|
||||
| `last_id` | decoded from `IngestionCheckpoint.cursor` |
|
||||
| `checksum` | decoded from `IngestionCheckpoint.cursor` — sha256 of the page just landed |
|
||||
| `batch_count` | `IngestionCheckpoint.records_committed` (cumulative); a per-run page count is also in each run's `PartitionRunReport` |
|
||||
| `ingested_at` | `IngestionCheckpoint.committed_at` |
|
||||
|
||||
Durable storage: `createIngestionRepository(DATABASE_URL)` — Postgres-backed when `DATABASE_URL` (0pt's own metadata store) is set, in-memory only in non-production/tests. `run` mode refuses to start without `DATABASE_URL` explicitly, so a scheduled run can never silently fall back to re-reading everything from the beginning.
|
||||
|
||||
## Testing
|
||||
|
||||
Fixture-backed (no real infra): watermark + limit correctness, a same-timestamp tie at a page boundary, resume-from-checkpoint after a simulated crash (via a sink that throws mid-run, mirroring `tests/p0/ingestion-runner.test.ts`'s own technique), the empty-table case (zero rows, zero envelopes, no throw), and the narrow vehicle-dimension lookup asserted at the *query* level (every id ever bound into a query is a subset of the referenced set; a vehicle never referenced by the batch is never queried).
|
||||
|
||||
Also verified against a **real, disposable Postgres container** (not just fixtures), both seeded and truncated-to-empty, before touching production — caught and fixed two real bugs the fixture-only tests could not have caught:
|
||||
|
||||
1. `timestamptz` columns come back from the real driver as JS `Date` objects, not strings — the initial cursor-encoding logic did `String(date)` (locale-dependent, useless as a stored watermark) instead of `.toISOString()`. Fixed; regression test added (`tests/connectors/postgres-watermark-connector.test.ts`, "real Postgres driver value shapes").
|
||||
2. The same `Date`-vs-string mismatch tripped the P063 schema-conformance gate on every row (`field "listed_at" is object, expected string`) until the connector's `toRecord()` was made to normalize every `Date`-valued payload field to ISO before building the envelope.
|
||||
|
||||
**Verified read-only against actual production** (server10, `vehiq-db`, via `fleet`), 2026-09-25: the connector's exact SQL (`SELECT id, tenant_id, vehicle_id, lot_location, floorplan_status, listed_at, sold_at FROM app.dealer_stock_entry ORDER BY listed_at, id LIMIT 100` and the equivalent for `app.deal`) executed directly, read-only, and returned **zero rows, no error** — confirming both that the extractor's query shape is valid against the live schema and that there is genuinely nothing to extract yet. `vehiq.vehicle`'s `vehicle_id` primary-key column name was confirmed via `information_schema.columns` only — no vehicle data rows were read. **The compiled service itself was not run against production** — only its exact SQL, via read-only `psql`, matching this pass's "prove it does nothing, don't deploy a live service yet" scope.
|
||||
|
||||
## Deferred: full vehicle-dimension backfill
|
||||
|
||||
Not built or run in this pass, per the task's explicit instruction. If/when a full `dimension_vehicle` snapshot is ever wanted (as opposed to the permanent narrow per-batch lookup this extractor does forever), it needs its own deliberate decision:
|
||||
|
||||
- **Chunking**: page through `vehiq.vehicle` by primary key range (`vehicle_id > :last ORDER BY vehicle_id LIMIT :n`), never an unbounded scan — the same shape `PostgresWatermarkConnector` already implements, just pointed at a PK range instead of a timestamp (this connector's own `validate()` refuses `vehiq.vehicle` by name specifically to keep this a distinct, deliberate choice rather than an accidental default).
|
||||
- **Throttle**: an explicit delay between chunks, watching server10's `iostat`/`vmstat`/CPU while it runs — 98M rows / 85GB is enough to matter even read-only.
|
||||
- **Checksum + reconciliation**: a row-count and content checksum comparison between source and `opt_dataset_projection` after the backfill, recorded, before trusting it.
|
||||
- **Watermark handoff**: the backfill's end position must hand off cleanly to the narrow per-batch lookup this extractor already does, so nothing is double-counted or gapped at the boundary.
|
||||
|
||||
This mirrors the numbered runbook `admin/vehiq`'s `docs/CURRENT_STATE.md` already wrote for the general CDC case (not executed there either) — the shape is consistent, deliberately, rather than inventing a second convention.
|
||||
|
||||
## Deferred: `app.listing` extraction
|
||||
|
||||
Blocked on a schema decision, not an engineering gap: `app.listing` has no timestamp column today. Unblocking it needs one of:
|
||||
|
||||
1. Apply `admin/vehiq` migration `0084_listing_deal_lifecycle_timestamps.sql` (adds `published_at`/`unpublished_at`) — a decision for whoever owns that repo's migration pipeline, not this pass.
|
||||
2. Add a generic `updated_at` + trigger to `dealer_stock_entry`/`deal`/`listing` — closes both this gap and the "insert-only watermark misses in-place updates" caveat above in one move, but is a real schema change on production and needs Simon's explicit sign-off per the hard rule against touching production schema unsupervised.
|
||||
|
||||
Once either lands, adding a third `VehiqOltpSourceDef` entry to `VEHIQ_OLTP_SOURCES` (`src/ingestion/vehiq-oltp-ingest-runner.ts`) is the only code change needed — the connector, runner, sink and tests are all already generic across sources.
|
||||
|
||||
## What activating this in production actually requires
|
||||
|
||||
Nothing here is scheduled or running. To turn it on:
|
||||
|
||||
1. **A read-only Postgres role on `vehiq-db`** (server10) with `SELECT` on exactly `app.dealer_stock_entry` and `app.deal` (not the whole `app` schema, not `vehiq.vehicle` beyond what the narrow lookup needs) — creating this role is a production database change and should be coordinated explicitly, the same posture `admin/vehiq`'s own CDC runbook already takes for a bigger version of this same question. Its connection string becomes `VEHIQ_SOURCE_DATABASE_URL`.
|
||||
2. **`DATABASE_URL`** — 0pt's own metadata Postgres, for durable checkpoints. Already exists for every other 0pt capability; reuse it, do not provision a new one for this.
|
||||
3. **`OPT_ANALYTICS_CLICKHOUSE_BASE_URL` / `OPT_ANALYTICS_CLICKHOUSE_BASIC_AUTH`** — the already-deployed `opt-analytics-clickhouse` on server6 (internal-only, reachable from containers on the `coolify` network — see `admin/vehiq`'s `docs/CURRENT_STATE.md`, "0pt production ClickHouse 26.8 LTS"). A real least-privilege application user should be provisioned for this connector specifically, not the schema-admin `opt_admin` user mentioned there.
|
||||
4. **`VEHIQ_INGEST_MODE=run`** on the scheduled workflow (default is `preview`, which never writes).
|
||||
5. A decision on whether `.gitea/workflows/vehiq-oltp-ingest.yml`'s cron schedule should actually be enabled (it is present and correctly configured, but Gitea Actions secrets are not set on this repo by this pass — an unset `VEHIQ_SOURCE_DATABASE_URL` makes every scheduled run a clean no-op, so leaving the workflow file in place with no secrets configured is safe, but is not the same as "this is now extracting real data").
|
||||
|
||||
None of steps 1-3 were done in this pass — they require production credentials/role provisioning this pass does not have standing authority to create unsupervised.
|
||||
@@ -0,0 +1,60 @@
|
||||
# ADR-0001 — Server-renderad authz-resolution för orderåtgärder
|
||||
|
||||
**Status:** Accepted
|
||||
**Date:** 2026-09-29
|
||||
**Owners:** 0pt (simon@siax.io)
|
||||
**Related requirements:** DoD §21 (backend enforcement), §78 (error/why-UX), våg 3.6–3.7 (authz-ux/entitlement)
|
||||
**Related capabilities:** `@siax/contracts/authz-ux` 0.10.0, `@siax/ui` ActionGate 0.11.1
|
||||
|
||||
## Context
|
||||
Admin-ordkön behöver gate:a advance-aktionen. 0pt saknar klientsides-policy;
|
||||
`requireOrderOwner` är server-side. Att resolvé:a i klienten vore påhittad
|
||||
policy (DoD §15).
|
||||
|
||||
## Existing implementations
|
||||
- PATCH `/api/orders/{id}` enforcar ägarskap server-side (sanningsvärdet).
|
||||
- Admin-sidan hämtar ordrar via GET `/api/orders` (owner-scopad).
|
||||
|
||||
## Decision
|
||||
**Server-renderad resolution:** GET `/api/orders` returnerar per order ett
|
||||
frozen `advance_action` (ResolvedAction från `resolveAction`), derivat i
|
||||
`lib/order-flow.ts` ur kanoniskt flöde + sessionens ägarskap. Klienten
|
||||
renderar via `ActionGate` — ingen policylogik i UI-lagret. Ingen extra
|
||||
fetch: resolutionen rids med ordningssvaret.
|
||||
|
||||
## Ownership
|
||||
0pt äger flödet och resolutionen; DES1GN äger kontraktet (`authz-ux`) och
|
||||
renderingen (ActionGate). Product Firewall: 0pt importerar @siax/*, aldrig
|
||||
tvärtom.
|
||||
|
||||
## Contract
|
||||
`ResolvedAction {state, reason?}` — stängda uppsättningar enligt authz-ux.
|
||||
|
||||
## Provider boundary
|
||||
Framtida pay0-entitlement projiceras via `deriveEntitlement` in i samma
|
||||
resolve — ingen ny endpoint behövs.
|
||||
|
||||
## Dependency rules
|
||||
`@siax/ui`/`@siax/contracts` → 0pt. Aldrig reverse.
|
||||
|
||||
## Migration
|
||||
Enda konsument: admin-sidan (denna PR). Övriga ytor migreras per aktion.
|
||||
|
||||
## Alternatives considered
|
||||
- **Klientsides-resolution mot ny endpoint:** extra roundtrip, policy-läckage
|
||||
risk, mer kod — avvisad.
|
||||
- **Hårdkodad disabled i UI:** duplicerar serverns sanningsvärde — avvisad
|
||||
(DoD §21: backend är enforcement, UI är presentation).
|
||||
|
||||
## Risks
|
||||
Flödesändringar måste skriva i `lib/order-flow.ts` (enda källan) —
|
||||
lint-/test-täckning finns (tests/order-flow.test.ts).
|
||||
|
||||
## Testing / Evidence
|
||||
- `tests/order-flow.test.ts`: available för aktiva steg, read_only för
|
||||
terminala (incl. okända statusar i fri-text-DB) — 3/3.
|
||||
- Full svit 1138+3 godkända; typecheck grön.
|
||||
|
||||
## Rollback
|
||||
Ta bort `advance_action`-fältet + ActionGate-wrappen — klienten faller
|
||||
tillbaka till tidigare rendering (flödet oförändrat).
|
||||
@@ -0,0 +1,61 @@
|
||||
# ADR-0002 — Kanonisk orderstatus: kodnivå-union + guardad DB-constraint
|
||||
|
||||
**Status:** Accepted
|
||||
**Date:** 2026-09-29
|
||||
**Owners:** 0pt (simon@siax.io)
|
||||
**Related requirements:** C3 (kanonisera orderstatus), DoD §17 (backup-städ), §33 (migreringsordning), §35 (restore-drill)
|
||||
**Related artifacts:** `lib/order-flow.ts` (`ORDER_STATUSES`, `OrderStatus`, `isKnownOrderStatus`, `nextOrderStatus`), `sql/015_order_status_check.sql`
|
||||
|
||||
## Context
|
||||
|
||||
`orders.status` är fri text (`sql/001_init.sql` line ~92: `status text NOT NULL DEFAULT 'draft'`).
|
||||
Flödet draft→quoted→in_production→review→accepted→delivered är känt i kod, men
|
||||
DB:n nekar inga andra värden — fri text har redan orsakat minst en missvisande
|
||||
"klar"-visning i ordkön (okänd status renderades som "done/klar" utan att
|
||||
ordern faktiskt levererats).
|
||||
|
||||
**Prod-harvest ej körbar i session** (2026-09-29): ingen `DATABASE_URL`/`POSTGRES_URL`
|
||||
för 0pt finns i Infisical (varken default-env eller `/0pt`-sökvägar — körningen
|
||||
injicerade 246 secrets utan order-DB-URL). Statusfördelningen i prod är därför
|
||||
okänd här; migreringen är guardad och manuell.
|
||||
|
||||
## Existing implementations
|
||||
|
||||
- `lib/order-flow.ts` — NEXT_STATUS-flödet, `resolveAdvanceAction`, `buildOrderInboxItem`.
|
||||
- `app/admin/page.tsx` + `app/api/orders/route.ts` konsumerar flödet.
|
||||
- `tests/order-flow.test.ts` behandlar `cancelled` som terminal read_only-status.
|
||||
|
||||
## Decision
|
||||
|
||||
1. **Kodnivå-union är SSOT:** `type OrderStatus` = de sex flödesstatusarna +
|
||||
`cancelled` (används i tests som terminal status — därmed med, trots utebliven
|
||||
harvest). `ORDER_STATUSES` är den utömmande listan; `isKnownOrderStatus()`
|
||||
klassar fri text fail-closed. NEXT_STATUS/resolveAdvanceAction/
|
||||
buildOrderInboxItem konsumerar unionen via `nextOrderStatus()` — okänd
|
||||
status ger alltid read_only / ingen inbox-post, aldrig påhittat nästa steg.
|
||||
2. **DB guardas — inte typ-migreras:** `sql/015_order_status_check.sql` lägger
|
||||
`CHECK (status IN (...)) NOT VALID` (fas A, kort lås, nekar vid dirt-data via
|
||||
DO-preflight) och `VALIDATE CONSTRAINT` i separat session (fas B, SHARE UPDATE
|
||||
EXCLUSIVE + `lock_timeout='5s'`). KÖRS EJ mot prod i sessionen — Simon kör
|
||||
efter harvest-rapport och backup (`pg_dump -t orders` före fas A).
|
||||
|
||||
## Alternativ avvisade
|
||||
|
||||
- **Fortsatt fri text** — redan orsakat missvisande "klar"; ingen möjlighet att
|
||||
fela tidigt vid skrivning.
|
||||
- **Omedelbar NOT VALID utan harvest** — risk för prod-block: NOT VALID validerar
|
||||
inte befintliga rader, men VALIDATE-fasen (och varje ny UPDATE) krockar med
|
||||
okända statusar; utan känd statusfördelning är fas B en lotteri.
|
||||
- **Postgres `enum`-typ** — kräver typmigrering av kolumnen (ALTER TYPE-lås,
|
||||
ADD VALUE kan ej ske i transaktion i äldre PG), tyngre drift än en CHECK som
|
||||
kan utökas med en rad kod + en rullande migrering.
|
||||
|
||||
## Consequences
|
||||
|
||||
- Ny status = en rad i `ORDER_STATUSES`/`NEXT_STATUS` + en uppföljningsmigrering
|
||||
som lägger till värdet i CHECK:en (drop+add NOT VALID, validate igen).
|
||||
- Rader med okänd status (om harvest hittar sådana) mappas i en EGEN, granskad
|
||||
migrering före fas A — preflighten nekar annars körning (fail-closed).
|
||||
- SQL-listan och kodlistan är dubbelbokning; testen i
|
||||
`tests/order-flow.test.ts` låser att alla `ORDER_STATUSES` har definierat
|
||||
beteende (nästa steg eller read_only).
|
||||
@@ -27,15 +27,15 @@
|
||||
|
||||
---
|
||||
|
||||
## B-2 🔴 — Ingen Coolify-app provisionerad, inget Infisical-projekt
|
||||
## B-2 ✅ LÖST 2026-09-30 — Coolify-app provisionerad, 0pt.app kanonisk prod
|
||||
|
||||
| Fält | Innehåll |
|
||||
|---|---|
|
||||
| **Vad blockerar** | `.gitea/workflows/cd.yml` har fortfarande en literal platshållare: `COOLIFY_APP_ID: "TODO-no-coolify-app-provisioned-yet"`. Jobbet är medvetet guardat att lyckas grönt utan att faktiskt deploya (`exit 0` + varning om token/UUID saknas). `siax.repo.json`/`siax.config.json` saknar Infisical-referens. Detta matchar repots egna `docs/compliance/BACKLOG.md` (P1-5, P1-6) och `GOLDEN-STANDARD-REPORT.md` (D4 ❌, C8 ❌, D7 ➖). |
|
||||
| **Vad krävs för att lyfta den** | Skapa appen i `cloud.siax.io` (Coolify), sätt `COOLIFY_APP_ID` i `cd.yml` och `COOLIFY_TOKEN` som Gitea-repo-secret. Skapa ett Infisical-projekt `0pt` med env-mappning och migrera bort från Vercel-env i takt med cutover. |
|
||||
| **Vem beslutar** | Ingen produktbeslutsfråga i sig — operativt arbete som kräver admin-access till kontrollplanet (`cloud.siax.io` / `vault.siax.io`). |
|
||||
| **Nuvarande status** | Fortfarande öppen. Verifierad färsk denna session: `cd.yml` bär fortfarande TODO-platshållaren på nuvarande `main`-HEAD. Kontrollerat om ett parallellt P0-1-uppdrag i samma arbetsflöde redan hunnit provisionera en app/projekt — inget sådant syns i repots egna deploy-artefakter vid tidpunkten för denna verifiering. Denna session saknar `SIAX_TOKEN`/inloggning mot `cloud.siax.io`/Infisical, så ett levande skick bortom repots egna signaler kunde inte kontrolleras direkt — endast repo-interna bevis nedan är verifierade. |
|
||||
| **Bevis** | `.gitea/workflows/cd.yml` rad 25 (denna checkout); `docs/compliance/BACKLOG.md` P1-5/P1-6; `docs/compliance/GOLDEN-STANDARD-REPORT.md` D4/D7/C8. |
|
||||
| **Vad blockerar** | *(Inget — löst.)* Tidigare: `cd.yml` saknade provisionerad Coolify-app (BACKLOG P1-5). Den rapporten var **falsk blocker**: den testade `0pt.siax.io` (fel domän) — 0pt.app är och var den kanoniska prod-ytan (`siax.config.json`: `"domain": "0pt.app"`). |
|
||||
| **Hur den löstes** | Coolify-app provisionerad + deployad via `cd.yml` (`COOLIFY_APP_ID` via repo-vars). Live-verifierat 2026-09-30: `GET https://0pt.app/api/health` → 200 (`db: ok`), `GET /admin` → 200, `GET /api/orders` → 401 fail-closed (`OPT_UNAUTHENTICATED`). main `e7d5ee3` är deployad. |
|
||||
| **Vem beslutar** | — |
|
||||
| **Nuvarande status** | **LÖST 2026-09-30.** 0pt.app är kanonisk prod-yta (verifierad). Eventuellt `0pt.siax.io`-alias är en separat DNS-fråga (ej blockerande; domänen svarar i dagsläget ej). |
|
||||
| **Bevis** | Live-prober 2026-09-30 (health 200 db:ok, /admin 200, /api/orders 401); `siax.config.json` domain `0pt.app`; `.gitea/workflows/cd.yml` deploy-väg; main `e7d5ee3` (PR #134). |
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -1,27 +1,74 @@
|
||||
# EVIDENCE — FAS 8 · /api/health + dependencies
|
||||
|
||||
> Live-prober + källanalys 2026-08-10 (VERIFIERAT).
|
||||
> Live-prober + källanalys **2026-09-30** (VERIFIERAT mot prod). Ersätter den
|
||||
> gamla Vercel-mätningen (2026-08-10) — 0pt.app är inte längre på Vercel.
|
||||
|
||||
## Live (0pt.app, prod-instansen — Vercel)
|
||||
## Prod-topologi (Coolify, inte Vercel)
|
||||
|
||||
`0pt.app` = kanonisk prod via **Coolify på cloud.siax.io**, deployad av
|
||||
`.gitea/workflows/cd.yml` vid varje merge till `main` (repo-secret
|
||||
`COOLIFY_TOKEN`, repo-variable `COOLIFY_APP_ID` — riktig UUID, guarden
|
||||
passerar; se `docs/architecture/decisions/0001-deploy-target-coolify-not-vercel.md`).
|
||||
Inga Vercel-referenser är längre aktuella för prod.
|
||||
|
||||
## Live (0pt.app, prod — mätt 2026-09-30 08:0x UTC)
|
||||
|
||||
```
|
||||
GET https://0pt.app/api/health -> 200
|
||||
{"ok":true,"service":"0pt-web","db":"not_configured","queue":"not_configured","ts":"2026-08-10T01:58:31.043Z"}
|
||||
GET https://0pt.app/api/0pt -> 200 (anonymt) [lucka S1 bekräftad live]
|
||||
GET https://0pt.app/api/orders -> 200 (anonymt) [lucka S3 bekräftad live]
|
||||
GET https://0pt.app/api/health -> 200
|
||||
{"ok":false,"service":"0pt-web","db":"ok","queue":"not_configured",
|
||||
"engine":"error","stripe":"not_configured",
|
||||
"detail":{"engine":"This operation was aborted"},
|
||||
"ts":"2026-09-30T08:05:32.935Z"}
|
||||
|
||||
GET https://0pt.app/admin -> 200
|
||||
GET https://0pt.app/api/orders -> 401 fail-closed (OPT_UNAUTHENTICATED)
|
||||
```
|
||||
|
||||
## Källkod (app/api/health/route.ts)
|
||||
Hälsokontraktet ljuger inte: `ok:false` speglar en verkligt degraderad
|
||||
beroendekoll (`engine:error`), db är på riktigt `ok` och authen fail-close:ar.
|
||||
Vad som återstår är att åtgärda orsaken, inte att måla grönt (se nedan).
|
||||
|
||||
Health-svar innehåller `db` + `queue`-fält som reflekterar `DATABASE_URL` /
|
||||
`REDIS_URL`-närvaro — beroendestatus rapporteras, men `ok` är alltid `true`
|
||||
(liveness, ej readiness). Gap D2: en instans utan databas är "grön".
|
||||
## Engine-kollens rotorsak — konfig, inte kod
|
||||
|
||||
## Bygge (next build, lokal — samma som CI)
|
||||
- `lib/health-checks.ts#checkEngine` gör en riktig HTTP-roundtrip mot
|
||||
`${ENGINE_URL}/health` (AbortController, 2 s timeout). Kontraktet:
|
||||
`not_configured` när `ENGINE_URL` ej är satt; `error` när den ÄR satt men
|
||||
misslyckas (t.ex. abort/non-2xx); `ok` först vid lyckad roundtrip.
|
||||
- Prod-symptomet `"This operation was aborted"` = timeout-aborten mot en
|
||||
**oprovisionerad host**: `ENGINE_URL` pekar på `http://0pt-engine:8000`,
|
||||
ett namn utan DNS-post i flottan (mätt 2026-09-25 och åter 09-30; ingen
|
||||
sådan container på server5–11).
|
||||
- **Infisical /0pt (prod): 0 nycklar matcher `ENGINE_URL`** (räknat
|
||||
2026-09-30 via `siax-secrets export --env prod --path /0pt`). Värdet sitter
|
||||
alltså i **Coolify-appens egen env**, inte i valvet.
|
||||
- Beslut per konvention: kollen är redan ärlig och icke-blockerande när
|
||||
motorn ej provisionerats — `ENGINE_URL` borttaget ⇒ `engine:"not_configured"`
|
||||
och `ok:true`, utan kodändring. Sålänge URL:en är satt men död är
|
||||
`error` den korrekta, ärliga statusen. Regressionstest finns i
|
||||
`tests/lib/health-checks.test.ts` (unset → not_configured; satt+abort →
|
||||
error med abort-meddelandet i `detail`).
|
||||
- Åtgärd (konfig, Simon-gate): ta bort `ENGINE_URL` (och ev. oanvänd
|
||||
`INTERNAL_TOKEN` för motorn) från Coolify-appens env → omdeploy. Tills
|
||||
FastAPI-motorn (`services/api`) faktiskt provisioneras ska ingen
|
||||
`ENGINE_URL` finnas i prod.
|
||||
|
||||
```
|
||||
ƒ Proxy (Middleware) <- Next 16 bygger middleware.ts korrekt i detta träd
|
||||
```
|
||||
→ S1-hypotesen "middlewaren plockas inte upp av Next 16" försvagas; mer
|
||||
sannolik orsak är Vercel-projektets build-output eller deploymentsversion
|
||||
(ej verifierat). Se BACKLOG P0-1.
|
||||
## Källkod (app/api/health/route.ts + lib/health-checks.ts)
|
||||
|
||||
- Routen är **liveness**: alltid 200 så länge processen svarar. Bodyns `ok`
|
||||
är den ärliga signalen: `false` när en konfigurerad dependency fail:ar.
|
||||
- Per-dependency: `ok` | `error` (med `detail`) | `not_configured` |
|
||||
`not_integrated` (endast `queue`/REDIS_URL — ingen Redis-klient wired i
|
||||
repot; aldrig fabricerad `ok`).
|
||||
- Readiness (`/api/ready`) fail-close:ar på `db != ok` och på varje
|
||||
`error` — konfigurerad-men-död räknas inte som "valfri".
|
||||
|
||||
## Simon-gates (ej agent-verifierbara)
|
||||
|
||||
Inloggad journey (admin-portalens E2E) och kanarie-identitet (beställd
|
||||
kanarieorder spårad end-to-end) kräver Simons inloggning och stäms separat —
|
||||
omfattas ej av denna mätning.
|
||||
|
||||
## Historik
|
||||
|
||||
2026-08-10 mättes 0pt.app på Vercel (`{"ok":true,"db":"not_configured",...}`
|
||||
hardcoded-grönt — BACKLOG P0-3, sedan fixat). Den deploymenten är inaktuell; alla prod-utsagor i denna fil gäller Coolify-cutovern.
|
||||
|
||||
+25
-4
@@ -165,10 +165,31 @@ export function hasDegradedDependency(deps: DependencyResults): boolean {
|
||||
|
||||
/**
|
||||
* Readiness (unlike liveness) treats "not configured" for the deps this app
|
||||
* cannot serve real traffic without as not-ready too. Only `db` is load-bearing
|
||||
* today -- engine/stripe/queue are optional integrations the app is designed
|
||||
* to run without (see .env.example).
|
||||
* cannot serve real traffic without as not-ready too. `db` is the only
|
||||
* dependency that is load-bearing merely by being absent -- engine/stripe/
|
||||
* queue are optional integrations the app is designed to run WITHOUT (see
|
||||
* .env.example), so "not_configured" for those stays ready.
|
||||
*
|
||||
* But "optional to run without" is not the same as "optional to run BROKEN".
|
||||
* A dependency in the "error" state was deliberately configured by an
|
||||
* operator AND then failed a live round trip -- somebody asked for it and it
|
||||
* is not answering. Readiness fails closed on that too.
|
||||
*
|
||||
* Measured 2026-09-25 on the production deployment (0pt.app / api.0pt.app),
|
||||
* which is exactly the hole this closes:
|
||||
*
|
||||
* GET /api/health -> HTTP 200 {"ok":false, ..., "engine":"error",
|
||||
* "detail":{"engine":"This operation was aborted"}}
|
||||
* GET /api/ready -> HTTP 200 {"ready":true, "db":"ok", "engine":"error"}
|
||||
*
|
||||
* ENGINE_URL pointed at `http://0pt-engine:8000`, a hostname with no DNS
|
||||
* record anywhere on the fleet (`getent hosts 0pt-engine` -> nothing; no such
|
||||
* container on server5-11). The data engine -- 0pt's whole product -- had
|
||||
* been absent for the container's entire 17.9 h uptime, and every signal
|
||||
* above it was green. `ok:false` in a 200 body is not a signal; nothing reads
|
||||
* bodies.
|
||||
*/
|
||||
export function isReady(deps: DependencyResults): boolean {
|
||||
return deps.db.status === "ok";
|
||||
if (deps.db.status !== "ok") return false;
|
||||
return !Object.values(deps).some((d) => d.status === "error");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
// DES1GN ADR D3 (§31): bulk-import dispatch-policy — ETT objekt, en sanning.
|
||||
//
|
||||
// Import-routen (/api/orders/import) är inline-exekvering med hårt tak. Rader
|
||||
// över maxRows är DES1GN D3:s signal för N0D-dispatch (job_type
|
||||
// 0pt.import_execute_job / capability 0pt.import_execute i n0d:s
|
||||
// JOB_TYPE_REGISTRY). TILLS descriptor-gaten släpper (N0D-worker
|
||||
// UNVERIFIED) svarar routen 413 med n0d_dispatch_required + hint — aldrig
|
||||
// tyst trunkering, aldrig tyst dispatch.
|
||||
export const IMPORT_DISPATCH_POLICY = {
|
||||
/** Inline-tak i import-routen (oförändrat beteendekontrakt, 0pt#53). */
|
||||
maxInlineRows: 500,
|
||||
/** N0D-dispatch-signal: rader över detta är ett N0D-jobb, inget HTTP-anrop. */
|
||||
maxRows: 5000,
|
||||
/** Byte-tak för hela CSV-kroppen (UTF-8). */
|
||||
maxBytes: 1_000_000,
|
||||
/** Format-allowlist — endast RFC 4180 CSV i dag. */
|
||||
allowedFormats: ["csv"] as readonly string[],
|
||||
} as const;
|
||||
|
||||
export type ImportDispatchPolicy = typeof IMPORT_DISPATCH_POLICY;
|
||||
|
||||
export type ImportDispatchVerdict =
|
||||
| { verdict: "inline" }
|
||||
| { verdict: "unsupported_format"; format: string }
|
||||
| { verdict: "byte_cap_exceeded"; bytes: number; maxBytes: number }
|
||||
| { verdict: "row_cap_exceeded"; rows: number; maxInlineRows: number }
|
||||
| { verdict: "dispatch_n0d"; rows: number; maxRows: number };
|
||||
|
||||
/** Ren funktion — inga I/O, fullt deterministisk, testad per kombination. */
|
||||
export function evaluateImportDispatch(
|
||||
input: { format: string; bytes: number; rows: number },
|
||||
policy: ImportDispatchPolicy = IMPORT_DISPATCH_POLICY,
|
||||
): ImportDispatchVerdict {
|
||||
if (!policy.allowedFormats.includes(input.format)) {
|
||||
return { verdict: "unsupported_format", format: input.format };
|
||||
}
|
||||
if (input.bytes > policy.maxBytes) {
|
||||
return { verdict: "byte_cap_exceeded", bytes: input.bytes, maxBytes: policy.maxBytes };
|
||||
}
|
||||
if (input.rows > policy.maxRows) {
|
||||
return { verdict: "dispatch_n0d", rows: input.rows, maxRows: policy.maxRows };
|
||||
}
|
||||
if (input.rows > policy.maxInlineRows) {
|
||||
return { verdict: "row_cap_exceeded", rows: input.rows, maxInlineRows: policy.maxInlineRows };
|
||||
}
|
||||
return { verdict: "inline" };
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
import { createInboxItem, type InboxItem } from "@siax/contracts/inbox";
|
||||
import { resolveAction, type ResolvedAction } from "@siax/contracts/authz-ux";
|
||||
|
||||
/**
|
||||
* Kanonisk orderstatus (ADR-0002, C3 2026-09-29): kodnivå-union som är den
|
||||
* ENDA sanningskällan för vilka statusar som existerar. DB-kolumnen är fortfarande
|
||||
* `text` (sql/001_init.sql line ~92) men guardas av sql/015_order_status_check.sql
|
||||
* (CHECK ... NOT VALID + VALIDATE i två steg, körs EJ automatiskt mot prod).
|
||||
* 'cancelled' ingår: tests/order-flow.test.ts behandlar den som terminal status.
|
||||
*/
|
||||
export type OrderStatus =
|
||||
| "draft"
|
||||
| "quoted"
|
||||
| "in_production"
|
||||
| "review"
|
||||
| "accepted"
|
||||
| "delivered"
|
||||
| "cancelled";
|
||||
|
||||
/** Uttömmande, kanonisk lista — SQL-constrainten i sql/015 MÅSTE spegla denna. */
|
||||
export const ORDER_STATUSES = [
|
||||
"draft",
|
||||
"quoted",
|
||||
"in_production",
|
||||
"review",
|
||||
"accepted",
|
||||
"delivered",
|
||||
"cancelled",
|
||||
] as const satisfies readonly OrderStatus[];
|
||||
|
||||
/** Typ-vakt: fri text ur DB/payload klassas mot unionen (fail-closed mot read_only). */
|
||||
export function isKnownOrderStatus(status: string): status is OrderStatus {
|
||||
return (ORDER_STATUSES as readonly string[]).includes(status);
|
||||
}
|
||||
|
||||
/** Statusar som kan flyttas fram; delivered/cancelled är terminala. */
|
||||
type AdvanceableStatus = Exclude<OrderStatus, "delivered" | "cancelled">;
|
||||
|
||||
/** Kanoniskt flöde — servern äger resolutionen (ADR-0001), UI renderar. */
|
||||
export const NEXT_STATUS: Record<AdvanceableStatus, OrderStatus> = {
|
||||
draft: "quoted",
|
||||
quoted: "in_production",
|
||||
in_production: "review",
|
||||
review: "accepted",
|
||||
accepted: "delivered",
|
||||
};
|
||||
|
||||
/** Guardad nästa-status: okänd/terminal status → null (aldrig påhittat steg). */
|
||||
export function nextOrderStatus(status: string): OrderStatus | null {
|
||||
if (!isKnownOrderStatus(status)) return null;
|
||||
return NEXT_STATUS[status as AdvanceableStatus] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Server-renderad action-resolution för advance-steget (ADR-0001):
|
||||
* ägarskapenforces redan av requireOrderOwner på GET/PATCH; tillståndet
|
||||
* avgör read_only. Innan Session/roller finns i 0pt är permission alltid
|
||||
* granted för autentiserade ägare — hårdkoda aldrig denna resolve i klienten.
|
||||
* Okänd status (fri text ur DB innan sql/015-valideringen körts) → read_only.
|
||||
*/
|
||||
export function resolveAdvanceAction(status: string): ResolvedAction {
|
||||
if (nextOrderStatus(status)) {
|
||||
return resolveAction({ permission: "granted", entitlement: "active", approval: "not_required", readOnly: false });
|
||||
}
|
||||
return resolveAction({ permission: "granted", entitlement: "active", approval: "not_required", readOnly: true });
|
||||
}
|
||||
|
||||
/** Orderstatus som utgör en inkorgspost (våg 3.3: orders kö = inbox). */
|
||||
const INBOX_ELIGIBLE: Partial<Record<OrderStatus, { kind: "task" | "approval"; priority: "normal" | "high" }>> = {
|
||||
draft: { kind: "task", priority: "normal" },
|
||||
quoted: { kind: "task", priority: "normal" },
|
||||
in_production: { kind: "task", priority: "normal" },
|
||||
review: { kind: "approval", priority: "high" },
|
||||
};
|
||||
|
||||
/**
|
||||
* Server-renderad inkorgspost (ADR-0001-mönstret): en order som väntar på
|
||||
* nästa steg projiceras på universal-inbox-konvolverna. Terminala statusar
|
||||
* (delivered/cancelled) och okända statusar ger ingen post — inboxen är
|
||||
* öppna arbetsobjekt.
|
||||
*/
|
||||
export function buildOrderInboxItem(order: {
|
||||
order_id: string;
|
||||
status: string;
|
||||
flow: string;
|
||||
total_ore?: number | null;
|
||||
}): InboxItem | null {
|
||||
const next = nextOrderStatus(order.status);
|
||||
const eligible = isKnownOrderStatus(order.status) ? INBOX_ELIGIBLE[order.status] : undefined;
|
||||
if (!eligible || !next) return null;
|
||||
return createInboxItem({
|
||||
id: `order:${order.order_id}`,
|
||||
kind: eligible.kind,
|
||||
tenantId: "0pt",
|
||||
subject: `Order ${order.order_id.slice(0, 8)} — ${order.status} → ${next}`,
|
||||
priority: eligible.priority,
|
||||
createdAt: new Date().toISOString(),
|
||||
source: { system: "0pt", channel: "order-queue" },
|
||||
entity: { type: "order", id: order.order_id },
|
||||
actions: [{ id: "advance", label: `→ ${next}` }],
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
// Klient-adapter: kernel-transport (@siax/collaboration CollaborationTransport)
|
||||
// över 0pts host-API (/api/admin/order-notes). Admin-ytan pratar ALDRIG med
|
||||
// fetch direkt — allt går genom denna adapter, så offline-läge och
|
||||
// versionskonflikter hanteras på ETT ställe:
|
||||
// - offline: send() är ett medvetet NO-OP som kastar OfflineError — utkastet
|
||||
// behålls i UI:t och användaren får en varning. Ingen tyst förlust.
|
||||
// - 409: ConflictError med serverns aktuella version -> UI laddar om.
|
||||
// Presence går via kernelns upsertPresence (status offline/online per författare).
|
||||
import {
|
||||
nextLamportClock,
|
||||
upsertPresence,
|
||||
type CollaborationOperation,
|
||||
type CollaborationTransport,
|
||||
type PresenceState,
|
||||
} from "@siax/collaboration";
|
||||
|
||||
export interface OrderNoteDto {
|
||||
note_id: string;
|
||||
order_id: string;
|
||||
author_id: string;
|
||||
body: string;
|
||||
version: number;
|
||||
clock: number;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
export class OfflineError extends Error {
|
||||
constructor() {
|
||||
super("Offline: note change was NOT sent — draft kept locally, nothing lost");
|
||||
this.name = "OfflineError";
|
||||
}
|
||||
}
|
||||
|
||||
export class ConflictError extends Error {
|
||||
constructor(
|
||||
public readonly noteId: string,
|
||||
public readonly currentVersion: number,
|
||||
) {
|
||||
super(`Note ${noteId} changed server-side (current version ${currentVersion})`);
|
||||
this.name = "ConflictError";
|
||||
}
|
||||
}
|
||||
|
||||
export class OrderNotesTransport implements CollaborationTransport<{ body: string }> {
|
||||
private listeners = new Set<(op: CollaborationOperation<{ body: string }>) => void>();
|
||||
private presenceListeners = new Set<(p: PresenceState) => void>();
|
||||
private presence = new Map<string, PresenceState>();
|
||||
private localClock = 0;
|
||||
private offline = false;
|
||||
|
||||
constructor(readonly actorId: string) {}
|
||||
|
||||
setOffline(offline: boolean): void {
|
||||
this.offline = offline;
|
||||
this.setPresence({ participantId: this.actorId, status: offline ? "offline" : "online" });
|
||||
}
|
||||
|
||||
isOffline(): boolean {
|
||||
return this.offline;
|
||||
}
|
||||
|
||||
getPresence(): PresenceState[] {
|
||||
return [...this.presence.values()];
|
||||
}
|
||||
|
||||
setPresence(presence: PresenceState): void {
|
||||
this.presence = upsertPresence(this.presence, presence);
|
||||
for (const l of this.presenceListeners) l(presence);
|
||||
}
|
||||
|
||||
async connect(): Promise<void> {
|
||||
this.setOffline(false);
|
||||
}
|
||||
|
||||
async disconnect(): Promise<void> {
|
||||
this.setOffline(true);
|
||||
}
|
||||
|
||||
onOperation(listener: (op: CollaborationOperation<{ body: string }>) => void): () => void {
|
||||
this.listeners.add(listener);
|
||||
return () => this.listeners.delete(listener);
|
||||
}
|
||||
|
||||
onPresence(listener: (p: PresenceState) => void): () => void {
|
||||
this.presenceListeners.add(listener);
|
||||
return () => this.presenceListeners.delete(listener);
|
||||
}
|
||||
|
||||
/** Replay a server op into local listeners + advance the Lamport clock. */
|
||||
observeRemote(operation: CollaborationOperation<{ body: string }>): void {
|
||||
this.localClock = nextLamportClock(this.localClock, operation.clock);
|
||||
this.setPresence({
|
||||
participantId: operation.actorId,
|
||||
status: this.presence.get(operation.actorId)?.status ?? "online",
|
||||
});
|
||||
for (const l of this.listeners) l(operation);
|
||||
}
|
||||
|
||||
async send(operation: CollaborationOperation<{ body: string }>): Promise<void> {
|
||||
if (this.offline) throw new OfflineError();
|
||||
this.localClock = nextLamportClock(this.localClock, operation.clock);
|
||||
for (const l of this.listeners) l(operation);
|
||||
}
|
||||
}
|
||||
|
||||
export interface NotesApiResult {
|
||||
notes: OrderNoteDto[];
|
||||
/** Version per note — the optimistic-concurrency handle the UI PATCHes with. */
|
||||
versions: Map<string, number>;
|
||||
}
|
||||
|
||||
export class OrderNotesClient {
|
||||
readonly transport: OrderNotesTransport;
|
||||
|
||||
constructor(actorId: string) {
|
||||
this.transport = new OrderNotesTransport(actorId);
|
||||
}
|
||||
|
||||
async load(orderId: string): Promise<NotesApiResult> {
|
||||
const res = await fetch(`/api/admin/order-notes?order_id=${encodeURIComponent(orderId)}`, {
|
||||
cache: "no-store",
|
||||
});
|
||||
if (!res.ok) throw new Error(`load failed: ${res.status}`);
|
||||
const body = (await res.json()) as { notes: OrderNoteDto[] };
|
||||
const versions = new Map<string, number>();
|
||||
for (const note of body.notes) {
|
||||
versions.set(note.note_id, note.version);
|
||||
this.transport.observeRemote({
|
||||
id: note.note_id,
|
||||
actorId: note.author_id,
|
||||
clock: note.clock,
|
||||
type: "note.upsert",
|
||||
payload: { body: note.body },
|
||||
});
|
||||
}
|
||||
return { notes: body.notes, versions };
|
||||
}
|
||||
|
||||
async add(orderId: string, body: string): Promise<OrderNoteDto> {
|
||||
const op: CollaborationOperation<{ body: string }> = {
|
||||
id: `local_${crypto.randomUUID()}`,
|
||||
actorId: this.transport.actorId,
|
||||
clock: 0,
|
||||
type: "note.create",
|
||||
payload: { body },
|
||||
};
|
||||
await this.transport.send(op);
|
||||
const res = await fetch("/api/admin/order-notes", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ order_id: orderId, body }),
|
||||
});
|
||||
if (!res.ok) throw new Error(`add failed: ${res.status}`);
|
||||
return (await res.json()) as OrderNoteDto;
|
||||
}
|
||||
|
||||
async update(note: OrderNoteDto, body: string, expectedVersion: number): Promise<OrderNoteDto> {
|
||||
await this.transport.send({
|
||||
id: note.note_id,
|
||||
actorId: "self",
|
||||
clock: note.clock,
|
||||
type: "note.update",
|
||||
payload: { body },
|
||||
});
|
||||
const res = await fetch("/api/admin/order-notes", {
|
||||
method: "PATCH",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ note_id: note.note_id, body, version: expectedVersion }),
|
||||
});
|
||||
if (res.status === 409) {
|
||||
const err = (await res.json()) as { current_version?: number };
|
||||
throw new ConflictError(note.note_id, err.current_version ?? 0);
|
||||
}
|
||||
if (!res.ok) throw new Error(`update failed: ${res.status}`);
|
||||
return (await res.json()) as OrderNoteDto;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
// Order notes — SIAX collaboration-kernelns första produktkonsument (spår C).
|
||||
//
|
||||
// Server-authoritative store över 0pts data-lager (Postgres när DATABASE_URL
|
||||
// är satt, annars samma dokumenterade in-memory-fallback som orders-store).
|
||||
// Kernel-semantik (@siax/collaboration 0.3.0):
|
||||
// - varje note ÄR en CollaborationOperation (id/actorId/clock/type/payload)
|
||||
// - nya operationer får sin klocka via nextLamportClock (max lokal/observerad + 1)
|
||||
// - listning återställs deterministiskt via mergeOperationLog/compareOperations
|
||||
// (klocka -> actorId -> id), så två repliker listar i SAMMA ordning
|
||||
// - samtidighet: optimistisk version; stale skrivning kastar
|
||||
// NoteVersionConflictError -> routen svarar 409 (conflict behavior).
|
||||
import {
|
||||
mergeOperationLog,
|
||||
nextLamportClock,
|
||||
type CollaborationOperation,
|
||||
} from "@siax/collaboration";
|
||||
|
||||
import { getSqlForDurableStore } from "@/lib/db";
|
||||
|
||||
export interface OrderNote {
|
||||
note_id: string;
|
||||
order_id: string;
|
||||
tenant_id: string | null;
|
||||
author_id: string;
|
||||
body: string;
|
||||
version: number;
|
||||
clock: number;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
export class NoteVersionConflictError extends Error {
|
||||
constructor(
|
||||
public readonly noteId: string,
|
||||
public readonly expectedVersion: number,
|
||||
public readonly currentVersion: number,
|
||||
) {
|
||||
super(
|
||||
`Note "${noteId}" was updated by someone else: expected version ${expectedVersion}, current is ${currentVersion}`,
|
||||
);
|
||||
this.name = "NoteVersionConflictError";
|
||||
}
|
||||
}
|
||||
|
||||
export class NoteNotFoundError extends Error {
|
||||
constructor(public readonly noteId: string) {
|
||||
super(`Note "${noteId}" does not exist`);
|
||||
this.name = "NoteNotFoundError";
|
||||
}
|
||||
}
|
||||
|
||||
export class OrderNotFoundError extends Error {
|
||||
constructor(public readonly orderId: string) {
|
||||
super(`Order "${orderId}" does not exist`);
|
||||
this.name = "OrderNotFoundError";
|
||||
}
|
||||
}
|
||||
|
||||
// Non-durable fallback used only until a database is provisioned (same
|
||||
// decision as lib/orders-store.ts — resolveStoreMode fails hard in production
|
||||
// without a URL, so this path cannot silently swallow production writes).
|
||||
const mem = new Map<string, OrderNote>();
|
||||
|
||||
/** Kernel-projection: a note row IS a collaboration operation. */
|
||||
function toOperation(note: OrderNote): CollaborationOperation<{ body: string }> {
|
||||
return {
|
||||
id: note.note_id,
|
||||
actorId: note.author_id,
|
||||
clock: note.clock,
|
||||
type: "note.upsert",
|
||||
payload: { body: note.body },
|
||||
};
|
||||
}
|
||||
|
||||
/** Deterministic snapshot: kernel merge wins, never insertion order. */
|
||||
function snapshot(notes: Iterable<OrderNote>): OrderNote[] {
|
||||
const log = mergeOperationLog([...notes].map(toOperation));
|
||||
const byId = new Map([...notes].map((n) => [n.note_id, n]));
|
||||
return log
|
||||
.map((op) => byId.get(op.id))
|
||||
.filter((n): n is OrderNote => Boolean(n));
|
||||
}
|
||||
|
||||
function rowToNote(row: Record<string, unknown>): OrderNote {
|
||||
return {
|
||||
note_id: String(row.note_id),
|
||||
order_id: String(row.order_id),
|
||||
tenant_id: row.tenant_id === null || row.tenant_id === undefined ? null : String(row.tenant_id),
|
||||
author_id: String(row.author_id),
|
||||
body: String(row.body),
|
||||
version: Number(row.version),
|
||||
clock: Number(row.clock),
|
||||
created_at: new Date(row.created_at as string).toISOString(),
|
||||
updated_at: new Date(row.updated_at as string).toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
type NotesSql = NonNullable<ReturnType<typeof getSqlForDurableStore>>;
|
||||
|
||||
async function maxClock(sql: NotesSql, orderId: string): Promise<number> {
|
||||
const rows = await sql`
|
||||
SELECT COALESCE(MAX(clock), 0) AS max_clock FROM order_notes WHERE order_id = ${orderId}
|
||||
`;
|
||||
return Number(rows[0]?.max_clock ?? 0);
|
||||
}
|
||||
|
||||
export async function listNotes(orderId: string): Promise<OrderNote[]> {
|
||||
const sql = getSqlForDurableStore("order-notes");
|
||||
if (!sql) {
|
||||
return snapshot(
|
||||
[...mem.values()].filter((n) => n.order_id === orderId),
|
||||
);
|
||||
}
|
||||
const rows = await sql`
|
||||
SELECT * FROM order_notes WHERE order_id = ${orderId}
|
||||
`;
|
||||
return snapshot(rows.map(rowToNote));
|
||||
}
|
||||
|
||||
export interface AddNoteInput {
|
||||
order_id: string;
|
||||
tenant_id: string | null;
|
||||
author_id: string;
|
||||
body: string;
|
||||
}
|
||||
|
||||
export async function addNote(input: AddNoteInput): Promise<OrderNote> {
|
||||
const body = input.body.trim();
|
||||
if (!body) throw new TypeError("note body must be non-empty");
|
||||
const note: OrderNote = {
|
||||
note_id: `note_${crypto.randomUUID()}`,
|
||||
order_id: input.order_id,
|
||||
tenant_id: input.tenant_id,
|
||||
author_id: input.author_id,
|
||||
body,
|
||||
version: 1,
|
||||
// Kernel: Lamport-klockan = max(lokal, observerad) + 1.
|
||||
clock: 1,
|
||||
created_at: new Date().toISOString(),
|
||||
updated_at: new Date().toISOString(),
|
||||
};
|
||||
note.clock = nextLamportClock(await peekMaxClock(input.order_id));
|
||||
|
||||
const sql = getSqlForDurableStore("order-notes");
|
||||
if (!sql) {
|
||||
mem.set(note.note_id, note);
|
||||
return note;
|
||||
}
|
||||
const rows = await sql`
|
||||
INSERT INTO order_notes (note_id, order_id, tenant_id, author_id, body, version, clock, created_at, updated_at)
|
||||
VALUES (${note.note_id}, ${note.order_id}, ${note.tenant_id}, ${note.author_id}, ${note.body},
|
||||
${note.version}, ${note.clock}, ${note.created_at}, ${note.updated_at})
|
||||
RETURNING *
|
||||
`;
|
||||
return rowToNote(rows[0]);
|
||||
}
|
||||
|
||||
async function peekMaxClock(orderId: string): Promise<number> {
|
||||
const sql = getSqlForDurableStore("order-notes");
|
||||
if (!sql) {
|
||||
let max = 0;
|
||||
for (const n of mem.values()) if (n.order_id === orderId && n.clock > max) max = n.clock;
|
||||
return max;
|
||||
}
|
||||
return maxClock(sql, orderId);
|
||||
}
|
||||
|
||||
export interface UpdateNoteInput {
|
||||
note_id: string;
|
||||
author_id: string;
|
||||
body: string;
|
||||
/** Optimistic concurrency: must equal the caller's seen version, else 409. */
|
||||
expected_version: number;
|
||||
}
|
||||
|
||||
export async function updateNote(input: UpdateNoteInput): Promise<OrderNote> {
|
||||
const body = input.body.trim();
|
||||
if (!body) throw new TypeError("note body must be non-empty");
|
||||
if (!Number.isInteger(input.expected_version) || input.expected_version < 1) {
|
||||
throw new TypeError("expected_version must be a positive integer");
|
||||
}
|
||||
|
||||
const sql = getSqlForDurableStore("order-notes");
|
||||
if (!sql) {
|
||||
const current = mem.get(input.note_id);
|
||||
if (!current) throw new NoteNotFoundError(input.note_id);
|
||||
if (current.version !== input.expected_version) {
|
||||
throw new NoteVersionConflictError(input.note_id, input.expected_version, current.version);
|
||||
}
|
||||
const updated: OrderNote = {
|
||||
...current,
|
||||
body,
|
||||
version: current.version + 1,
|
||||
clock: nextLamportClock(current.clock),
|
||||
updated_at: new Date().toISOString(),
|
||||
};
|
||||
mem.set(updated.note_id, updated);
|
||||
return updated;
|
||||
}
|
||||
|
||||
const rows = await sql`
|
||||
UPDATE order_notes
|
||||
SET body = ${body},
|
||||
version = version + 1,
|
||||
clock = clock + 1,
|
||||
updated_at = ${new Date().toISOString()}
|
||||
WHERE note_id = ${input.note_id} AND version = ${input.expected_version}
|
||||
RETURNING *
|
||||
`;
|
||||
if (rows.length === 0) {
|
||||
const existing = await sql`SELECT * FROM order_notes WHERE note_id = ${input.note_id}`;
|
||||
if (existing.length === 0) throw new NoteNotFoundError(input.note_id);
|
||||
throw new NoteVersionConflictError(
|
||||
input.note_id,
|
||||
input.expected_version,
|
||||
Number(existing[0].version),
|
||||
);
|
||||
}
|
||||
return rowToNote(rows[0]);
|
||||
}
|
||||
|
||||
/** Direct read by note id — used by the route for server-authoritative tenant checks. */
|
||||
export async function findNote(noteId: string): Promise<OrderNote | null> {
|
||||
const sql = getSqlForDurableStore("order-notes");
|
||||
if (!sql) return mem.get(noteId) ?? null;
|
||||
const rows = await sql`SELECT * FROM order_notes WHERE note_id = ${noteId}`;
|
||||
return rows.length > 0 ? rowToNote(rows[0]) : null;
|
||||
}
|
||||
|
||||
/** Delete for tests: the in-memory fallback needs isolation between tests. */
|
||||
export function resetOrderNotesMemoryForTests(): void {
|
||||
mem.clear();
|
||||
}
|
||||
@@ -110,6 +110,11 @@ const securityHeaders = [
|
||||
|
||||
/** @type {import('next').NextConfig} */
|
||||
const nextConfig = {
|
||||
// @siax/contracts, @siax/config och @siax/tokens exporterar fortfarande
|
||||
// src/ (TS) enligt DES1GN-konventionen — Turbopack måste transpilera dem
|
||||
// (ActionGate-piloten, ADR-0001). @siax/ui 0.12.0+ (och @siax/charts 0.4.0+)
|
||||
// shippar kompilerad dist/ i tarballen och ska INTE transpileras (C1-kanarie).
|
||||
transpilePackages: ["@siax/contracts", "@siax/config", "@siax/tokens"],
|
||||
// Självhostad drift i SIAX Cloud (Coolify + Traefik) — ersätter Vercel.
|
||||
//
|
||||
// "standalone" får `next build` att packa en fristående Node-server i
|
||||
|
||||
Generated
+4
-4
@@ -50,7 +50,7 @@
|
||||
"input-otp": "1.4.1",
|
||||
"jose": "^5.9.6",
|
||||
"lucide-react": "^0.454.0",
|
||||
"next": "16.3.4",
|
||||
"next": "16.3.6",
|
||||
"next-themes": "^0.4.6",
|
||||
"postgres": "^3.4.9",
|
||||
"react": "19.2.8",
|
||||
@@ -6605,9 +6605,9 @@
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/next": {
|
||||
"version": "16.3.4",
|
||||
"resolved": "https://registry.npmjs.org/next/-/next-16.3.4.tgz",
|
||||
"integrity": "sha512-/Ztf6CeRH+ejEXUrYtqI4gkS66eFIHuSwqi60RgcpWKodxFZx2/dqVCMKBwILfAHXQ+F1b1vAudgj3mnxqtoIA==",
|
||||
"version": "16.3.6",
|
||||
"resolved": "https://registry.npmjs.org/next/-/next-16.3.6.tgz",
|
||||
"integrity": "sha512-L+otWM/aQbYTx98aZhgEoMb4bZAXx1YVW4UMA/vuCyCoWG5HJyZUili8QAkqzrcC+5///tsz3s0M+SlyB5bLMw==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@next/env": "16.3.4",
|
||||
|
||||
+16
-10
@@ -16,7 +16,9 @@
|
||||
"format:write": "prettier --write .",
|
||||
"typecheck:app": "tsc --noEmit -p tsconfig.json",
|
||||
"siax:doctor": "node scripts/siax-doctor.mjs",
|
||||
"deps:check": "depcruise src app components lib --config .dependency-cruiser.cjs"
|
||||
"deps:check": "depcruise src app components lib --config .dependency-cruiser.cjs",
|
||||
"vehiq:oltp-ingest": "tsx scripts/vehiq-oltp-ingest/run.ts",
|
||||
"check:contrast": "node scripts/ci/check-text-contrast.mjs"
|
||||
},
|
||||
"dependencies": {
|
||||
"@fontsource-variable/inter": "^5.2.5",
|
||||
@@ -50,19 +52,23 @@
|
||||
"@radix-ui/react-toggle-group": "1.1.19",
|
||||
"@radix-ui/react-tooltip": "1.2.16",
|
||||
"@siax/0pt-decision": "workspace:*",
|
||||
"@siax/charts": "^0.5.0",
|
||||
"@siax/collaboration": "0.3.0",
|
||||
"@siax/config": "^0.2.1",
|
||||
"@siax/contracts": "^0.10.0",
|
||||
"@siax/import-export": "^0.2.0",
|
||||
"@siax/tokens": "^0.1.2",
|
||||
"@siax/ui": "^0.4.1",
|
||||
"@siax/ui": "^0.14.0",
|
||||
"autoprefixer": "^10.4.20",
|
||||
"class-variance-authority": "^0.7.1",
|
||||
"clsx": "^2.1.1",
|
||||
"cmdk": "1.0.4",
|
||||
"cmdk": "1.1.1",
|
||||
"date-fns": "4.1.0",
|
||||
"embla-carousel-react": "8.5.1",
|
||||
"input-otp": "1.4.1",
|
||||
"jose": "^5.9.6",
|
||||
"lucide-react": "^0.454.0",
|
||||
"next": "16.3.4",
|
||||
"next": "16.3.6",
|
||||
"next-themes": "^0.4.6",
|
||||
"postgres": "^3.4.9",
|
||||
"react": "19.2.8",
|
||||
@@ -70,8 +76,7 @@
|
||||
"react-dom": "19.2.8",
|
||||
"react-hook-form": "^7.60.0",
|
||||
"react-resizable-panels": "^2.1.7",
|
||||
"recharts": "2.15.4",
|
||||
"sonner": "^1.7.4",
|
||||
"sonner": "^2.0.6",
|
||||
"stripe": "^22.2.3",
|
||||
"tailwind-merge": "^3.3.1",
|
||||
"tailwindcss-animate": "^1.0.7",
|
||||
@@ -89,15 +94,16 @@
|
||||
"postcss": "^8.5.28",
|
||||
"prettier": "^3.9.6",
|
||||
"tailwindcss": "^4.1.9",
|
||||
"tsx": "^4.23.15",
|
||||
"tw-animate-css": "1.4.0",
|
||||
"typescript": "^5",
|
||||
"typescript-eslint": "^8.15.0",
|
||||
"vitest": "^3.2.7"
|
||||
},
|
||||
"pnpm": {
|
||||
"onlyBuiltDependencies": [
|
||||
"sharp",
|
||||
"esbuild"
|
||||
]
|
||||
"overrides": {
|
||||
"@siax/ui>@siax/contracts": "0.10.0",
|
||||
"@siax/ui>@siax/import-export": "0.2.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Generated
+366
-560
File diff suppressed because it is too large
Load Diff
Executable
+74
@@ -0,0 +1,74 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Blocking guard for finding UI-09 (measured 2026-09-26): 0pt.app carried 82
|
||||
* `color-contrast` violations at axe-core 4.10.2 impact=serious — the estate's
|
||||
* single largest a11y defect. Every one of them came from the same shape: a
|
||||
* Tailwind alpha modifier on a muted text colour.
|
||||
*
|
||||
* Measured WCAG 2.2 contrast against the two theme backgrounds in
|
||||
* styles/globals.css (--background: oklch(1 0 0) light / oklch(0.145 0 0) dark,
|
||||
* --muted-foreground: oklch(0.556 0 0) / oklch(0.708 0 0)):
|
||||
*
|
||||
* text-muted-foreground 4.73:1 light 7.63:1 dark PASS (AA needs 4.5)
|
||||
* text-muted-foreground/70 2.71:1 4.20:1 FAIL
|
||||
* text-muted-foreground/60 2.29:1 3.37:1 FAIL
|
||||
* text-muted-foreground/50 1.96:1 2.67:1 FAIL
|
||||
* text-muted-foreground/40 1.69:1 2.11:1 FAIL
|
||||
* text-muted-foreground/10 1.13:1 1.13:1 FAIL
|
||||
*
|
||||
* The unmodified token passes in both themes, so the fix is simply never to
|
||||
* fade it. Purely decorative text is exempt — but it has to SAY so: the element
|
||||
* must carry `aria-hidden`, which is also what makes axe skip it. The single
|
||||
* remaining case (app/bestall/page.tsx, the oversized "01" masthead numeral) is
|
||||
* `aria-hidden` + `select-none` + `pointer-events-none` and is allowed by that
|
||||
* rule.
|
||||
*
|
||||
* This is a source-level guard, not a browser run. The full axe-core job that
|
||||
* UI-09 also asks for needs a build + serve in CI and is tracked separately;
|
||||
* until it exists this stops the regression at the shape that caused it.
|
||||
*/
|
||||
import { readdirSync, readFileSync, statSync } from 'node:fs';
|
||||
import { join, relative } from 'node:path';
|
||||
|
||||
const ROOT = new URL('../..', import.meta.url).pathname.replace(/\/$/, '');
|
||||
const SKIP = new Set(['node_modules', '.next', '.git', 'dist', 'build', 'coverage', '.turbo']);
|
||||
const EXT = /\.(tsx|ts|jsx|js|css)$/;
|
||||
/** Alpha-modified colour utilities on TEXT. Background/border alphas are fine. */
|
||||
const PATTERN = /\btext-(muted-foreground|foreground|primary|secondary|accent-foreground|card-foreground|popover-foreground)\/\d+/g;
|
||||
|
||||
function walk(dir, out = []) {
|
||||
for (const name of readdirSync(dir)) {
|
||||
if (SKIP.has(name)) continue;
|
||||
const p = join(dir, name);
|
||||
if (statSync(p).isDirectory()) walk(p, out);
|
||||
else if (EXT.test(name)) out.push(p);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
const violations = [];
|
||||
for (const file of walk(ROOT)) {
|
||||
const src = readFileSync(file, 'utf8');
|
||||
const lines = src.split('\n');
|
||||
lines.forEach((line, i) => {
|
||||
for (const m of line.matchAll(PATTERN)) {
|
||||
// Decorative text is exempt when the element declares itself hidden to AT.
|
||||
// The attribute may sit on an earlier line of the same JSX element.
|
||||
const window = lines.slice(Math.max(0, i - 6), i + 1).join('\n');
|
||||
if (/\baria-hidden\b/.test(window)) continue;
|
||||
violations.push({ file: relative(ROOT, file), line: i + 1, token: m[0], text: line.trim() });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (violations.length) {
|
||||
console.error(`\n✗ ${violations.length} alpha-faded text colour(s) found — WCAG 2.2 AA 1.4.3 (finding UI-09).\n`);
|
||||
for (const v of violations) {
|
||||
console.error(` ${v.file}:${v.line} ${v.token}`);
|
||||
console.error(` ${v.text.slice(0, 160)}`);
|
||||
}
|
||||
console.error('\n Use the unmodified token (text-muted-foreground = 4.73:1 light / 7.63:1 dark).');
|
||||
console.error(' If the text really is decoration, put aria-hidden on the element.\n');
|
||||
process.exit(1);
|
||||
}
|
||||
console.log('✓ no alpha-faded text colours — WCAG 2.2 AA 1.4.3 contrast shape is clean (UI-09)');
|
||||
@@ -0,0 +1,137 @@
|
||||
#!/usr/bin/env -S node
|
||||
/**
|
||||
* scripts/vehiq-oltp-ingest/run.ts
|
||||
* ===================================
|
||||
* Thin CLI shim over src/ingestion/vehiq-oltp-ingest-runner.ts. All the
|
||||
* actual logic (connectors, the runner call, the checkpoint mapping) is in
|
||||
* that blocking-typechecked, tested `src/` module; this file only reads
|
||||
* `process.env`, wires real implementations, and prints a summary.
|
||||
*
|
||||
* TWO MODES, PREVIEW IS THE DEFAULT — this is the activation gate:
|
||||
*
|
||||
* VEHIQ_INGEST_MODE=preview (default)
|
||||
* One bounded read-only connect+discover+read per source, against
|
||||
* whatever checkpoint already exists. NEVER commits a checkpoint, NEVER
|
||||
* calls the sink, NEVER touches ClickHouse. Safe to run against real
|
||||
* production at any time — this is what proves "zero batches, zero
|
||||
* writes" without activating anything.
|
||||
*
|
||||
* VEHIQ_INGEST_MODE=run
|
||||
* The real, scheduled path: P062 runner, durable checkpoints, writes
|
||||
* landing in `opt_dataset_projection`. Requires DATABASE_URL (0pt's own
|
||||
* metadata store, for durable checkpoints) and the OPT_ANALYTICS_
|
||||
* CLICKHOUSE_* vars (the write target) to be set, or it refuses to run
|
||||
* rather than silently falling back to in-memory checkpoints or a
|
||||
* no-op sink — see docs/P070_VEHIQ_OLTP_EXTRACTOR.md "What activating
|
||||
* this in production actually requires".
|
||||
*
|
||||
* NO VEHIQ_SOURCE_DATABASE_URL AT ALL -> exits 0 having done nothing. This
|
||||
* is what makes the scheduled workflow (.gitea/workflows/vehiq-oltp-
|
||||
* ingest.yml) safe to leave enabled with no secret configured: cron fires,
|
||||
* the script logs "not configured", exits 0, no error, no alert.
|
||||
*/
|
||||
import {
|
||||
VEHIQ_OLTP_SOURCES,
|
||||
previewVehiqOltpSource,
|
||||
runVehiqOltpIngestion,
|
||||
} from "../../src/ingestion/vehiq-oltp-ingest-runner";
|
||||
import { makePostgresQueryImpl } from "../../src/connectors/postgres-query-impl";
|
||||
import { createIngestionRepository } from "../../src/repositories/ingestion-repository";
|
||||
import { SchemaRegistry } from "../../src/schema/schema-registry";
|
||||
import { LineageStore } from "../../src/lineage/lineage-store";
|
||||
import { ClickHouseAnalyticsEngine } from "../../src/analytics/engine/clickhouse-engine";
|
||||
import { projectionSink } from "../../src/ingestion/projection-sink";
|
||||
import type { AnalyticsIdentity } from "../../src/analytics/engine/siax-analytics-engine";
|
||||
|
||||
const TENANT_ID = process.env.VEHIQ_INGEST_TENANT_ID ?? "t_vehiq";
|
||||
const PROJECT_ID = process.env.VEHIQ_INGEST_PROJECT_ID ?? "vehiq";
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const sourceUrl = process.env.VEHIQ_SOURCE_DATABASE_URL;
|
||||
if (!sourceUrl) {
|
||||
console.log(
|
||||
JSON.stringify({ status: "not_configured", detail: "VEHIQ_SOURCE_DATABASE_URL is unset — nothing to do" }),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const mode = process.env.VEHIQ_INGEST_MODE === "run" ? "run" : "preview";
|
||||
const { query, close } = makePostgresQueryImpl({
|
||||
connectionString: sourceUrl,
|
||||
ssl: process.env.VEHIQ_SOURCE_PGSSL === "require" ? "require" : false,
|
||||
statementTimeoutMs: process.env.VEHIQ_SOURCE_STATEMENT_TIMEOUT_MS
|
||||
? Number(process.env.VEHIQ_SOURCE_STATEMENT_TIMEOUT_MS)
|
||||
: undefined,
|
||||
});
|
||||
|
||||
try {
|
||||
if (mode === "preview") {
|
||||
const repo = createIngestionRepository(process.env.DATABASE_URL);
|
||||
const results = [];
|
||||
for (const source of VEHIQ_OLTP_SOURCES) {
|
||||
results.push(
|
||||
await previewVehiqOltpSource(source, query, repo, {
|
||||
sourceId: "vehiq_oltp",
|
||||
tenantId: TENANT_ID,
|
||||
projectId: PROJECT_ID,
|
||||
limit: 100,
|
||||
}),
|
||||
);
|
||||
}
|
||||
console.log(JSON.stringify({ status: "preview", mode, results }, null, 2));
|
||||
return;
|
||||
}
|
||||
|
||||
// mode === "run" — the activation-gated real path.
|
||||
if (!process.env.DATABASE_URL) {
|
||||
throw new Error(
|
||||
"VEHIQ_INGEST_MODE=run requires DATABASE_URL (0pt's own metadata store, for durable checkpoints) — " +
|
||||
"refusing to run with in-memory checkpoints, which would silently re-read every row on every scheduled invocation",
|
||||
);
|
||||
}
|
||||
const chBaseUrl = process.env.OPT_ANALYTICS_CLICKHOUSE_BASE_URL;
|
||||
if (!chBaseUrl) {
|
||||
throw new Error(
|
||||
"VEHIQ_INGEST_MODE=run requires OPT_ANALYTICS_CLICKHOUSE_BASE_URL — refusing to run with no write target",
|
||||
);
|
||||
}
|
||||
|
||||
const repo = createIngestionRepository(process.env.DATABASE_URL);
|
||||
const schemas = new SchemaRegistry(repo);
|
||||
const lineage = new LineageStore(repo);
|
||||
const engine = new ClickHouseAnalyticsEngine({
|
||||
baseUrl: chBaseUrl,
|
||||
basicAuth: process.env.OPT_ANALYTICS_CLICKHOUSE_BASIC_AUTH,
|
||||
database: process.env.OPT_ANALYTICS_CLICKHOUSE_DATABASE ?? "opt_analytics",
|
||||
});
|
||||
const identity: AnalyticsIdentity = {
|
||||
tenant_id: TENANT_ID,
|
||||
project_id: PROJECT_ID,
|
||||
principal_id: "svc_vehiq_oltp_extractor",
|
||||
};
|
||||
const sink = projectionSink({
|
||||
engine,
|
||||
identity,
|
||||
datasetIdFor: (partitionKey) => `vehiq.${partitionKey}`,
|
||||
});
|
||||
|
||||
const summary = await runVehiqOltpIngestion({
|
||||
repo,
|
||||
schemas,
|
||||
lineage,
|
||||
sourceQuery: query,
|
||||
oltpSink: sink,
|
||||
dimensionSink: sink,
|
||||
tenantId: TENANT_ID,
|
||||
projectId: PROJECT_ID,
|
||||
});
|
||||
console.log(JSON.stringify({ status: "ran", mode, summary }, null, 2));
|
||||
} finally {
|
||||
await close();
|
||||
}
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error(JSON.stringify({ status: "error", detail: err instanceof Error ? err.message : String(err) }));
|
||||
process.exitCode = 1;
|
||||
});
|
||||
+1
-1
@@ -123,7 +123,7 @@
|
||||
},
|
||||
"reliability": {
|
||||
"backupFrequency": "none",
|
||||
"provenRestore": false
|
||||
"provenRestore": true
|
||||
},
|
||||
"engines": [
|
||||
"postgres",
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
{
|
||||
"id": "pe-0pt-f6a29da7e586",
|
||||
"repo": "0pt",
|
||||
"evidenceType": "deploy-log",
|
||||
"result": "pass",
|
||||
"verifiedAt": "2026-09-22T18:52:42Z",
|
||||
"verifiedBy": "coolify-deployment-db (application_deployment_queues, senaste finished-deploy)",
|
||||
"proofRef": "coolify://deployment/75me1uneekzvjevgqeewcovm",
|
||||
"sourceSha": "f6a29da7e58601ce426181b11ea4959e90f07dcc"
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,4 @@
|
||||
# 0pt — cosign-signeringskedjan (2026-09-29)
|
||||
|
||||
Prod-imagen git.cloud.siax.io/sax3l/0pt:prod-09b9cc80bf (sha256:8a1649c7418c8728e6793ff885814a266f055114f950e31ca9d7b252645dedd3) är cosign-signerad
|
||||
med nyckelparet vars pubkey ligger här. Privata nyckeln raderad efter signering.
|
||||
@@ -0,0 +1,4 @@
|
||||
-----BEGIN PUBLIC KEY-----
|
||||
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEXES2u9EA2vFf6aQS5Hg/hsjudCJk
|
||||
mC1G/IFtBOzweyPlQD+A84UcYDJmxIR5Cfq5t8nvF3bY/ff18AhhK+WZmw==
|
||||
-----END PUBLIC KEY-----
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"id": "sb-0pt-f6a29da7e586",
|
||||
"repo": "0pt",
|
||||
"commitSha": "f6a29da7e58601ce426181b11ea4959e90f07dcc",
|
||||
"branch": "main",
|
||||
"producedAt": "2026-09-22T18:52:42Z"
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
sonar.projectKey=0pt
|
||||
sonar.host.url=https://sonar.siax.io
|
||||
sonar.exclusions=**/node_modules/**,**/dist/**,**/build/**,**/.next/**,**/out/**,**/coverage/**,**/__pycache__/**,**/*.min.js,**/.venv/**,**/.pnpm-store/**,**/.opt-blobs/**,**/.data/**
|
||||
@@ -0,0 +1,64 @@
|
||||
-- 015_order_status_check.sql — Kanonisk orderstatus (ADR-0002, C3 2026-09-29)
|
||||
--
|
||||
-- Guardar `orders.status` (fri text sedan sql/001_init.sql line ~92) mot den
|
||||
-- kanoniska unionen i lib/order-flow.ts (ORDER_STATUSES). Denna fil KÖRS EJ
|
||||
-- automatiskt av deploy — Simon kör den manuellt mot prod efter harvest.
|
||||
--
|
||||
-- KÖRORDNING (DoD §17 backup-städ, §33 migreringsordning, §35 restore-drill):
|
||||
-- 1. BACKUP FÖRST: pg_dump "$DATABASE_URL" -t orders -Fc -f orders-pre-015.dump
|
||||
-- (och verifiera att restore-drillen har körts nyligen — §35).
|
||||
-- 2. HARVEST-PREFLIGHT (läs-only, körs före fas A):
|
||||
-- SELECT status, COUNT(*) FROM orders GROUP BY status ORDER BY 2 DESC;
|
||||
-- Alla rader MÅSTE ligga i listan nedan. Finns okända statusar:
|
||||
-- AVBRYT — mappa dem först (UPDATE ... WHERE status = '<okänd>') i en
|
||||
-- egen, granskad migrering; kör aldrig fas A med okända rader kvar.
|
||||
-- 3. FAS A ( TRANSACTIONEN NEDAN — ADD CONSTRAINT ... NOT VALID ):
|
||||
-- tar bara ett kort ACCESS EXCLUSIVE-lås vid själva katalogändringen,
|
||||
-- validerar INTE befintliga rader, blockerar inte skrivningar längre tid.
|
||||
-- 4. FAS B (SEPARAT session/transaktion — VALIDATE CONSTRAINT):
|
||||
-- BEGIN; SET lock_timeout = '5s';
|
||||
-- ALTER TABLE orders VALIDATE CONSTRAINT order_status_check;
|
||||
-- COMMIT;
|
||||
-- VALIDATE tar SHARE UPDATE EXCLUSIVE (skrivningar tillåtna); med
|
||||
-- lock_timeout stänger vi ute långa låsköer. Misslyckas fas B: retry.
|
||||
--
|
||||
-- Listan är kod-SSOT: håll synkroniserad med ORDER_STATUSES i lib/order-flow.ts.
|
||||
|
||||
BEGIN;
|
||||
|
||||
-- Preflight-guard: fas A nekar om okända statusar finns (fail-closed).
|
||||
-- (SELECT ... FOR UPDATE-stil låsning behövs ej — NOT VALID-constrainten
|
||||
-- läser inga rader; vi vill bara neka körning vid dirt-data.)
|
||||
DO $preflight$
|
||||
DECLARE
|
||||
unknown_rows bigint;
|
||||
BEGIN
|
||||
SELECT COUNT(*) INTO unknown_rows
|
||||
FROM orders
|
||||
WHERE status NOT IN ('draft', 'quoted', 'in_production', 'review', 'accepted', 'delivered', 'cancelled');
|
||||
IF unknown_rows > 0 THEN
|
||||
RAISE EXCEPTION
|
||||
'order_status_check preflight: % rader med status utanför den kanoniska unionen — kör harvest (SELECT status, COUNT(*) FROM orders GROUP BY status), mappa okända statusar, KÖR EJ fas A', unknown_rows;
|
||||
END IF;
|
||||
END
|
||||
$preflight$;
|
||||
|
||||
-- Rapportera (loggar) okända statusar om preflighten ändå passerar tom:
|
||||
SELECT status, COUNT(*) AS n
|
||||
FROM orders
|
||||
WHERE status NOT IN ('draft', 'quoted', 'in_production', 'review', 'accepted', 'delivered', 'cancelled')
|
||||
GROUP BY status;
|
||||
-- (0 rader = grönt; >0 rader = ovanstående DO-block har redan avbrutit.)
|
||||
|
||||
ALTER TABLE orders
|
||||
ADD CONSTRAINT order_status_check
|
||||
CHECK (status IN ('draft', 'quoted', 'in_production', 'review', 'accepted', 'delivered', 'cancelled'))
|
||||
NOT VALID;
|
||||
|
||||
COMMIT;
|
||||
|
||||
-- FAS B körs i EGEN session (se körordning steg 4) — kommenterad här för att
|
||||
-- den ALDRIG ska ligga i samma transaktion som fas A:
|
||||
-- BEGIN; SET lock_timeout = '5s';
|
||||
-- ALTER TABLE orders VALIDATE CONSTRAINT order_status_check;
|
||||
-- COMMIT;
|
||||
@@ -0,0 +1,16 @@
|
||||
-- 016: order notes — persisted collaboration state för admin-orders
|
||||
-- (@siax/collaboration-kernelns första produktkonsument, spår C).
|
||||
-- Varje note är en kernel-CollaborationOperation: `clock` är Lamport-klockan,
|
||||
-- `version` är optimistisk samtidighetskontroll (stale skrivning -> HTTP 409).
|
||||
CREATE TABLE IF NOT EXISTS order_notes (
|
||||
note_id TEXT PRIMARY KEY,
|
||||
order_id TEXT NOT NULL,
|
||||
tenant_id TEXT,
|
||||
author_id TEXT NOT NULL,
|
||||
body TEXT NOT NULL,
|
||||
version INTEGER NOT NULL DEFAULT 1,
|
||||
clock BIGINT NOT NULL DEFAULT 1,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS order_notes_order_idx ON order_notes(order_id);
|
||||
@@ -113,10 +113,15 @@ export class GraphMailboxAdapter {
|
||||
return this.cachedToken.token;
|
||||
}
|
||||
|
||||
// Graph-API:t är genuint ostrukturerat JSON (value-array med fri form);
|
||||
// full typning av hela Graph-ytan är egen insats — any här är medveten
|
||||
// och avgränsad till denna adapter (eslint-undantag med skäl, DoD §13).
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
private async get(path: string): Promise<any> {
|
||||
const token = await this.getToken();
|
||||
const url = path.startsWith("http") ? path : `${GRAPH_BASE}${path}`;
|
||||
const res = await fetch(url, { headers: { authorization: `Bearer ${token}` } });
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Graph-JSON är genuint ostrukturerat; full Graph-typning är egen insats
|
||||
const json: any = await res.json();
|
||||
if (!res.ok) {
|
||||
throw new Error(`Graph GET ${path} -> ${json.error?.code || res.status}: ${json.error?.message || ""}`);
|
||||
@@ -150,7 +155,7 @@ export class GraphMailboxAdapter {
|
||||
receivedAt: m.receivedDateTime || null,
|
||||
senderName: m.from?.emailAddress?.name || null,
|
||||
senderEmail: m.from?.emailAddress?.address || null,
|
||||
toRecipients: (m.toRecipients || []).map((r: any) => r.emailAddress?.address).filter(Boolean),
|
||||
toRecipients: (m.toRecipients || []).map((r: { emailAddress?: { address?: string } }) => r.emailAddress?.address).filter(Boolean),
|
||||
subject: m.subject || null,
|
||||
bodyPreview: m.bodyPreview || null,
|
||||
bodyText: m.body?.content ? stripHtml(m.body.content) : m.bodyPreview || null,
|
||||
|
||||
@@ -0,0 +1,258 @@
|
||||
/**
|
||||
* src/connectors/postgres-keyed-lookup-connector.ts
|
||||
* ====================================================
|
||||
* P061 connector: a narrow, explicit-key-list lookup against a large table —
|
||||
* built specifically so `0pt dimension_vehicle` never becomes `SELECT *
|
||||
* FROM vehicle` against the 98M-row/85GB `vehiq.vehicle` table.
|
||||
*
|
||||
* THE RULE THIS ENFORCES (Simon's decision #2, `admin/vehiq` 2026-09-25
|
||||
* pass): `dealer_stock_entry.vehicle_id` -> unique vehicle IDs -> a
|
||||
* TARGETED lookup of just those rows' attributes -> `dimension_vehicle`.
|
||||
* Never a full scan, never an unbounded WHERE clause, never "give me
|
||||
* everything and I'll filter downstream".
|
||||
*
|
||||
* HOW IT'S STRUCTURALLY DIFFERENT FROM `postgres-watermark-connector.ts`,
|
||||
* not just configured differently: this connector's config carries `keys`
|
||||
* — a caller-supplied, already-resolved, finite list of record keys. There
|
||||
* is no watermark, no `ORDER BY updated_at`, no notion of "everything since
|
||||
* X". `read()` can ONLY ever emit rows whose key is in that list, and
|
||||
* `connect()` REFUSES an empty or missing key list rather than defaulting
|
||||
* to "read everything" — see the "zero keys issues zero queries" test,
|
||||
* which is the assertion that actually proves the narrow-scan property
|
||||
* (not just that the SQL text LOOKS bounded).
|
||||
*
|
||||
* THE ORCHESTRATOR (scripts/vehiq-oltp-ingest/run.ts) is what supplies
|
||||
* `keys`: the DISTINCT `vehicle_id`s seen in the `dealer_stock_entry` batch
|
||||
* this connector instance was built for, and nothing else. A full snapshot/
|
||||
* backfill of the whole `vehicle` table is a deliberate, separate, later
|
||||
* decision — see docs/P070_VEHIQ_OLTP_EXTRACTOR.md's "Deferred: full
|
||||
* vehicle dimension backfill" section. This connector does not build
|
||||
* towards that; it is the permanent narrow path, not a stepping stone.
|
||||
*/
|
||||
import {
|
||||
ConnectorNotConnectedError,
|
||||
type BackfillRequest,
|
||||
type Connector,
|
||||
type ConnectorConfig,
|
||||
type ConnectorContext,
|
||||
type ConnectorHealth,
|
||||
type ConnectorRecord,
|
||||
type DiscoveryResult,
|
||||
type ReadRequest,
|
||||
type ReadResult,
|
||||
type ValidationResult,
|
||||
} from "./connector";
|
||||
import type { IngestionCheckpoint } from "../types/ingestion";
|
||||
import type { RecordSchemaFields } from "../schema/record-schema";
|
||||
import type { QueryImpl, SourceRow } from "./postgres-watermark-connector";
|
||||
|
||||
export const POSTGRES_KEYED_LOOKUP_CONNECTOR_VERSION = "postgres-keyed-lookup-connector@1";
|
||||
|
||||
export interface PostgresKeyedLookupConnectorConfig extends ConnectorConfig {
|
||||
/** Fully-qualified source table, e.g. "vehiq.vehicle". */
|
||||
table: string;
|
||||
partition_key: string;
|
||||
columns: string[];
|
||||
record_key_column: string;
|
||||
schema: RecordSchemaFields;
|
||||
/**
|
||||
* The ENTIRE set of keys this connector instance is permitted to read —
|
||||
* resolved by the caller BEFORE construction (e.g. distinct
|
||||
* `dealer_stock_entry.vehicle_id` values from one ingestion batch). Never
|
||||
* mutated after connect(); a new batch means a new connector instance.
|
||||
*/
|
||||
keys: ReadonlyArray<string | number>;
|
||||
/** How many keys go into one SQL `= ANY($1)` call. Default 500. */
|
||||
keys_per_query?: number;
|
||||
}
|
||||
|
||||
function isConfig(config: ConnectorConfig): config is PostgresKeyedLookupConnectorConfig {
|
||||
return typeof config.table === "string" && typeof config.partition_key === "string" && Array.isArray(config.keys);
|
||||
}
|
||||
|
||||
/** Cursor vocabulary: the decimal count of keys already looked up (an index into `config.keys`). */
|
||||
function parseCursor(cursor: string | undefined): number {
|
||||
if (cursor === undefined) return 0;
|
||||
const n = Number.parseInt(cursor, 10);
|
||||
if (!Number.isInteger(n) || n < 0) throw new Error(`invalid cursor "${cursor}" — expected a key-list offset`);
|
||||
return n;
|
||||
}
|
||||
|
||||
export class PostgresKeyedLookupConnector implements Connector {
|
||||
readonly connector_id = "postgres_keyed_lookup";
|
||||
readonly connector_version = POSTGRES_KEYED_LOOKUP_CONNECTOR_VERSION;
|
||||
|
||||
private config?: PostgresKeyedLookupConnectorConfig;
|
||||
private ctx?: ConnectorContext;
|
||||
private lastCursor = new Map<string, string | undefined>();
|
||||
private connected = false;
|
||||
|
||||
/** Query calls actually issued, for tests to assert the narrow-scan property against. */
|
||||
readonly queryLog: Array<{ sql: string; keys: Array<string | number> }> = [];
|
||||
|
||||
constructor(private readonly queryImpl: QueryImpl) {}
|
||||
|
||||
validate(config: ConnectorConfig): ValidationResult {
|
||||
const errors: string[] = [];
|
||||
if (typeof config.table !== "string" || config.table.length === 0) errors.push("table is required");
|
||||
if (typeof config.partition_key !== "string" || config.partition_key.length === 0) {
|
||||
errors.push("partition_key is required");
|
||||
}
|
||||
const columns = config.columns;
|
||||
if (!Array.isArray(columns) || columns.length === 0) {
|
||||
errors.push("columns is required and must be a non-empty explicit column list");
|
||||
} else if (columns.some((c) => c === "*" || typeof c !== "string" || c.length === 0)) {
|
||||
errors.push('columns must not contain "*" or an empty entry — SELECT * is not permitted');
|
||||
}
|
||||
if (typeof config.record_key_column !== "string" || config.record_key_column.length === 0) {
|
||||
errors.push("record_key_column is required");
|
||||
} else if (Array.isArray(columns) && !columns.includes(config.record_key_column)) {
|
||||
errors.push("record_key_column must be included in columns");
|
||||
}
|
||||
const schema = config.schema;
|
||||
if (typeof schema !== "object" || schema === null || Object.keys(schema).length === 0) {
|
||||
errors.push("schema is required and must declare at least one field");
|
||||
}
|
||||
if (!Array.isArray(config.keys)) {
|
||||
errors.push("keys is required and must be an array");
|
||||
} else if (config.keys.length === 0) {
|
||||
// Deliberately a hard validation error, not a silent "0 records" read.
|
||||
// A caller with nothing to look up should not construct this connector
|
||||
// at all — this keeps "no keys => no query, ever" true even for a
|
||||
// caller that forgets to check first.
|
||||
errors.push("keys must not be empty — construct this connector only when there is something to look up");
|
||||
}
|
||||
if (config.keys_per_query !== undefined && (typeof config.keys_per_query !== "number" || config.keys_per_query <= 0)) {
|
||||
errors.push("keys_per_query must be a positive number when provided");
|
||||
}
|
||||
return { valid: errors.length === 0, errors };
|
||||
}
|
||||
|
||||
async connect(config: ConnectorConfig, ctx: ConnectorContext): Promise<void> {
|
||||
const verdict = this.validate(config);
|
||||
if (!verdict.valid) {
|
||||
throw new Error(`postgres_keyed_lookup connector config invalid: ${verdict.errors.join("; ")}`);
|
||||
}
|
||||
if (!isConfig(config)) throw new Error("postgres_keyed_lookup connector config invalid");
|
||||
this.config = config;
|
||||
this.ctx = ctx;
|
||||
this.connected = true;
|
||||
}
|
||||
|
||||
async discover(): Promise<DiscoveryResult> {
|
||||
const config = this.requireSession("discover");
|
||||
return {
|
||||
partitions: [{ partition_key: config.partition_key, name: config.table, schema: config.schema }],
|
||||
modes: ["batch"],
|
||||
};
|
||||
}
|
||||
|
||||
async health(): Promise<ConnectorHealth> {
|
||||
const config = this.requireSession("health");
|
||||
const started = Date.now();
|
||||
try {
|
||||
await this.queryImpl(`SELECT 1 FROM ${config.table} LIMIT 0`, []);
|
||||
return { status: "up", latency_ms: Date.now() - started };
|
||||
} catch (err) {
|
||||
return {
|
||||
status: "down",
|
||||
latency_ms: Date.now() - started,
|
||||
detail: err instanceof Error ? err.message : "unreachable",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async read(req: ReadRequest): Promise<ReadResult> {
|
||||
const config = this.requireSession("read");
|
||||
const offset = parseCursor(req.cursor);
|
||||
const pageLimit = Math.min(req.limit, config.keys_per_query ?? 500);
|
||||
const page = config.keys.slice(offset, offset + pageLimit);
|
||||
|
||||
let rows: SourceRow[] = [];
|
||||
if (page.length > 0) {
|
||||
const columnList = config.columns.join(", ");
|
||||
const sql = `SELECT ${columnList} FROM ${config.table} WHERE ${config.record_key_column} = ANY($1)`;
|
||||
this.queryLog.push({ sql, keys: [...page] });
|
||||
rows = await this.queryImpl(sql, [page]);
|
||||
}
|
||||
// Structural guarantee, not just an intent: if `page` is empty, `queryImpl`
|
||||
// is never called at all — this branch cannot silently widen into an
|
||||
// unbounded scan even if a future edit forgets the `page.length > 0` guard
|
||||
// above, because the caller of this method never asks for more than
|
||||
// `config.keys.length` total across repeated read()s (see `exhausted`).
|
||||
const records = rows.map((row) => this.toRecord(row, config));
|
||||
const nextOffset = offset + page.length;
|
||||
const nextCursor = String(nextOffset);
|
||||
this.lastCursor.set(req.partition_key, nextCursor);
|
||||
return {
|
||||
records,
|
||||
next_cursor: nextCursor,
|
||||
exhausted: nextOffset >= config.keys.length,
|
||||
};
|
||||
}
|
||||
|
||||
async checkpoint(partitionKey: string): Promise<{ partition_key: string; cursor?: string }> {
|
||||
this.requireSession("checkpoint");
|
||||
return { partition_key: partitionKey, cursor: this.lastCursor.get(partitionKey) };
|
||||
}
|
||||
|
||||
async resume(checkpoint: IngestionCheckpoint, limit: number): Promise<ReadResult> {
|
||||
this.requireSession("resume");
|
||||
return this.read({ partition_key: checkpoint.partition_key, cursor: checkpoint.cursor, limit });
|
||||
}
|
||||
|
||||
/**
|
||||
* "Backfill" for a keyed lookup means re-reading a bounded INDEX range of
|
||||
* the same already-narrow key list — never a fresh, wider key set. `from`/
|
||||
* `to` are decimal offsets into `config.keys`, documented here per the
|
||||
* contract's "bound vocabulary is connector-defined" rule.
|
||||
*/
|
||||
async backfill(req: BackfillRequest): Promise<ReadResult> {
|
||||
const config = this.requireSession("backfill");
|
||||
if (!req.from || !req.to) {
|
||||
throw new Error("backfill requires both `from` and `to` — an unbounded backfill is a full reload, not a backfill");
|
||||
}
|
||||
const from = parseCursor(req.from);
|
||||
const to = parseCursor(req.to);
|
||||
if (to <= from) throw new Error("backfill requires `to` > `from`");
|
||||
const page = config.keys.slice(from, Math.min(to, from + req.limit));
|
||||
let rows: SourceRow[] = [];
|
||||
if (page.length > 0) {
|
||||
const columnList = config.columns.join(", ");
|
||||
const sql = `SELECT ${columnList} FROM ${config.table} WHERE ${config.record_key_column} = ANY($1)`;
|
||||
this.queryLog.push({ sql, keys: [...page] });
|
||||
rows = await this.queryImpl(sql, [page]);
|
||||
}
|
||||
return { records: rows.map((row) => this.toRecord(row, config)), next_cursor: undefined, exhausted: true };
|
||||
}
|
||||
|
||||
async disconnect(): Promise<void> {
|
||||
this.config = undefined;
|
||||
this.ctx = undefined;
|
||||
this.lastCursor.clear();
|
||||
this.connected = false;
|
||||
}
|
||||
|
||||
// --- internals -------------------------------------------------------------
|
||||
|
||||
private toRecord(row: SourceRow, config: PostgresKeyedLookupConnectorConfig): ConnectorRecord {
|
||||
const key = row[config.record_key_column];
|
||||
if (typeof key !== "string" && typeof key !== "number") {
|
||||
throw new Error(`record is missing string/number key column "${config.record_key_column}"`);
|
||||
}
|
||||
// Same real-driver normalization as PostgresWatermarkConnector.toRecord —
|
||||
// a Date-valued column must become an ISO string before it reaches the
|
||||
// P063 schema-conformance gate. See that connector's `normalizeRow` doc
|
||||
// comment for how this was found.
|
||||
const payload: SourceRow = {};
|
||||
for (const [field, value] of Object.entries(row)) {
|
||||
payload[field] = value instanceof Date ? value.toISOString() : value;
|
||||
}
|
||||
return { record_key: String(key), payload, operation: "upsert" };
|
||||
}
|
||||
|
||||
private requireSession(op: string): PostgresKeyedLookupConnectorConfig {
|
||||
if (!this.connected || !this.config) throw new ConnectorNotConnectedError(this.connector_id, op);
|
||||
return this.config;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* src/connectors/postgres-query-impl.ts
|
||||
* ========================================
|
||||
* The REAL `QueryImpl` (see postgres-watermark-connector.ts) — a thin,
|
||||
* read-only wrapper over `postgres` (postgres.js, already a repo dependency
|
||||
* — see src/repositories/db.ts for the sibling pattern this mirrors).
|
||||
*
|
||||
* Used ONLY by scripts/vehiq-oltp-ingest/run.ts and the opt-in realinfra
|
||||
* test. Every connector-level test in tests/connectors/*.test.ts injects a
|
||||
* fixture QueryImpl instead — no real Postgres required to prove the query
|
||||
* shape, the watermark math, or the narrow-scan property.
|
||||
*
|
||||
* DOUBLE-ENFORCED READ-ONLY. The connectors above only ever build SELECT
|
||||
* statements — there is no code path that builds anything else. This
|
||||
* wrapper adds a second, independent check anyway: it refuses to execute
|
||||
* any statement whose first keyword is not SELECT. Belt-and-suspenders is
|
||||
* appropriate here specifically because the thing on the other end is a
|
||||
* separate product's live production OLTP primary, not 0pt's own store.
|
||||
*/
|
||||
import postgres from "postgres";
|
||||
import type { QueryImpl, SourceRow } from "./postgres-watermark-connector";
|
||||
|
||||
export interface PostgresQueryImplOptions {
|
||||
connectionString: string;
|
||||
ssl?: "require" | false;
|
||||
/** Statement timeout (ms), applied per-connection so a slow query cannot pin the source's OLTP primary. Default 5000. */
|
||||
statementTimeoutMs?: number;
|
||||
}
|
||||
|
||||
class NonSelectStatementError extends Error {
|
||||
constructor(sql: string) {
|
||||
super(`refusing to execute a non-SELECT statement against a read-only source connector: ${sql.slice(0, 120)}`);
|
||||
this.name = "NonSelectStatementError";
|
||||
}
|
||||
}
|
||||
|
||||
export function assertReadOnlyStatement(sql: string): void {
|
||||
const firstWord = sql.trim().split(/\s+/, 1)[0]?.toUpperCase();
|
||||
if (firstWord !== "SELECT") throw new NonSelectStatementError(sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a `QueryImpl` bound to one `postgres` client. The caller owns the
|
||||
* client's lifecycle (open once per orchestration run, `sql.end()` when
|
||||
* done) — this function does not memoize or reuse connections across calls,
|
||||
* unlike `src/repositories/db.ts`'s deliberate memoization for 0pt's own
|
||||
* store, because this one instance is scoped to a single ingestion run
|
||||
* against an EXTERNAL production database and should not outlive it.
|
||||
*/
|
||||
export function makePostgresQueryImpl(opts: PostgresQueryImplOptions): { query: QueryImpl; sql: ReturnType<typeof postgres>; close: () => Promise<void> } {
|
||||
const sql = postgres(opts.connectionString, {
|
||||
prepare: false,
|
||||
idle_timeout: 20,
|
||||
max: 2,
|
||||
ssl: opts.ssl ?? false,
|
||||
connection: {
|
||||
statement_timeout: opts.statementTimeoutMs ?? 5_000,
|
||||
// A narrow, self-identifying application_name — same posture as
|
||||
// http-json-connector.ts's fixed User-Agent: no impersonation, easy
|
||||
// to spot in the source's own pg_stat_activity.
|
||||
application_name: "0pt-vehiq-oltp-extractor",
|
||||
},
|
||||
});
|
||||
const query: QueryImpl = async (text: string, params: unknown[]): Promise<SourceRow[]> => {
|
||||
assertReadOnlyStatement(text);
|
||||
const rows = await sql.unsafe(text, params as never[]);
|
||||
return rows.map((r) => ({ ...r }));
|
||||
};
|
||||
return { query, sql, close: () => sql.end({ timeout: 5 }) };
|
||||
}
|
||||
@@ -0,0 +1,350 @@
|
||||
/**
|
||||
* src/connectors/postgres-watermark-connector.ts
|
||||
* =================================================
|
||||
* P061 connector: a read-only, watermark-polled Postgres OLTP table.
|
||||
*
|
||||
* WHY THIS EXISTS. BI-3/BI-4 need a controlled, incremental path from a
|
||||
* product's live OLTP Postgres into 0pt — without `wal_level=logical`,
|
||||
* without a Postgres restart, without a permanent replica, and without ever
|
||||
* scanning a whole table. `docs/60-data/bi/...` (read from `admin/vehiq`,
|
||||
* 2026-09-25 pass) names the exact shape this connector implements:
|
||||
*
|
||||
* WHERE updated_at > :last_watermark ORDER BY updated_at, id
|
||||
* LIMIT :batch_size
|
||||
*
|
||||
* with a checkpoint of `(source, table, last_updated_at, last_id,
|
||||
* batch_count, checksum, ingested_at)`. This connector produces exactly
|
||||
* that query and that checkpoint — see the field-mapping table in
|
||||
* `docs/P070_VEHIQ_OLTP_EXTRACTOR.md`.
|
||||
*
|
||||
* TIE-BREAK, NOT A DEVIATION. A bare `updated_at > $1` drops rows that share
|
||||
* the exact same `updated_at` as the LAST row of a page: if row 100 and row
|
||||
* 101 both have `updated_at = T`, and row 100 is the last row of a page, a
|
||||
* naive `> T` on the next page skips row 101 forever. The query below adds
|
||||
* the documented `ORDER BY updated_at, id` tie-break to the WHERE clause
|
||||
* too: `updated_at > $1 OR (updated_at = $1 AND id > $2)`. Same watermark
|
||||
* semantics Simon specified, correct at page boundaries — exactly the class
|
||||
* of bug `tests/connectors/postgres-watermark-connector.test.ts`'s
|
||||
* "same-timestamp tie at a page boundary" case exists to catch.
|
||||
*
|
||||
* GUARD RAIL, NOT DECORATION. `validate()` refuses any config whose `table`
|
||||
* is (or ends in) the 98M-row `vehicle` table. That table gets its own
|
||||
* narrow, keyed connector (`postgres-keyed-lookup-connector.ts`) — this one
|
||||
* must never be pointed at it, even by a future caller's typo. The refusal
|
||||
* is asserted in tests, not just documented.
|
||||
*
|
||||
* READ-ONLY, STRUCTURALLY. There is no INSERT/UPDATE/DELETE anywhere in this
|
||||
* file. `columns` is an explicit allow-list the caller declares — never
|
||||
* `SELECT *` — so a schema drift on the source table cannot silently widen
|
||||
* what this connector reads or hands downstream.
|
||||
*
|
||||
* INJECTABLE QUERY IMPL — same seam as `http-json-connector.ts`'s
|
||||
* `fetchImpl`: tests drive real SQL text/params through a fixture function,
|
||||
* no real Postgres required for the connector-level tests. The default
|
||||
* implementation (`makePostgresQueryImpl`) is a thin wrapper over `postgres`
|
||||
* (postgres.js, already a repo dependency), used only by the orchestration
|
||||
* script and the opt-in realinfra test.
|
||||
*/
|
||||
import { createHash } from "node:crypto";
|
||||
import {
|
||||
ConnectorNotConnectedError,
|
||||
type BackfillRequest,
|
||||
type Connector,
|
||||
type ConnectorConfig,
|
||||
type ConnectorContext,
|
||||
type ConnectorHealth,
|
||||
type ConnectorRecord,
|
||||
type DiscoveryResult,
|
||||
type ReadRequest,
|
||||
type ReadResult,
|
||||
type ValidationResult,
|
||||
} from "./connector";
|
||||
import type { IngestionCheckpoint } from "../types/ingestion";
|
||||
import type { RecordSchemaFields } from "../schema/record-schema";
|
||||
|
||||
export const POSTGRES_WATERMARK_CONNECTOR_VERSION = "postgres-watermark-connector@1";
|
||||
|
||||
/** One row back from the source, column name -> raw value. */
|
||||
export type SourceRow = Record<string, unknown>;
|
||||
|
||||
/**
|
||||
* Injectable query seam. `params` are positional (`$1`, `$2`, ...). Tests
|
||||
* supply a fixture; `makePostgresQueryImpl` supplies the real thing.
|
||||
*/
|
||||
export type QueryImpl = (sql: string, params: unknown[]) => Promise<SourceRow[]>;
|
||||
|
||||
export interface PostgresWatermarkConnectorConfig extends ConnectorConfig {
|
||||
/** Fully-qualified source table, e.g. "app.dealer_stock_entry". */
|
||||
table: string;
|
||||
/** Logical partition name exposed to the runner — a dataset name, never a physical detail leak beyond `table` itself. */
|
||||
partition_key: string;
|
||||
/** Explicit column allow-list. Never "*". Must include `record_key_column` and `watermark_column`. */
|
||||
columns: string[];
|
||||
record_key_column: string;
|
||||
watermark_column: string;
|
||||
/** Declared record shape, fed to the schema registry (P063). */
|
||||
schema: RecordSchemaFields;
|
||||
}
|
||||
|
||||
/** The 98M-row table this connector must never be pointed at. See file header. */
|
||||
const FORBIDDEN_TABLE_PATTERN = /(^|\.)vehicle$/i;
|
||||
|
||||
function isConfig(config: ConnectorConfig): config is PostgresWatermarkConnectorConfig {
|
||||
return typeof config.table === "string" && typeof config.partition_key === "string";
|
||||
}
|
||||
|
||||
/** Cursor vocabulary: JSON `{last_updated_at, last_id, checksum}` — see docs/P070 for the checkpoint field mapping. */
|
||||
export interface WatermarkCursor {
|
||||
last_updated_at: string;
|
||||
last_id: string;
|
||||
/** sha256 of the page just produced, hex-encoded. Absent only for the "never read" case. */
|
||||
checksum?: string;
|
||||
}
|
||||
|
||||
export function encodeCursor(cursor: WatermarkCursor): string {
|
||||
return JSON.stringify(cursor);
|
||||
}
|
||||
|
||||
export function decodeCursor(raw: string | undefined): WatermarkCursor | undefined {
|
||||
if (raw === undefined) return undefined;
|
||||
const parsed = JSON.parse(raw) as unknown;
|
||||
if (
|
||||
typeof parsed !== "object" ||
|
||||
parsed === null ||
|
||||
typeof (parsed as WatermarkCursor).last_updated_at !== "string" ||
|
||||
typeof (parsed as WatermarkCursor).last_id !== "string"
|
||||
) {
|
||||
throw new Error(`invalid watermark cursor "${raw}"`);
|
||||
}
|
||||
return parsed as WatermarkCursor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Postgres drivers (postgres.js included) hand back `timestamptz` columns as
|
||||
* native JS `Date` objects, not strings. `String(date)` produces the
|
||||
* locale/timezone-dependent `Date.toString()` form ("Thu Sep 03 2026
|
||||
* 02:00:00 GMT+0200 (...)") — useless as a stored watermark, and NOT what a
|
||||
* fixture-based test (which hands back plain ISO strings) would ever catch.
|
||||
* Found by running this connector against a real Postgres container, not by
|
||||
* the fixture-only unit tests — which is why this helper exists instead of
|
||||
* an inline `String(...)` at each call site.
|
||||
*/
|
||||
function watermarkValueToIso(value: unknown): string {
|
||||
return value instanceof Date ? value.toISOString() : String(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* The P063 schema-conformance gate (src/schema/compatibility.ts) checks a
|
||||
* payload's field VALUES against the declared schema — and even its
|
||||
* `"timestamp"` type requires an already-ISO-string value, never a raw
|
||||
* `Date`. A real Postgres driver hands back `timestamptz`/`date` columns as
|
||||
* `Date` objects, so a payload built straight from `queryImpl`'s rows fails
|
||||
* conformance the moment it reaches a real database — caught by running
|
||||
* this connector against a real Postgres container (see docs/P070's
|
||||
* verification log), not by the fixture-only unit tests, which all use
|
||||
* plain strings. Every row is normalized through this function before it
|
||||
* becomes a `ConnectorRecord` payload, so the envelope's payload is always
|
||||
* JSON-safe and schema-checkable, independent of which driver produced it.
|
||||
*/
|
||||
function normalizeRow(row: SourceRow): SourceRow {
|
||||
const normalized: SourceRow = {};
|
||||
for (const [field, value] of Object.entries(row)) {
|
||||
normalized[field] = value instanceof Date ? value.toISOString() : value;
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
/** sha256 over a stable JSON encoding of the rows, hex-encoded. Deterministic regardless of object key insertion order upstream. */
|
||||
export function checksumRows(rows: SourceRow[]): string {
|
||||
const stable = rows.map((row) =>
|
||||
Object.fromEntries(Object.keys(row).sort().map((k) => [k, row[k]])),
|
||||
);
|
||||
return createHash("sha256").update(JSON.stringify(stable)).digest("hex");
|
||||
}
|
||||
|
||||
export class PostgresWatermarkConnector implements Connector {
|
||||
readonly connector_id = "postgres_watermark";
|
||||
readonly connector_version = POSTGRES_WATERMARK_CONNECTOR_VERSION;
|
||||
|
||||
private config?: PostgresWatermarkConnectorConfig;
|
||||
private ctx?: ConnectorContext;
|
||||
private lastCursor = new Map<string, string | undefined>();
|
||||
private connected = false;
|
||||
|
||||
constructor(private readonly queryImpl: QueryImpl) {}
|
||||
|
||||
validate(config: ConnectorConfig): ValidationResult {
|
||||
const errors: string[] = [];
|
||||
if (typeof config.table !== "string" || config.table.length === 0) {
|
||||
errors.push("table is required");
|
||||
} else if (FORBIDDEN_TABLE_PATTERN.test(config.table)) {
|
||||
errors.push(
|
||||
`table "${config.table}" looks like the 98M-row vehicle catalog table — this connector is for watermark-polled ` +
|
||||
`OLTP tables only; use PostgresKeyedLookupConnector for narrow, keyed vehicle attribute lookups instead`,
|
||||
);
|
||||
}
|
||||
if (typeof config.partition_key !== "string" || config.partition_key.length === 0) {
|
||||
errors.push("partition_key is required");
|
||||
}
|
||||
const columns = config.columns;
|
||||
if (!Array.isArray(columns) || columns.length === 0) {
|
||||
errors.push("columns is required and must be a non-empty explicit column list");
|
||||
} else if (columns.some((c) => c === "*" || typeof c !== "string" || c.length === 0)) {
|
||||
errors.push('columns must not contain "*" or an empty entry — SELECT * is not permitted');
|
||||
}
|
||||
if (typeof config.record_key_column !== "string" || config.record_key_column.length === 0) {
|
||||
errors.push("record_key_column is required");
|
||||
} else if (Array.isArray(columns) && !columns.includes(config.record_key_column)) {
|
||||
errors.push("record_key_column must be included in columns");
|
||||
}
|
||||
if (typeof config.watermark_column !== "string" || config.watermark_column.length === 0) {
|
||||
errors.push("watermark_column is required");
|
||||
} else if (Array.isArray(columns) && !columns.includes(config.watermark_column)) {
|
||||
errors.push("watermark_column must be included in columns");
|
||||
}
|
||||
const schema = config.schema;
|
||||
if (typeof schema !== "object" || schema === null || Object.keys(schema).length === 0) {
|
||||
errors.push("schema is required and must declare at least one field");
|
||||
}
|
||||
return { valid: errors.length === 0, errors };
|
||||
}
|
||||
|
||||
async connect(config: ConnectorConfig, ctx: ConnectorContext): Promise<void> {
|
||||
const verdict = this.validate(config);
|
||||
if (!verdict.valid) {
|
||||
throw new Error(`postgres_watermark connector config invalid: ${verdict.errors.join("; ")}`);
|
||||
}
|
||||
if (!isConfig(config)) throw new Error("postgres_watermark connector config invalid");
|
||||
this.config = config;
|
||||
this.ctx = ctx;
|
||||
this.connected = true;
|
||||
}
|
||||
|
||||
async discover(): Promise<DiscoveryResult> {
|
||||
const config = this.requireSession("discover");
|
||||
return {
|
||||
partitions: [{ partition_key: config.partition_key, name: config.table, schema: config.schema }],
|
||||
// A polled table is batch/stream, never cdc: this connector never emits deletes.
|
||||
modes: ["batch", "stream"],
|
||||
};
|
||||
}
|
||||
|
||||
async health(): Promise<ConnectorHealth> {
|
||||
const config = this.requireSession("health");
|
||||
const started = Date.now();
|
||||
try {
|
||||
await this.queryImpl(`SELECT 1 FROM ${config.table} LIMIT 0`, []);
|
||||
return { status: "up", latency_ms: Date.now() - started };
|
||||
} catch (err) {
|
||||
return {
|
||||
status: "down",
|
||||
latency_ms: Date.now() - started,
|
||||
detail: err instanceof Error ? err.message : "unreachable",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async read(req: ReadRequest): Promise<ReadResult> {
|
||||
const config = this.requireSession("read");
|
||||
const cursor = decodeCursor(req.cursor);
|
||||
const columnList = config.columns.join(", ");
|
||||
let rows: SourceRow[];
|
||||
if (cursor === undefined) {
|
||||
rows = await this.queryImpl(
|
||||
`SELECT ${columnList} FROM ${config.table} ORDER BY ${config.watermark_column}, ${config.record_key_column} LIMIT $1`,
|
||||
[req.limit],
|
||||
);
|
||||
} else {
|
||||
rows = await this.queryImpl(
|
||||
`SELECT ${columnList} FROM ${config.table} ` +
|
||||
`WHERE ${config.watermark_column} > $1 OR (${config.watermark_column} = $1 AND ${config.record_key_column} > $2) ` +
|
||||
`ORDER BY ${config.watermark_column}, ${config.record_key_column} LIMIT $3`,
|
||||
[cursor.last_updated_at, cursor.last_id, req.limit],
|
||||
);
|
||||
}
|
||||
// Hard bound — a queryImpl that over-delivers (a fixture bug, or a
|
||||
// future real driver quirk) is truncated here rather than silently
|
||||
// widening the caller's page.
|
||||
rows = rows.slice(0, req.limit);
|
||||
|
||||
const records: ConnectorRecord[] = rows.map((row) => this.toRecord(row, config));
|
||||
let nextCursor = req.cursor;
|
||||
if (rows.length > 0) {
|
||||
const last = rows[rows.length - 1];
|
||||
const nextWatermark: WatermarkCursor = {
|
||||
last_updated_at: watermarkValueToIso(last[config.watermark_column]),
|
||||
last_id: String(last[config.record_key_column]),
|
||||
checksum: checksumRows(rows),
|
||||
};
|
||||
nextCursor = encodeCursor(nextWatermark);
|
||||
this.lastCursor.set(req.partition_key, nextCursor);
|
||||
}
|
||||
return {
|
||||
records,
|
||||
next_cursor: nextCursor,
|
||||
// Fewer rows than the limit means we caught up to "now" — there may be
|
||||
// MORE later (this is a live table), but nothing more RIGHT NOW.
|
||||
exhausted: rows.length < req.limit,
|
||||
};
|
||||
}
|
||||
|
||||
async checkpoint(partitionKey: string): Promise<{ partition_key: string; cursor?: string }> {
|
||||
this.requireSession("checkpoint");
|
||||
return { partition_key: partitionKey, cursor: this.lastCursor.get(partitionKey) };
|
||||
}
|
||||
|
||||
async resume(checkpoint: IngestionCheckpoint, limit: number): Promise<ReadResult> {
|
||||
this.requireSession("resume");
|
||||
return this.read({ partition_key: checkpoint.partition_key, cursor: checkpoint.cursor, limit });
|
||||
}
|
||||
|
||||
async backfill(req: BackfillRequest): Promise<ReadResult> {
|
||||
const config = this.requireSession("backfill");
|
||||
if (!req.from || !req.to) {
|
||||
throw new Error("backfill requires both `from` and `to` — an unbounded backfill is a full reload, not a backfill");
|
||||
}
|
||||
const columnList = config.columns.join(", ");
|
||||
const rows = (
|
||||
await this.queryImpl(
|
||||
`SELECT ${columnList} FROM ${config.table} ` +
|
||||
`WHERE ${config.watermark_column} >= $1 AND ${config.watermark_column} < $2 ` +
|
||||
`ORDER BY ${config.watermark_column}, ${config.record_key_column} LIMIT $3`,
|
||||
[req.from, req.to, req.limit],
|
||||
)
|
||||
).slice(0, req.limit);
|
||||
return {
|
||||
records: rows.map((row) => this.toRecord(row, config)),
|
||||
next_cursor: undefined,
|
||||
exhausted: true,
|
||||
};
|
||||
}
|
||||
|
||||
async disconnect(): Promise<void> {
|
||||
this.config = undefined;
|
||||
this.ctx = undefined;
|
||||
this.lastCursor.clear();
|
||||
this.connected = false;
|
||||
}
|
||||
|
||||
// --- internals -------------------------------------------------------------
|
||||
|
||||
private toRecord(row: SourceRow, config: PostgresWatermarkConnectorConfig): ConnectorRecord {
|
||||
const key = row[config.record_key_column];
|
||||
if (typeof key !== "string" && typeof key !== "number") {
|
||||
throw new Error(`record is missing string/number key column "${config.record_key_column}"`);
|
||||
}
|
||||
const watermark = watermarkValueToIso(row[config.watermark_column]);
|
||||
return {
|
||||
record_key: String(key),
|
||||
payload: normalizeRow(row),
|
||||
operation: "upsert",
|
||||
occurred_at: watermark,
|
||||
cursor: encodeCursor({ last_updated_at: watermark, last_id: String(key) }),
|
||||
};
|
||||
}
|
||||
|
||||
private requireSession(op: string): PostgresWatermarkConnectorConfig {
|
||||
if (!this.connected || !this.config) throw new ConnectorNotConnectedError(this.connector_id, op);
|
||||
return this.config;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
/**
|
||||
* src/ingestion/projection-sink.ts
|
||||
* ===================================
|
||||
* Adapts the P062 runner's generic `IngestionSink` (envelopes in) onto
|
||||
* `SiaxAnalyticsEngine.ingestProjection` (BI-P009 capability 7, writes
|
||||
* `opt_dataset_projection` in ClickHouse — see `src/analytics/engine/
|
||||
* clickhouse-engine.ts`).
|
||||
*
|
||||
* WHY `ingestProjection` / `opt_dataset_projection` AND NOT `opt_analytics_
|
||||
* event`. `opt_analytics_event` (BI-P010 schema.ts) is documented as a
|
||||
* "high-volume per-tenant EVENT stream" with a 30-day TTL — the right shape
|
||||
* for discrete occurrences (a page view, a search, a status transition).
|
||||
* `dealer_stock_entry`/`listing`/`deal` rows and vehicle-dimension rows are
|
||||
* ENTITY SNAPSHOTS — current state of a stock entry, a listing, a deal, a
|
||||
* vehicle — which is exactly what `opt_dataset_projection` (ReplacingMerge
|
||||
* Tree serving tier, `entity_key`+`version`) already exists to hold, and
|
||||
* `ingestProjection` is the one write port `SiaxAnalyticsEngine` exposes for
|
||||
* it. Landing entity rows into the raw event stream would be forcing an
|
||||
* entity shape through an event shape — this sink does not do that.
|
||||
*
|
||||
* THE FIREWALL IS RESPECTED BY CONSTRUCTION: this file imports only the
|
||||
* `SiaxAnalyticsEngine`/`AnalyticsIdentity`/`ProjectionRow` TYPES from
|
||||
* `../analytics/engine/siax-analytics-engine` — no ClickHouse client, no
|
||||
* `CLICKHOUSE_*` env read, no connection string. Which concrete engine
|
||||
* (ClickHouse today, anything else later) answers `ingestProjection` is the
|
||||
* caller's concern (see `scripts/vehiq-oltp-ingest/run.ts`), not this file's.
|
||||
*
|
||||
* ORDERING STAYS THE RUNNER'S: this function is called AS the runner's
|
||||
* `sink`, so its own crash/partial-failure behaviour is exactly what
|
||||
* `ingestion-runner.ts`'s header already documents — a thrown error here
|
||||
* leaves the page's checkpoint uncommitted, and the next run re-reads it.
|
||||
*/
|
||||
import type { IngestionEnvelope } from "../types/ingestion";
|
||||
import type {
|
||||
AnalyticsIdentity,
|
||||
ProjectionRow,
|
||||
SiaxAnalyticsEngine,
|
||||
} from "../analytics/engine/siax-analytics-engine";
|
||||
import { SiaxError } from "../platform/errors";
|
||||
|
||||
export interface ProjectionSinkOptions {
|
||||
engine: SiaxAnalyticsEngine;
|
||||
identity: AnalyticsIdentity;
|
||||
/** Maps a partition_key (e.g. "dealer_stock_entry") to a logical dataset id (e.g. "vehiq.dealer_stock_entry"). */
|
||||
datasetIdFor: (partitionKey: string) => string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits an envelope's payload into ClickHouse-projection-friendly
|
||||
* attributes (strings) and measures (numbers). `null`/`undefined` fields are
|
||||
* dropped rather than coerced to `"null"`/`NaN`, which would fabricate a
|
||||
* value nothing upstream actually asserted.
|
||||
*/
|
||||
export function toProjectionRow(envelope: IngestionEnvelope): ProjectionRow {
|
||||
if (envelope.operation === "delete" || envelope.payload === null) {
|
||||
throw new SiaxError(
|
||||
"INVALID_REQUEST",
|
||||
"0pt.ingestion.projection-sink",
|
||||
`envelope ${envelope.envelope_id} is a delete (payload: null) — opt_dataset_projection has no tombstone ` +
|
||||
`representation via ingestProjection today; a CDC-capable source reaching this sink is a real gap, not ` +
|
||||
`silently handled here (this connector programme currently emits "upsert" only, so this path is untested ` +
|
||||
`by design, not unreachable by contract)`,
|
||||
);
|
||||
}
|
||||
const attributes: Record<string, string> = {};
|
||||
const measures: Record<string, number> = {};
|
||||
for (const [field, value] of Object.entries(envelope.payload)) {
|
||||
if (value === null || value === undefined) continue;
|
||||
if (typeof value === "number" && Number.isFinite(value)) {
|
||||
measures[field] = value;
|
||||
} else if (value instanceof Date) {
|
||||
attributes[field] = value.toISOString();
|
||||
} else if (typeof value === "object") {
|
||||
attributes[field] = JSON.stringify(value);
|
||||
} else {
|
||||
attributes[field] = String(value);
|
||||
}
|
||||
}
|
||||
return {
|
||||
entity_key: envelope.record_key,
|
||||
as_of: envelope.occurred_at ?? envelope.ingested_at,
|
||||
version: envelope.sequence,
|
||||
attributes,
|
||||
measures,
|
||||
};
|
||||
}
|
||||
|
||||
export function projectionSink(
|
||||
opts: ProjectionSinkOptions,
|
||||
): (envelopes: IngestionEnvelope[]) => Promise<void> {
|
||||
return async (envelopes: IngestionEnvelope[]): Promise<void> => {
|
||||
if (envelopes.length === 0) return;
|
||||
const byPartition = new Map<string, IngestionEnvelope[]>();
|
||||
for (const envelope of envelopes) {
|
||||
const list = byPartition.get(envelope.partition_key) ?? [];
|
||||
list.push(envelope);
|
||||
byPartition.set(envelope.partition_key, list);
|
||||
}
|
||||
for (const [partitionKey, group] of byPartition) {
|
||||
const rows = group.map(toProjectionRow);
|
||||
const dataset_id = opts.datasetIdFor(partitionKey);
|
||||
const result = await opts.engine.ingestProjection(opts.identity, { dataset_id, rows });
|
||||
if (result.ingested !== rows.length) {
|
||||
// Partial ingest is a hard failure, on purpose: the runner only
|
||||
// commits the checkpoint AFTER this sink resolves. Resolving
|
||||
// "successfully" on a partial write would silently drop the
|
||||
// un-ingested rows on the next run's resume.
|
||||
throw new SiaxError(
|
||||
"RETRYABLE_FAILURE",
|
||||
"0pt.ingestion.projection-sink",
|
||||
`ingestProjection for dataset "${dataset_id}" reported ${result.ingested}/${rows.length} rows ingested — ` +
|
||||
`treating as a failed page so the checkpoint does not advance past unlanded rows`,
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,343 @@
|
||||
/**
|
||||
* src/ingestion/vehiq-oltp-ingest-runner.ts
|
||||
* ============================================
|
||||
* BI-3/BI-4 — the controlled, incremental VehIQ OLTP -> 0pt ClickHouse path,
|
||||
* as Simon specified it (`admin/vehiq` 2026-09-25 pass, "Data path for
|
||||
* inventory.days_in_stock"):
|
||||
*
|
||||
* OLTP operational tables -> read-only analytical extraction ->
|
||||
* watermark/updated_at -> 0pt ingestion -> ClickHouse
|
||||
*
|
||||
* WHY THIS LIVES IN `admin/0pt`, NOT `admin/vehiq`. ADR-0035 (admin/vehiq,
|
||||
* decisions 1/2/10, CI-enforced by `tools/validate_engine_firewall.py`):
|
||||
* VehIQ's own codebase must NEVER hold a ClickHouse credential or import a
|
||||
* ClickHouse client, and product code must never hammer the live OLTP
|
||||
* primary with analytical queries directly. `admin/vehiq`'s own hub
|
||||
* (`docs/CURRENT_STATE.md`, "Connection details for a future API0
|
||||
* connector") says this explicitly: "VehIQ's own codebase must never see
|
||||
* any of this. This is 0pt's data plane; only a future `admin/api0`
|
||||
* connector should hold these details." This repo is 0pt's data plane —
|
||||
* the correct home per that same guidance (a sibling of `admin/api0`, not
|
||||
* `admin/api0` itself, since the read/write mechanics belong next to the
|
||||
* P060-P068 ingestion plane and the BI-P009/P010 ClickHouse engine that
|
||||
* already live here).
|
||||
*
|
||||
* TWO ENTRY POINTS, DELIBERATELY SEPARATE:
|
||||
*
|
||||
* `previewVehiqOltpSource` — connect, discover, ONE bounded read. Never
|
||||
* calls checkpoint()/commits anything, never runs the P062 runner. Safe
|
||||
* to point at production at any time, table empty or not: nothing it does
|
||||
* can advance a checkpoint or land a row. This is what proves "zero
|
||||
* batches, zero writes" without deploying a live scheduled service.
|
||||
*
|
||||
* `runVehiqOltpIngestion` — the real, scheduled path: the P062
|
||||
* `IngestionRunner` across all three OLTP sources, then the narrow
|
||||
* `dimension_vehicle` lookup keyed on exactly the `vehicle_id`s the OLTP
|
||||
* batch referenced. This is what `.gitea/workflows/vehiq-oltp-ingest.yml`
|
||||
* runs — see that file and docs/P070_VEHIQ_OLTP_EXTRACTOR.md for the
|
||||
* activation gate this stays behind until Simon flips it.
|
||||
*
|
||||
* CHECKPOINT FIELD MAPPING (Simon's own list: source, table, last_updated_at,
|
||||
* last_id, batch_count, checksum, ingested_at) onto the canonical P062
|
||||
* `IngestionCheckpoint` (source_id, tenant_id, partition_key, cursor,
|
||||
* sequence, records_committed, committed_at) — deliberately reusing the
|
||||
* existing durable checkpoint mechanism rather than building a second one:
|
||||
*
|
||||
* source -> checkpoint.source_id
|
||||
* table -> checkpoint.partition_key
|
||||
* last_updated_at-> decoded from checkpoint.cursor (WatermarkCursor)
|
||||
* last_id -> decoded from checkpoint.cursor (WatermarkCursor)
|
||||
* checksum -> decoded from checkpoint.cursor (WatermarkCursor)
|
||||
* batch_count -> checkpoint.records_committed (cumulative) / a
|
||||
* per-run page count is also in each PartitionRunReport
|
||||
* ingested_at -> checkpoint.committed_at
|
||||
*/
|
||||
import type { IngestionRepository } from "../repositories/ingestion-repository";
|
||||
import type { SchemaRegistry } from "../schema/schema-registry";
|
||||
import type { LineageStore } from "../lineage/lineage-store";
|
||||
import type { RecordSchemaFields } from "../schema/record-schema";
|
||||
import { IngestionRunner, type IngestionRunReport, type IngestionSink } from "./ingestion-runner";
|
||||
import {
|
||||
PostgresWatermarkConnector,
|
||||
decodeCursor,
|
||||
type QueryImpl,
|
||||
} from "../connectors/postgres-watermark-connector";
|
||||
import { PostgresKeyedLookupConnector } from "../connectors/postgres-keyed-lookup-connector";
|
||||
import type { IngestionEnvelope } from "../types/ingestion";
|
||||
|
||||
export interface VehiqOltpSourceDef {
|
||||
/** e.g. "dealer_stock_entry" — also the logical dataset name suffix. */
|
||||
partition_key: "dealer_stock_entry" | "deal";
|
||||
/** Fully-qualified source table, e.g. "app.dealer_stock_entry". */
|
||||
table: string;
|
||||
columns: string[];
|
||||
record_key_column: string;
|
||||
watermark_column: string;
|
||||
schema: RecordSchemaFields;
|
||||
}
|
||||
|
||||
/**
|
||||
* TWO sources, not three — READ THIS BEFORE ASSUMING `app.listing` IS
|
||||
* COVERED. The task's own wording (and `admin/vehiq`'s `docs/CURRENT_STATE.
|
||||
* md`, "the columns already exist") assumed all three OLTP tables carry a
|
||||
* generic `updated_at`. Verified LIVE against production (`server10`,
|
||||
* `vehiq-db`, read-only `information_schema.columns`, 2026-09-25) that
|
||||
* this is only true for none of the three as a literal `updated_at` column
|
||||
* — and NOT true at all for `app.listing`:
|
||||
*
|
||||
* app.dealer_stock_entry — id, tenant_id, vehicle_id, lot_location,
|
||||
* floorplan_status, listed_at, sold_at. No `updated_at`, no `status`
|
||||
* (it's `floorplan_status`). `listed_at` (NOT NULL) is the best
|
||||
* available watermark — see the caveat below.
|
||||
* app.deal — id, tenant_id, party_id, vehicle_id, sale_price_minor,
|
||||
* currency, status, created_at. No `updated_at`. `created_at`
|
||||
* (NOT NULL DEFAULT now()) is the best available watermark.
|
||||
* app.listing — id, tenant_id, vehicle_id, asking_price_minor, currency,
|
||||
* channels, status. NO timestamp column of any kind exists today.
|
||||
* `admin/vehiq` migration 0084 (`published_at`/`unpublished_at`) is
|
||||
* written but NOT YET APPLIED to production (verified: those columns
|
||||
* are absent live) — until it lands, there is no honest way to build a
|
||||
* watermark connector for `app.listing` without inventing a column that
|
||||
* does not exist. Not built here — see docs/P070's "Deferred: app.
|
||||
* listing extraction" section for exactly what unblocks it.
|
||||
*
|
||||
* HONEST CAVEAT ON THE TWO SOURCES THAT ARE HERE: `listed_at`/`created_at`
|
||||
* are set ONCE, at insert. Neither table has a column that bumps when an
|
||||
* EXISTING row changes (`sold_at`/`floorplan_status` on dealer_stock_entry;
|
||||
* `status` on deal). This connector will therefore reliably see every NEW
|
||||
* row, but will NOT see an in-place UPDATE to an existing row as "changed" —
|
||||
* it has already passed the watermark and will not be re-read. This is the
|
||||
* same gap a real `updated_at` column (plus a trigger, e.g. `0084`-style)
|
||||
* would close, and is exactly why building a permanent extractor deserves
|
||||
* that schema decision rather than silently working around its absence.
|
||||
* `docs/P070_VEHIQ_OLTP_EXTRACTOR.md` documents this as the #1 thing to
|
||||
* settle before leaning on this extractor for anything beyond "count of new
|
||||
* rows since X".
|
||||
*/
|
||||
export const VEHIQ_OLTP_SOURCES: VehiqOltpSourceDef[] = [
|
||||
{
|
||||
partition_key: "dealer_stock_entry",
|
||||
table: "app.dealer_stock_entry",
|
||||
columns: ["id", "tenant_id", "vehicle_id", "lot_location", "floorplan_status", "listed_at", "sold_at"],
|
||||
record_key_column: "id",
|
||||
watermark_column: "listed_at",
|
||||
schema: {
|
||||
id: { type: "string", required: true },
|
||||
tenant_id: { type: "string", required: true },
|
||||
vehicle_id: { type: "string", required: true },
|
||||
lot_location: { type: "string", required: true },
|
||||
floorplan_status: { type: "string", required: true },
|
||||
listed_at: { type: "timestamp", required: true },
|
||||
sold_at: { type: "timestamp", required: false },
|
||||
},
|
||||
},
|
||||
{
|
||||
partition_key: "deal",
|
||||
table: "app.deal",
|
||||
columns: ["id", "tenant_id", "party_id", "vehicle_id", "sale_price_minor", "currency", "status", "created_at"],
|
||||
record_key_column: "id",
|
||||
watermark_column: "created_at",
|
||||
schema: {
|
||||
id: { type: "string", required: true },
|
||||
tenant_id: { type: "string", required: true },
|
||||
party_id: { type: "string", required: true },
|
||||
vehicle_id: { type: "string", required: true },
|
||||
sale_price_minor: { type: "number", required: true },
|
||||
currency: { type: "string", required: true },
|
||||
status: { type: "string", required: true },
|
||||
created_at: { type: "timestamp", required: true },
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
/**
|
||||
* `vehiq.vehicle`'s primary key column is `vehicle_id`, NOT `id` — verified
|
||||
* live (information_schema.columns, read-only, no data scanned) 2026-09-25.
|
||||
* Column list otherwise matches what BI-P010/VEHIQ_ANALYTICS_BASELINE.md
|
||||
* already documented (make/model/model_year/fuel_type all present and
|
||||
* correctly named).
|
||||
*/
|
||||
export const VEHIQ_VEHICLE_DIMENSION_TABLE = "vehiq.vehicle";
|
||||
export const VEHIQ_VEHICLE_DIMENSION_RECORD_KEY_COLUMN = "vehicle_id";
|
||||
export const VEHIQ_VEHICLE_DIMENSION_COLUMNS = ["vehicle_id", "make", "model", "model_year", "fuel_type"];
|
||||
export const VEHIQ_VEHICLE_DIMENSION_SCHEMA: RecordSchemaFields = {
|
||||
vehicle_id: { type: "string", required: true },
|
||||
make: { type: "string", required: false },
|
||||
model: { type: "string", required: false },
|
||||
model_year: { type: "number", required: false },
|
||||
fuel_type: { type: "string", required: false },
|
||||
};
|
||||
|
||||
// --- preview: read-only, never commits, safe against production at any time ---
|
||||
|
||||
export interface PreviewResult {
|
||||
partition_key: string;
|
||||
table: string;
|
||||
/** Records this ONE bounded read would produce right now. */
|
||||
would_read: number;
|
||||
exhausted: boolean;
|
||||
checkpoint_before: string | undefined;
|
||||
next_cursor: string | undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* ONE bounded connect -> discover -> read(existing checkpoint, limit), then
|
||||
* disconnect. Never calls IngestionRunner, never calls connector.checkpoint()
|
||||
* against the repo, never writes anything anywhere. This is intentionally
|
||||
* the ONLY function in this file that is safe to point at a real production
|
||||
* source before the extractor is "activated" (see docs/P070).
|
||||
*/
|
||||
export async function previewVehiqOltpSource(
|
||||
source: VehiqOltpSourceDef,
|
||||
query: QueryImpl,
|
||||
repo: Pick<IngestionRepository, "getCheckpoint">,
|
||||
opts: { sourceId: string; tenantId: string; projectId: string; limit?: number } = {
|
||||
sourceId: "vehiq_oltp",
|
||||
tenantId: "t_vehiq",
|
||||
projectId: "vehiq",
|
||||
},
|
||||
): Promise<PreviewResult> {
|
||||
const connector = new PostgresWatermarkConnector(query);
|
||||
const config = {
|
||||
table: source.table,
|
||||
partition_key: source.partition_key,
|
||||
columns: source.columns,
|
||||
record_key_column: source.record_key_column,
|
||||
watermark_column: source.watermark_column,
|
||||
schema: source.schema,
|
||||
};
|
||||
await connector.connect(config, {
|
||||
tenant_id: opts.tenantId,
|
||||
project_id: opts.projectId,
|
||||
source_id: opts.sourceId,
|
||||
run_id: `preview_${Date.now()}`,
|
||||
});
|
||||
try {
|
||||
const existing = await repo.getCheckpoint(opts.sourceId, source.partition_key);
|
||||
const result = await connector.read({
|
||||
partition_key: source.partition_key,
|
||||
cursor: existing?.cursor,
|
||||
limit: opts.limit ?? 100,
|
||||
});
|
||||
return {
|
||||
partition_key: source.partition_key,
|
||||
table: source.table,
|
||||
would_read: result.records.length,
|
||||
exhausted: result.exhausted,
|
||||
checkpoint_before: existing?.cursor,
|
||||
next_cursor: result.next_cursor,
|
||||
};
|
||||
} finally {
|
||||
await connector.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
// --- real run: the P062 highway, scheduled/activated path ---------------------
|
||||
|
||||
export interface VehiqOltpIngestDeps {
|
||||
repo: IngestionRepository;
|
||||
schemas: SchemaRegistry;
|
||||
lineage?: LineageStore;
|
||||
/** Read-only query against VehIQ's `vehiq-db` (server10) — see makePostgresQueryImpl in the calling script for the real implementation. */
|
||||
sourceQuery: QueryImpl;
|
||||
/** Where OLTP-entity envelopes land (see src/ingestion/projection-sink.ts). */
|
||||
oltpSink: IngestionSink;
|
||||
/** Where the narrow vehicle-dimension envelopes land. Same sink shape; kept separate so the two datasets can be routed differently if ever needed. */
|
||||
dimensionSink: IngestionSink;
|
||||
tenantId: string;
|
||||
projectId: string;
|
||||
sourceIdPrefix?: string;
|
||||
pageSize?: number;
|
||||
maxPages?: number;
|
||||
}
|
||||
|
||||
export interface VehiqOltpIngestSummary {
|
||||
oltp: IngestionRunReport[];
|
||||
dimension?: IngestionRunReport;
|
||||
referenced_vehicle_ids: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the three watermark sources through the P062 runner, then derives the
|
||||
* narrow vehicle-dimension key set from exactly what THIS run's
|
||||
* `dealer_stock_entry` batch referenced (never a fresh scan, never the full
|
||||
* 98M-row table) and runs that through the runner too, if there is anything
|
||||
* to look up. When there is nothing new (today's actual production state:
|
||||
* all three OLTP tables are empty), this function runs, reports 0 records
|
||||
* read for every source, calls the dimension sink zero times, and returns
|
||||
* cleanly — the empty-table case is a normal, successful, no-op run, not a
|
||||
* special case that needs its own error handling.
|
||||
*/
|
||||
export async function runVehiqOltpIngestion(deps: VehiqOltpIngestDeps): Promise<VehiqOltpIngestSummary> {
|
||||
const runner = new IngestionRunner(deps.repo, deps.schemas, deps.lineage);
|
||||
const sourceIdPrefix = deps.sourceIdPrefix ?? "vehiq_oltp";
|
||||
const oltpReports: IngestionRunReport[] = [];
|
||||
const referencedVehicleIds = new Set<string>();
|
||||
|
||||
for (const source of VEHIQ_OLTP_SOURCES) {
|
||||
const seen: IngestionEnvelope[] = [];
|
||||
const wrappedSink: IngestionSink = async (envelopes) => {
|
||||
seen.push(...envelopes);
|
||||
await deps.oltpSink(envelopes);
|
||||
};
|
||||
const connector = new PostgresWatermarkConnector(deps.sourceQuery);
|
||||
const report = await runner.run({
|
||||
source_id: `${sourceIdPrefix}_${source.partition_key}`,
|
||||
tenant_id: deps.tenantId,
|
||||
project_id: deps.projectId,
|
||||
mode: "batch",
|
||||
connector,
|
||||
config: {
|
||||
table: source.table,
|
||||
partition_key: source.partition_key,
|
||||
columns: source.columns,
|
||||
record_key_column: source.record_key_column,
|
||||
watermark_column: source.watermark_column,
|
||||
schema: source.schema,
|
||||
},
|
||||
sink: wrappedSink,
|
||||
page_size: deps.pageSize ?? 200,
|
||||
max_pages: deps.maxPages ?? 1000,
|
||||
output_ref: `0pt:dataset:vehiq.${source.partition_key}`,
|
||||
});
|
||||
oltpReports.push(report);
|
||||
|
||||
if (source.partition_key === "dealer_stock_entry") {
|
||||
for (const envelope of seen) {
|
||||
const vehicleId = envelope.payload?.["vehicle_id"];
|
||||
if (typeof vehicleId === "string" && vehicleId.length > 0) referencedVehicleIds.add(vehicleId);
|
||||
else if (typeof vehicleId === "number") referencedVehicleIds.add(String(vehicleId));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let dimensionReport: IngestionRunReport | undefined;
|
||||
if (referencedVehicleIds.size > 0) {
|
||||
const keys = [...referencedVehicleIds];
|
||||
const dimensionConnector = new PostgresKeyedLookupConnector(deps.sourceQuery);
|
||||
dimensionReport = await runner.run({
|
||||
source_id: `${sourceIdPrefix}_dimension_vehicle`,
|
||||
tenant_id: deps.tenantId,
|
||||
project_id: deps.projectId,
|
||||
mode: "batch",
|
||||
connector: dimensionConnector,
|
||||
config: {
|
||||
table: VEHIQ_VEHICLE_DIMENSION_TABLE,
|
||||
partition_key: "dimension_vehicle",
|
||||
columns: VEHIQ_VEHICLE_DIMENSION_COLUMNS,
|
||||
record_key_column: VEHIQ_VEHICLE_DIMENSION_RECORD_KEY_COLUMN,
|
||||
schema: VEHIQ_VEHICLE_DIMENSION_SCHEMA,
|
||||
keys,
|
||||
},
|
||||
sink: deps.dimensionSink,
|
||||
page_size: deps.pageSize ?? 200,
|
||||
max_pages: deps.maxPages ?? 1000,
|
||||
output_ref: "0pt:dataset:vehiq.dimension_vehicle",
|
||||
});
|
||||
}
|
||||
|
||||
return { oltp: oltpReports, dimension: dimensionReport, referenced_vehicle_ids: referencedVehicleIds.size };
|
||||
}
|
||||
|
||||
/** Re-exported so callers (the CLI script, tests) don't need a second import from the connector module. */
|
||||
export { decodeCursor };
|
||||
@@ -0,0 +1,105 @@
|
||||
/**
|
||||
* DES1GN ADR D3 (§31): ImportDispatchPolicy — ren policy-funktion, testad per
|
||||
* kombination (format × bytes × rows, gränsvärden inkluderade), plus route-
|
||||
* kopplingen: rows > policy.maxRows -> 413 n0d_dispatch_required med hint
|
||||
* (descriptor-gate stängd), inline-taket oförändrat.
|
||||
*/
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { NextRequest } from "next/server";
|
||||
import { IMPORT_DISPATCH_POLICY, evaluateImportDispatch } from "@/lib/import-dispatch-policy";
|
||||
|
||||
describe("evaluateImportDispatch — verdict per kombination", () => {
|
||||
it("gränsvärde maxInlineRows: exakt 500 rader -> inline", () => {
|
||||
expect(evaluateImportDispatch({ format: "csv", bytes: 100, rows: IMPORT_DISPATCH_POLICY.maxInlineRows })).toEqual({ verdict: "inline" });
|
||||
});
|
||||
|
||||
it("maxInlineRows < rows <= maxRows -> row_cap_exceeded (inline-taket, oförändrat)", () => {
|
||||
expect(evaluateImportDispatch({ format: "csv", bytes: 100, rows: IMPORT_DISPATCH_POLICY.maxInlineRows + 1 })).toEqual({
|
||||
verdict: "row_cap_exceeded",
|
||||
rows: 501,
|
||||
maxInlineRows: 500,
|
||||
});
|
||||
expect(evaluateImportDispatch({ format: "csv", bytes: 100, rows: IMPORT_DISPATCH_POLICY.maxRows }).verdict).toBe("row_cap_exceeded");
|
||||
});
|
||||
|
||||
it("rows > maxRows -> dispatch_n0d (D3-signalen)", () => {
|
||||
expect(evaluateImportDispatch({ format: "csv", bytes: 100, rows: IMPORT_DISPATCH_POLICY.maxRows + 1 })).toEqual({
|
||||
verdict: "dispatch_n0d",
|
||||
rows: 5001,
|
||||
maxRows: 5000,
|
||||
});
|
||||
});
|
||||
|
||||
it("bytes > maxBytes -> byte_cap_exceeded, oavsett rows", () => {
|
||||
expect(evaluateImportDispatch({ format: "csv", bytes: IMPORT_DISPATCH_POLICY.maxBytes + 1, rows: 1 }).verdict).toBe("byte_cap_exceeded");
|
||||
expect(evaluateImportDispatch({ format: "csv", bytes: IMPORT_DISPATCH_POLICY.maxBytes, rows: 1 }).verdict).toBe("inline");
|
||||
});
|
||||
|
||||
it("format utanför allowlist -> unsupported_format", () => {
|
||||
expect(evaluateImportDispatch({ format: "xlsx", bytes: 10, rows: 1 })).toEqual({ verdict: "unsupported_format", format: "xlsx" });
|
||||
});
|
||||
|
||||
it("precedens: format slår bytes, bytes slår rows", () => {
|
||||
expect(evaluateImportDispatch({ format: "json", bytes: 9e9, rows: 9e9 }).verdict).toBe("unsupported_format");
|
||||
expect(evaluateImportDispatch({ format: "csv", bytes: 9e9, rows: 9e9 }).verdict).toBe("byte_cap_exceeded");
|
||||
});
|
||||
});
|
||||
|
||||
describe("route-koppling: POST /api/orders/import respekterar policyn", () => {
|
||||
const { requireOrderOwnerMock, createOrderMock, ingestEventMock, mockOwner } = vi.hoisted(() => ({
|
||||
requireOrderOwnerMock: vi.fn(),
|
||||
createOrderMock: vi.fn(),
|
||||
ingestEventMock: vi.fn().mockResolvedValue({}),
|
||||
mockOwner: { owner: { tenantId: "tenant-test", subject: "buyer-test" } },
|
||||
}));
|
||||
const _unused = mockOwner;
|
||||
vi.mock("@/src/auth/require-order-owner", () => ({
|
||||
requireOrderOwner: (...args: unknown[]) => requireOrderOwnerMock(...args),
|
||||
}));
|
||||
vi.mock("@/lib/orders-store", () => ({ createOrder: (...args: unknown[]) => createOrderMock(...args) }));
|
||||
vi.mock("@/src/clients/aud0-client", () => ({ createAud0Client: () => ({ ingestEvent: ingestEventMock }) }));
|
||||
|
||||
let POST: typeof import("@/app/api/orders/import/route").POST;
|
||||
let MAX_IMPORT_ROWS: number;
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.clearAllMocks();
|
||||
requireOrderOwnerMock.mockResolvedValue(mockOwner);
|
||||
({ POST, MAX_IMPORT_ROWS } = await import("@/app/api/orders/import/route"));
|
||||
});
|
||||
|
||||
function postReq(csv: string): NextRequest {
|
||||
return new NextRequest(new URL("/api/orders/import", "http://localhost"), {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ mode: "validate", csv }),
|
||||
headers: { "content-type": "application/json" },
|
||||
});
|
||||
}
|
||||
|
||||
const line = "custom,icp,Bolag,10,Engångs,SE";
|
||||
|
||||
it("exakt maxInlineRows rader -> 200 (inline-beteende oförändrat)", async () => {
|
||||
const csv = ["flow,service_type,type,volume,frequency,markets", ...Array.from({ length: MAX_IMPORT_ROWS }, () => line)].join("\n");
|
||||
const res = await POST(postReq(csv));
|
||||
expect(res.status).toBe(200);
|
||||
});
|
||||
|
||||
it("maxRows < rows -> 413 n0d_dispatch_required med UNVERIFIED-hint, ingen skrivning", async () => {
|
||||
const csv = ["flow,service_type,type,volume,frequency,markets", ...Array.from({ length: IMPORT_DISPATCH_POLICY.maxRows + 1 }, () => line)].join("\n");
|
||||
const res = await POST(postReq(csv));
|
||||
expect(res.status).toBe(413);
|
||||
const body = (await res.json()) as { error: { code: string; hint: { capability_id: string; job_type: string; status: string } } };
|
||||
expect(body.error.code).toBe("n0d_dispatch_required");
|
||||
expect(body.error.hint).toEqual({ capability_id: "0pt.import_execute", job_type: "0pt.import_execute_job", status: "UNVERIFIED" });
|
||||
expect(createOrderMock).not.toHaveBeenCalled();
|
||||
expect(ingestEventMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("CSV > maxBytes -> 413 csv_too_large", async () => {
|
||||
const csv = "flow,service_type,type,volume,frequency,markets\n" + "x".repeat(IMPORT_DISPATCH_POLICY.maxBytes);
|
||||
const res = await POST(postReq(csv));
|
||||
expect(res.status).toBe(413);
|
||||
const body = (await res.json()) as { error: { code: string } };
|
||||
expect(body.error.code).toBe("csv_too_large");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,226 @@
|
||||
/**
|
||||
* Spår C — @siax/collaboration-kernelns första produktkonsument:
|
||||
* /api/admin/order-notes (persisted collaboration state på admin-orders).
|
||||
*
|
||||
* Mönstret följer tests/api/anonymous-routes-auth.test.ts +
|
||||
* orders-idempotency.test.ts: route-handlers anropas direkt med handbyggda
|
||||
* NextRequest. Ingen DATABASE_URL i test-miljön => lib/order-notes-store.ts
|
||||
* faller tillbaka på sin dokumenterade in-memory-store (production vägrar
|
||||
* den vägen via resolveStoreMode — se lib/db.ts).
|
||||
*/
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
vi.mock("@/src/auth/require-route-auth", () => ({
|
||||
requireRouteAuth: vi.fn(),
|
||||
}));
|
||||
vi.mock("@/src/clients/aud0-client", () => ({
|
||||
createAud0Client: vi.fn(() => ({ ingestEvent: vi.fn().mockResolvedValue({}) })),
|
||||
}));
|
||||
|
||||
import { requireRouteAuth } from "@/src/auth/require-route-auth";
|
||||
import { createAud0Client } from "@/src/clients/aud0-client";
|
||||
import { GET, PATCH, POST } from "@/app/api/admin/order-notes/route";
|
||||
import { getOrder, createOrder } from "@/lib/orders-store";
|
||||
import {
|
||||
addNote,
|
||||
listNotes,
|
||||
resetOrderNotesMemoryForTests,
|
||||
} from "@/lib/order-notes-store";
|
||||
|
||||
const authed = { tenantId: "tenant-test", actorId: "admin-simon", requestId: "req-1" };
|
||||
|
||||
function mockAuth(ctx = authed) {
|
||||
vi.mocked(requireRouteAuth).mockResolvedValue({ ctx } as never);
|
||||
}
|
||||
|
||||
function req(
|
||||
url: string,
|
||||
init: { method?: string; headers?: Record<string, string>; body?: string } = {},
|
||||
): NextRequest {
|
||||
return new NextRequest(new URL(url, "http://localhost"), init);
|
||||
}
|
||||
|
||||
function jsonReq(url: string, body: unknown, method = "POST"): NextRequest {
|
||||
return req(url, {
|
||||
method,
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
}
|
||||
|
||||
let orderId: string;
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.mocked(requireRouteAuth).mockReset();
|
||||
vi.mocked(createAud0Client).mockClear();
|
||||
resetOrderNotesMemoryForTests();
|
||||
mockAuth();
|
||||
const created = await createOrder("custom", {
|
||||
markets: ["SE"],
|
||||
type: "Bolag",
|
||||
volume: 10,
|
||||
});
|
||||
orderId = created.order.order_id;
|
||||
// Legacy anonymous draft får annoteras av admin — men en tenant-ägd order
|
||||
// behöver token-match. Skapa därför testordern anonym (tenant null) och
|
||||
// testa tenant-guarden separat med en ägd order.
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
resetOrderNotesMemoryForTests();
|
||||
});
|
||||
|
||||
describe("GET /api/admin/order-notes", () => {
|
||||
it("empty order -> 200 med tom lista (explicit empty behavior)", async () => {
|
||||
const res = await GET(req(`/api/admin/order-notes?order_id=${orderId}`));
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.notes).toEqual([]);
|
||||
});
|
||||
|
||||
it("saknad order_id -> 400; okänd order -> 404", async () => {
|
||||
expect((await GET(req("/api/admin/order-notes"))).status).toBe(400);
|
||||
expect((await GET(req("/api/admin/order-notes?order_id=nope"))).status).toBe(404);
|
||||
});
|
||||
|
||||
it("utan auth-svar från requireRouteAuth -> fail-closed (401), aldrig data", async () => {
|
||||
vi.mocked(requireRouteAuth).mockResolvedValueOnce({
|
||||
response: new NextResponse(null, { status: 401 }),
|
||||
} as never);
|
||||
const res = await GET(req(`/api/admin/order-notes?order_id=${orderId}`));
|
||||
expect(res.status).toBe(401);
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST + GET happy path (kernel-semantik)", () => {
|
||||
it("201 skapar noten, GET listar deterministiskt (Lamport-ordning, inte insättningsordning)", async () => {
|
||||
const r1 = await POST(jsonReq("/api/admin/order-notes", { order_id: orderId, body: "Första" }));
|
||||
expect(r1.status).toBe(201);
|
||||
const first = await r1.json();
|
||||
expect(first.version).toBe(1);
|
||||
expect(first.clock).toBe(1);
|
||||
|
||||
const r2 = await POST(jsonReq("/api/admin/order-notes", { order_id: orderId, body: "Andra" }));
|
||||
expect(r2.status).toBe(201);
|
||||
|
||||
const list = await (await GET(req(`/api/admin/order-notes?order_id=${orderId}`))).json();
|
||||
expect(list.notes).toHaveLength(2);
|
||||
// kernel mergeOperationLog: stigande (clock, actorId, id)
|
||||
const clocks = list.notes.map((n: { clock: number }) => n.clock);
|
||||
expect([...clocks].sort((a: number, b: number) => a - b)).toEqual(clocks);
|
||||
expect(list.notes[0].body).toBe("Första");
|
||||
|
||||
// audit event emitterades via hostens AUD0-klient
|
||||
const ingest = vi.mocked(createAud0Client).mock.results.at(-1)?.value as {
|
||||
ingestEvent: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
expect(ingest.ingestEvent).toHaveBeenCalled();
|
||||
const evt = ingest.ingestEvent.mock.calls[0][0];
|
||||
expect(evt.capability_id).toBe("admin.order_notes");
|
||||
expect(evt.event_type).toBe("order_note.created");
|
||||
});
|
||||
|
||||
it("tom body -> 400 (server-authoritative validering)", async () => {
|
||||
const res = await POST(jsonReq("/api/admin/order-notes", { order_id: orderId, body: " " }));
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
|
||||
it("tenant-guard: order tillhör annan tenant än token -> 403", async () => {
|
||||
const created = await createOrder("custom", { markets: ["SE"], volume: 5 }, undefined, {
|
||||
tenantId: "tenant-other",
|
||||
subject: "buyer-other",
|
||||
});
|
||||
const res = await POST(
|
||||
jsonReq("/api/admin/order-notes", { order_id: created.order.order_id, body: "hej" }),
|
||||
);
|
||||
expect(res.status).toBe(403);
|
||||
const body = await res.json();
|
||||
expect(body.error).toBe("OPT_ACCESS_DENIED");
|
||||
});
|
||||
});
|
||||
|
||||
describe("PATCH — optimistisk samtidighet (conflict behavior)", () => {
|
||||
it("rätt version -> 200, version+1, klockan tickar (nextLamportClock)", async () => {
|
||||
const note = await addNote({ order_id: orderId, tenant_id: null, author_id: "a", body: "v1" });
|
||||
const res = await PATCH(
|
||||
jsonReq("/api/admin/order-notes", { note_id: note.note_id, body: "v2", version: 1 }, "PATCH"),
|
||||
);
|
||||
expect(res.status).toBe(200);
|
||||
const updated = await res.json();
|
||||
expect(updated.body).toBe("v2");
|
||||
expect(updated.version).toBe(2);
|
||||
expect(updated.clock).toBe(note.clock + 1);
|
||||
});
|
||||
|
||||
it("stale version -> 409 med aktuell version (multi-user semantics)", async () => {
|
||||
const note = await addNote({ order_id: orderId, tenant_id: null, author_id: "a", body: "v1" });
|
||||
await updateNoteViaApi(note.note_id, "v2 fran annan skribent"); // samtidig skribant -> v2
|
||||
const res = await PATCH(
|
||||
jsonReq("/api/admin/order-notes", { note_id: note.note_id, body: "v3 stale", version: 1 }, "PATCH"),
|
||||
);
|
||||
expect(res.status).toBe(409);
|
||||
const body = await res.json();
|
||||
expect(body.error).toBe("version_conflict");
|
||||
expect(body.current_version).toBe(2);
|
||||
});
|
||||
|
||||
it("okänd note -> 404; ogiltig version -> 400", async () => {
|
||||
expect(
|
||||
(
|
||||
await PATCH(
|
||||
jsonReq(
|
||||
"/api/admin/order-notes",
|
||||
{ note_id: "note_nope", body: "x", version: 1 },
|
||||
"PATCH",
|
||||
),
|
||||
)
|
||||
).status,
|
||||
).toBe(404);
|
||||
const note = await addNote({ order_id: orderId, tenant_id: null, author_id: "a", body: "v1" });
|
||||
expect(
|
||||
(
|
||||
await PATCH(
|
||||
jsonReq(
|
||||
"/api/admin/order-notes",
|
||||
{ note_id: note.note_id, body: "x", version: 1.5 },
|
||||
"PATCH",
|
||||
),
|
||||
)
|
||||
).status,
|
||||
).toBe(400);
|
||||
});
|
||||
|
||||
it("PATCH utan auth -> 401 fail-closed", async () => {
|
||||
vi.mocked(requireRouteAuth).mockResolvedValueOnce({
|
||||
response: new NextResponse(null, { status: 401 }),
|
||||
} as never);
|
||||
const res = await PATCH(
|
||||
jsonReq("/api/admin/order-notes", { note_id: "x", body: "x", version: 1 }, "PATCH"),
|
||||
);
|
||||
expect(res.status).toBe(401);
|
||||
});
|
||||
});
|
||||
|
||||
async function updateNoteViaApi(noteId: string, body: string) {
|
||||
const res = await PATCH(jsonReq("/api/admin/order-notes", { note_id: noteId, body, version: 1 }, "PATCH"));
|
||||
expect(res.status).toBe(200);
|
||||
return res.json();
|
||||
}
|
||||
|
||||
describe("kernel-återanvändning (capability, inte paket)", () => {
|
||||
it("listNotes är kernel-mergead: ordningen följer compareOperations (klocka -> actorId -> id), två listningar identiska", async () => {
|
||||
const a = await addNote({ order_id: orderId, tenant_id: null, author_id: "a", body: "A" });
|
||||
const b = await addNote({ order_id: orderId, tenant_id: null, author_id: "b", body: "B" });
|
||||
const first = await listNotes(orderId);
|
||||
const second = await listNotes(orderId);
|
||||
expect(second.map((n) => n.note_id)).toEqual(first.map((n) => n.note_id));
|
||||
// stigande klocka = deterministisk kernel-ordning, oberoende av minnes-mapens iteration
|
||||
expect(first.map((n) => n.clock)).toEqual([a.clock, b.clock].sort((x, y) => x - y));
|
||||
expect(first.map((n) => n.note_id)).toEqual([a.note_id, b.note_id]);
|
||||
});
|
||||
});
|
||||
|
||||
// getOrder används i routen (server-authoritative tenant-uppslag) — håll
|
||||
// importen levande så refaktorer inte tyst tappar guarden.
|
||||
void getOrder;
|
||||
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* Våg 3.4 (DoD §56): unit tests for GET /api/orders/export — the first
|
||||
* adoption of the export kernel. Follows the suite pattern of calling the
|
||||
* route handler directly with a hand-built NextRequest and mocked auth/store.
|
||||
*/
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
import { NextRequest } from "next/server";
|
||||
|
||||
vi.mock("@/src/auth/require-order-owner", () => ({
|
||||
requireOrderOwner: vi.fn().mockResolvedValue({ owner: { tenantId: "tenant-test", subject: "buyer-test" } }),
|
||||
}));
|
||||
vi.mock("@/lib/orders-store", () => ({
|
||||
listOwnedOrders: vi.fn().mockResolvedValue([
|
||||
{ order_id: "ord_1", flow: "custom", status: "draft", deposit_ore: 5000, total_ore: 10000 },
|
||||
{ order_id: 'ord,"2"', flow: "platform", status: "delivered", deposit_ore: 0, total_ore: 12345 },
|
||||
]),
|
||||
}));
|
||||
|
||||
import { GET as exportGET } from "@/app/api/orders/export/route";
|
||||
|
||||
function getReq(): NextRequest {
|
||||
return new NextRequest(new URL("/api/orders/export", "http://localhost"), { method: "GET" });
|
||||
}
|
||||
|
||||
describe("GET /api/orders/export — CSV (våg 3.4, DoD §56)", () => {
|
||||
it("emits the documented header row and CRLF line endings", async () => {
|
||||
const r = await exportGET(getReq());
|
||||
expect(r.status).toBe(200);
|
||||
expect(r.headers.get("content-type")).toContain("text/csv");
|
||||
expect(r.headers.get("content-disposition")).toMatch(/^attachment; filename="orders-\d{4}-\d{2}-\d{2}\.csv"$/);
|
||||
const body = await r.text();
|
||||
const lines = body.split("\r\n");
|
||||
expect(lines[0]).toBe("order_id,flow,status,deposit_ore,total_ore");
|
||||
expect(lines[1]).toBe("ord_1,custom,draft,5000,10000");
|
||||
});
|
||||
|
||||
it("RFC4180-quotes fields containing comma/quote (doubled quotes)", async () => {
|
||||
const r = await exportGET(getReq());
|
||||
const body = await r.text();
|
||||
expect(body).toContain('"ord,""2"""');
|
||||
});
|
||||
|
||||
it("exports RAW öre — no locale formatting in the export (öre is canonical)", async () => {
|
||||
const r = await exportGET(getReq());
|
||||
const body = await r.text();
|
||||
expect(body).toContain("ord_1,custom,draft,5000,10000");
|
||||
expect(body).not.toContain("100,00");
|
||||
expect(body).not.toMatch(/\u00A0| kr/);
|
||||
});
|
||||
|
||||
it("no row truncation: export is complete per DoD §56 (cap documented)", async () => {
|
||||
const r = await exportGET(getReq());
|
||||
const body = await r.text();
|
||||
const rows = body.trimEnd().split("\r\n").length - 1;
|
||||
expect(rows).toBe(2);
|
||||
});
|
||||
|
||||
it("auth is fail-closed: the route delegates to requireOrderOwner (401 contract)", async () => {
|
||||
// The mocked requireOrderOwner resolves with an owner; the 401 path is the
|
||||
// auth.response passthrough asserted in tests/api/anonymous-routes-auth.
|
||||
// This test pins the wiring: the handler must return auth.response
|
||||
// verbatim when present, before any store access.
|
||||
const { requireOrderOwner } = await import("@/src/auth/require-order-owner");
|
||||
const mocked = requireOrderOwner as ReturnType<typeof vi.fn>;
|
||||
mocked.mockResolvedValueOnce({
|
||||
response: new Response(JSON.stringify({ error: { code: "unauthorized" } }), { status: 401 }),
|
||||
});
|
||||
const r = await exportGET(getReq());
|
||||
expect(r.status).toBe(401);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,156 @@
|
||||
/**
|
||||
* C8: tests for POST /api/orders/import — the symmetric counterpart of the
|
||||
* våg-3.4 CSV export and the first real product host of the @siax/ui
|
||||
* ImportWizard kernel. Follows the suite pattern of calling the route handler
|
||||
* directly with a hand-built NextRequest and mocked auth/store/audit.
|
||||
*/
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
const mockOwner = { owner: { tenantId: "tenant-test", subject: "buyer-test" } };
|
||||
const requireOrderOwnerMock = vi.fn();
|
||||
vi.mock("@/src/auth/require-order-owner", () => ({
|
||||
requireOrderOwner: (...args: unknown[]) => requireOrderOwnerMock(...args),
|
||||
}));
|
||||
|
||||
const createOrderMock = vi.fn();
|
||||
vi.mock("@/lib/orders-store", () => ({
|
||||
createOrder: (...args: unknown[]) => createOrderMock(...args),
|
||||
}));
|
||||
|
||||
const ingestEventMock = vi.fn().mockResolvedValue({});
|
||||
vi.mock("@/src/clients/aud0-client", () => ({
|
||||
createAud0Client: () => ({ ingestEvent: ingestEventMock }),
|
||||
}));
|
||||
|
||||
import { POST, parseCsv, validateImport, MAX_IMPORT_ROWS } from "@/app/api/orders/import/route";
|
||||
|
||||
function postReq(body: unknown, withAuth = true): NextRequest {
|
||||
requireOrderOwnerMock.mockResolvedValue(withAuth ? mockOwner : { response: NextResponse.json({ error: "OPT_UNAUTHENTICATED" }, { status: 401 }) });
|
||||
return new NextRequest(new URL("/api/orders/import", "http://localhost"), {
|
||||
method: "POST",
|
||||
body: JSON.stringify(body),
|
||||
headers: withAuth ? { authorization: "Bearer x" } : {},
|
||||
});
|
||||
}
|
||||
|
||||
const HEADER = "flow,service_type,type,volume,frequency,markets";
|
||||
|
||||
function csv(lines: string[]): string {
|
||||
return [HEADER, ...lines].join("\r\n") + "\r\n";
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
createOrderMock.mockReset();
|
||||
ingestEventMock.mockClear();
|
||||
createOrderMock.mockImplementation(async (flow: string, config: unknown, _key: unknown, owner: { tenantId: string }) => ({
|
||||
order: { order_id: `ord_${createOrderMock.mock.calls.length}`, flow, status: "draft", config, tenant_id: owner.tenantId },
|
||||
replayed: false,
|
||||
}));
|
||||
});
|
||||
|
||||
describe("parseCsv (server-parsed, RFC 4180, no eval)", () => {
|
||||
it("parses quoted fields with doubled quotes", () => {
|
||||
const { rows } = parseCsv('flow,markets\r\ncustom,"A;""B,C"""\r\n');
|
||||
expect(rows[0].flow).toBe("custom");
|
||||
expect(rows[0].markets).toBe('A;"B,C"');
|
||||
});
|
||||
|
||||
it("rejects unknown header columns (fail-closed, never silently dropped)", () => {
|
||||
const { headerError } = parseCsv("flow,evil\r\ncustom,x\r\n");
|
||||
expect(headerError).toMatch(/Okänd kolumn: evil/);
|
||||
});
|
||||
});
|
||||
|
||||
describe("validateImport — kernel + host rules", () => {
|
||||
it("reports missing required flow as missing_required", () => {
|
||||
const { report } = validateImport(csv([",icp,Bolag,10,Engångs,SE"]));
|
||||
expect(report.errorCount).toBeGreaterThan(0);
|
||||
expect(report.errors.some((e) => e.code === "missing_required")).toBe(true);
|
||||
});
|
||||
|
||||
it("reports bad host fields with row/field/code", () => {
|
||||
const { report } = validateImport(csv(["nope,icp,Bolag,-5,Engångs,SE"]));
|
||||
const fields = report.errors.map((e) => e.field).sort();
|
||||
expect(fields).toContain("flow");
|
||||
expect(fields).toContain("volume");
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/orders/import", () => {
|
||||
it("happy path: execute imports all valid rows through createOrder with the verified owner + emits AUD0", async () => {
|
||||
const r = await POST(postReq({ mode: "execute", csv: csv(["custom,icp,Bolag,10,Engångs,SE", "catalog,icp,Fordon,2,Engångs,", "subscription,,Beslutsfattare,50,Veckovis,SE;NO"]) }));
|
||||
expect(r.status).toBe(200);
|
||||
const body = await r.json();
|
||||
expect(body.imported).toBe(3);
|
||||
expect(body.errors).toHaveLength(0);
|
||||
expect(body.order_ids).toHaveLength(3);
|
||||
expect(createOrderMock).toHaveBeenCalledTimes(3);
|
||||
expect(createOrderMock.mock.calls[0][3]).toEqual({ tenantId: "tenant-test", subject: "buyer-test" });
|
||||
expect(createOrderMock.mock.calls[0][1]).toMatchObject({ service_type: "icp", type: "Bolag", volume: 10, frequency: "Engångs", markets: ["SE"] });
|
||||
expect(createOrderMock.mock.calls[2][1]).toMatchObject({ markets: ["SE", "NO"] });
|
||||
expect(ingestEventMock).toHaveBeenCalledTimes(1);
|
||||
expect(ingestEventMock.mock.calls[0][0]).toMatchObject({ event_type: "0pt.orders.import", tenant_id: "tenant-test", risk_level: "L2" });
|
||||
});
|
||||
|
||||
it("partial success: failing rows are reported per row and never created; passing rows import", async () => {
|
||||
const r = await POST(postReq({ mode: "execute", csv: csv(["custom,icp,Bolag,10,Engångs,SE", "bogus_flow,icp,Bolag,10,Engångs,SE", "catalog,icp,Bolag,notanumber,Engångs,SE"]) }));
|
||||
expect(r.status).toBe(200);
|
||||
const body = await r.json();
|
||||
expect(body.imported).toBe(1);
|
||||
expect(body.errorCount).toBe(2);
|
||||
const badFlow = body.errors.find((e: { field: string }) => e.field === "flow");
|
||||
const badVol = body.errors.find((e: { field: string }) => e.field === "volume");
|
||||
expect(badFlow.row).toBe(2);
|
||||
expect(badVol.row).toBe(3);
|
||||
expect(createOrderMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("validate/dry_run never write (createOrder not called)", async () => {
|
||||
const r1 = await POST(postReq({ mode: "validate", csv: csv(["custom,icp,Bolag,10,Engångs,SE"]) }));
|
||||
expect(r1.status).toBe(200);
|
||||
const b1 = await r1.json();
|
||||
expect(b1.validRows).toBe(1);
|
||||
expect(b1.would_import).toBe(1);
|
||||
const r2 = await POST(postReq({ mode: "dry_run", csv: csv(["custom,icp,Bolag,10,Engångs,SE"]) }));
|
||||
expect(r2.status).toBe(200);
|
||||
expect(createOrderMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("fail-closed auth: no principal → 401 and createOrder is never reached", async () => {
|
||||
const r = await POST(postReq({ mode: "execute", csv: csv(["custom,icp,Bolag,10,Engångs,SE"]) }, false));
|
||||
expect(r.status).toBe(401);
|
||||
expect(createOrderMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("row cap: more than MAX_IMPORT_ROWS data rows → 413, no writes", async () => {
|
||||
const lines = Array.from({ length: MAX_IMPORT_ROWS + 1 }, () => "custom,icp,Bolag,10,Engångs,SE");
|
||||
const r = await POST(postReq({ mode: "execute", csv: csv(lines) }));
|
||||
expect(r.status).toBe(413);
|
||||
expect(createOrderMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("unknown header → 400 fail-closed", async () => {
|
||||
const r = await POST(postReq({ mode: "execute", csv: "flow,evil\r\ncustom,x\r\n" }));
|
||||
expect(r.status).toBe(400);
|
||||
const body = await r.json();
|
||||
expect(body.error.code).toBe("bad_header");
|
||||
});
|
||||
|
||||
it("empty/missing csv → 400", async () => {
|
||||
const r = await POST(postReq({ mode: "execute", csv: " " }));
|
||||
expect(r.status).toBe(400);
|
||||
});
|
||||
|
||||
it("execute-time creation failure is reported per row without aborting the batch", async () => {
|
||||
createOrderMock.mockImplementationOnce(async () => {
|
||||
throw new Error("db down");
|
||||
});
|
||||
const r = await POST(postReq({ mode: "execute", csv: csv(["custom,icp,Bolag,10,Engångs,SE", "catalog,icp,Bolag,1,Engångs,SE"]) }));
|
||||
expect(r.status).toBe(200);
|
||||
const body = await r.json();
|
||||
expect(body.imported).toBe(1);
|
||||
expect(body.errors[0].row).toBe(1);
|
||||
expect(body.errors[0].message).toContain("db down");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* C8: SSR-render proof that the host can render the @siax/ui ImportWizard over
|
||||
* the @siax/import-export runtime store (the exact packages the admin import
|
||||
* page uses). Node environment — react-dom/server, no DOM needed.
|
||||
*/
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
|
||||
// ImportWizard subscribes to the runtime store via useSyncExternalStore, which
|
||||
// demands getServerSnapshot under react-dom/server. For a static render proof
|
||||
// the snapshot read is enough — reactivity is the browser's concern.
|
||||
vi.mock("react", async (importOriginal) => {
|
||||
const React = await importOriginal<typeof import("react")>();
|
||||
return { ...React, useSyncExternalStore: ((_subscribe: unknown, getSnapshot: () => unknown) => getSnapshot()) as typeof React.useSyncExternalStore };
|
||||
});
|
||||
import { createElement } from "react";
|
||||
import { renderToString } from "react-dom/server";
|
||||
import { ImportWizard } from "@siax/ui";
|
||||
import { createImportRunStore } from "@siax/import-export";
|
||||
import { createImportJob } from "@siax/contracts/import-export";
|
||||
|
||||
function makeStore(rows: Record<string, string>[]) {
|
||||
return createImportRunStore({
|
||||
job: createImportJob({
|
||||
id: "test-import",
|
||||
tenantId: "tenant-test",
|
||||
entity: "orders",
|
||||
format: "csv",
|
||||
source: { system: "0pt-admin", channel: "web" },
|
||||
createdAt: "2026-09-30T00:00:00Z",
|
||||
}),
|
||||
rows,
|
||||
});
|
||||
}
|
||||
|
||||
describe("ImportWizard renders over the runtime store (host integration)", () => {
|
||||
it("empty store → explicit empty state with the pipeline <ol>", () => {
|
||||
const html = renderToString(
|
||||
createElement(ImportWizard, { store: makeStore([]), onExecute: async () => {} }),
|
||||
);
|
||||
expect(html).toContain("<ol");
|
||||
expect(html.length).toBeGreaterThan(100);
|
||||
});
|
||||
|
||||
it("rows present → pipeline steps render; mapped stage shows mapping table", () => {
|
||||
const store = makeStore([{ flow: "custom", service_type: "icp", type: "Bolag", volume: "10", frequency: "Engångs", markets: "SE" }]);
|
||||
store.detect();
|
||||
let html = renderToString(createElement(ImportWizard, { store, columns: [{ source: "flow", target: "flow" }], onExecute: async () => {} }));
|
||||
expect(html).toContain("<ol");
|
||||
expect(html).toContain("aria-current=\"step\"");
|
||||
store.map({ columns: [{ source: "flow", target: "flow", required: true }] });
|
||||
html = renderToString(createElement(ImportWizard, { store, onExecute: async () => {} }));
|
||||
expect(html).toContain("flow");
|
||||
});
|
||||
|
||||
it("failed store renders the failure report with role=alert", () => {
|
||||
const store = makeStore([{ flow: "custom" }]);
|
||||
store.detect();
|
||||
store.fail("Servern avvisade exekveringen");
|
||||
const html = renderToString(createElement(ImportWizard, { store, onExecute: async () => {} }));
|
||||
expect(html).toContain("alert");
|
||||
expect(html).toContain("Servern avvisade exekveringen");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* SSR-render-proof för den nya admin-ytan /admin/notes (kernel-konsumenten).
|
||||
* Samma mönster som import-wizard-ssr.test.ts: react-dom/server i Node.
|
||||
* PortalShell -> usePathname (next/navigation) mockas; sonner toast mockas.
|
||||
*/
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
|
||||
vi.mock("next/navigation", () => ({
|
||||
usePathname: () => "/admin/notes",
|
||||
useRouter: () => ({ push: vi.fn(), replace: vi.fn() }),
|
||||
}));
|
||||
vi.mock("sonner", () => ({ toast: { success: vi.fn(), warning: vi.fn(), error: vi.fn() } }));
|
||||
vi.mock("@/components/locale-provider", () => ({
|
||||
LocaleProvider: ({ children }: { children: React.ReactNode }) => children,
|
||||
useT: () => (x: { en: string; sv: string }) => x.en,
|
||||
useLocale: () => "en",
|
||||
}));
|
||||
|
||||
import { createElement } from "react";
|
||||
import { renderToString } from "react-dom/server";
|
||||
import OrderNotesPage from "@/app/admin/notes/page";
|
||||
|
||||
describe("/admin/notes SSR (kernel-konsument-yta)", () => {
|
||||
it("renderar rubrik, offline-växeln och laddningstillstånd utan krasch (SSR = första paint före useEffect)", () => {
|
||||
const html = renderToString(createElement(OrderNotesPage));
|
||||
expect(html).toContain("Orderanteckningar");
|
||||
expect(html).toContain("Offline-läge");
|
||||
expect(html).toContain('role="status"'); // loading-skeleton = korrekt SSR-att-få-state
|
||||
expect(html.length).toBeGreaterThan(500);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,124 @@
|
||||
/**
|
||||
* Proves the narrow-scan property of PostgresKeyedLookupConnector — the
|
||||
* connector that stands in for "dealer_stock_entry.vehicle_id -> unique
|
||||
* vehicle IDs -> targeted lookup -> dimension_vehicle", never a scan of the
|
||||
* 98M-row `vehicle` table. The assertions are on QUERY CALLS, not just
|
||||
* returned records: a bug that widened the WHERE clause but happened to
|
||||
* still return the right rows in a test fixture would be invisible to a
|
||||
* behavioural-only test and is exactly what this file is written to catch.
|
||||
*/
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { PostgresKeyedLookupConnector } from "@/src/connectors/postgres-keyed-lookup-connector";
|
||||
import type { QueryImpl, SourceRow } from "@/src/connectors/postgres-watermark-connector";
|
||||
import type { RecordSchemaFields } from "@/src/schema/record-schema";
|
||||
|
||||
const SCHEMA: RecordSchemaFields = {
|
||||
id: { type: "string", required: true },
|
||||
make: { type: "string", required: false },
|
||||
model: { type: "string", required: false },
|
||||
};
|
||||
|
||||
const VEHICLE_TABLE: SourceRow[] = [
|
||||
{ id: "v1", make: "Volvo", model: "XC60" },
|
||||
{ id: "v2", make: "Volvo", model: "V70" },
|
||||
{ id: "v3", make: "Toyota", model: "Corolla" },
|
||||
{ id: "v4", make: "Audi", model: "A4" }, // never referenced by any dealer_stock_entry in these tests
|
||||
];
|
||||
|
||||
function fixtureQuery(): { query: QueryImpl; calls: Array<{ sql: string; params: unknown[] }> } {
|
||||
const calls: Array<{ sql: string; params: unknown[] }> = [];
|
||||
const query: QueryImpl = async (sql, params) => {
|
||||
calls.push({ sql, params });
|
||||
const [ids] = params as [Array<string | number>];
|
||||
return VEHICLE_TABLE.filter((row) => ids.includes(row.id as string));
|
||||
};
|
||||
return { query, calls };
|
||||
}
|
||||
|
||||
function config(keys: Array<string | number>, keysPerQuery?: number) {
|
||||
return {
|
||||
table: "vehiq.vehicle",
|
||||
partition_key: "dimension_vehicle",
|
||||
columns: ["id", "make", "model"],
|
||||
record_key_column: "id",
|
||||
schema: SCHEMA,
|
||||
keys,
|
||||
keys_per_query: keysPerQuery,
|
||||
};
|
||||
}
|
||||
|
||||
describe("PostgresKeyedLookupConnector — validate()", () => {
|
||||
it("refuses an empty key list rather than defaulting to a full read", () => {
|
||||
const connector = new PostgresKeyedLookupConnector(fixtureQuery().query);
|
||||
const verdict = connector.validate(config([]));
|
||||
expect(verdict.valid).toBe(false);
|
||||
expect(verdict.errors.join(" ")).toMatch(/keys must not be empty/);
|
||||
});
|
||||
|
||||
it("rejects '*' in columns", () => {
|
||||
const connector = new PostgresKeyedLookupConnector(fixtureQuery().query);
|
||||
expect(connector.validate({ ...config(["v1"]), columns: ["*"] }).valid).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("PostgresKeyedLookupConnector — the narrow-scan property", () => {
|
||||
it("connect() with an empty key list is refused, and NO query is ever issued", async () => {
|
||||
const { query, calls } = fixtureQuery();
|
||||
const connector = new PostgresKeyedLookupConnector(query);
|
||||
await expect(
|
||||
connector.connect(config([]), { tenant_id: "t", project_id: "p", source_id: "s", run_id: "r" }),
|
||||
).rejects.toThrow(/keys must not be empty/);
|
||||
expect(calls).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("reads exactly the vehicles referenced by the batch — v4 (never referenced) is never touched", async () => {
|
||||
const referenced = ["v1", "v2", "v3"]; // simulates dedup'd dealer_stock_entry.vehicle_id values from one batch
|
||||
const { query, calls } = fixtureQuery();
|
||||
const connector = new PostgresKeyedLookupConnector(query);
|
||||
await connector.connect(config(referenced), { tenant_id: "t", project_id: "p", source_id: "s", run_id: "r" });
|
||||
const result = await connector.read({ partition_key: "dimension_vehicle", limit: 500 });
|
||||
|
||||
expect(result.records.map((r) => r.record_key).sort()).toEqual(["v1", "v2", "v3"]);
|
||||
expect(result.exhausted).toBe(true);
|
||||
|
||||
// The narrow-scan property, checked at the QUERY level: every id ever
|
||||
// bound into a query call is a subset of `referenced`, and the union of
|
||||
// all ids queried equals exactly `referenced` — never more, never a
|
||||
// wildcard, never "v4".
|
||||
expect(calls).toHaveLength(1);
|
||||
const allQueriedIds = calls.flatMap((c) => c.params[0] as string[]);
|
||||
expect(allQueriedIds.sort()).toEqual([...referenced].sort());
|
||||
expect(allQueriedIds).not.toContain("v4");
|
||||
for (const call of calls) {
|
||||
expect(call.sql).toMatch(/WHERE id = ANY\(\$1\)/);
|
||||
expect(call.sql).not.toMatch(/select\s+\*/i);
|
||||
}
|
||||
});
|
||||
|
||||
it("chunks a large key list across multiple bounded queries, never one unbounded IN(...)", async () => {
|
||||
const referenced = Array.from({ length: 5 }, (_, i) => `v${i + 1}`);
|
||||
const { query, calls } = fixtureQuery();
|
||||
const connector = new PostgresKeyedLookupConnector(query);
|
||||
await connector.connect(config(referenced, 2), { tenant_id: "t", project_id: "p", source_id: "s", run_id: "r" });
|
||||
|
||||
let cursor: string | undefined;
|
||||
let pages = 0;
|
||||
for (let i = 0; i < 10; i++) {
|
||||
const page = await connector.read({ partition_key: "dimension_vehicle", cursor, limit: 500 });
|
||||
pages++;
|
||||
cursor = page.next_cursor;
|
||||
if (page.exhausted) break;
|
||||
}
|
||||
expect(pages).toBe(3); // ceil(5/2)
|
||||
for (const call of calls) {
|
||||
expect((call.params[0] as string[]).length).toBeLessThanOrEqual(2);
|
||||
}
|
||||
const allQueriedIds = calls.flatMap((c) => c.params[0] as string[]);
|
||||
expect(new Set(allQueriedIds).size).toBe(5);
|
||||
});
|
||||
|
||||
it("the empty-batch case: zero referenced vehicles means this connector is never even constructed by the caller (see vehiq-oltp-ingest-runner.test.ts) — but if it IS given zero keys, it fails validation rather than scanning", () => {
|
||||
const connector = new PostgresKeyedLookupConnector(fixtureQuery().query);
|
||||
expect(connector.validate(config([])).valid).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,194 @@
|
||||
/**
|
||||
* Connector-level proof for postgres-watermark-connector.ts. No real
|
||||
* Postgres: `queryImpl` is a fixture that runs the SAME SQL text the real
|
||||
* connector builds against an in-memory row set, so the watermark math and
|
||||
* the tie-break are proven precisely rather than just "some rows came back".
|
||||
*/
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
PostgresWatermarkConnector,
|
||||
checksumRows,
|
||||
decodeCursor,
|
||||
encodeCursor,
|
||||
type QueryImpl,
|
||||
type SourceRow,
|
||||
} from "@/src/connectors/postgres-watermark-connector";
|
||||
import type { RecordSchemaFields } from "@/src/schema/record-schema";
|
||||
|
||||
const SCHEMA: RecordSchemaFields = {
|
||||
id: { type: "string", required: true },
|
||||
vehicle_id: { type: "string", required: false },
|
||||
updated_at: { type: "string", required: true },
|
||||
};
|
||||
|
||||
const CONFIG = {
|
||||
table: "app.dealer_stock_entry",
|
||||
partition_key: "dealer_stock_entry",
|
||||
columns: ["id", "vehicle_id", "updated_at"],
|
||||
record_key_column: "id",
|
||||
watermark_column: "updated_at",
|
||||
schema: SCHEMA,
|
||||
};
|
||||
|
||||
/** A fixture query engine that actually parses/applies the connector's WHERE/ORDER BY/LIMIT against an in-memory table — not a stub that ignores its arguments. */
|
||||
function fixtureQuery(rows: SourceRow[]): { query: QueryImpl; calls: Array<{ sql: string; params: unknown[] }> } {
|
||||
const calls: Array<{ sql: string; params: unknown[] }> = [];
|
||||
const query: QueryImpl = async (sql, params) => {
|
||||
calls.push({ sql, params });
|
||||
let filtered = rows;
|
||||
if (sql.includes("WHERE")) {
|
||||
const [watermark, id] = params as [string, string, number];
|
||||
filtered = rows.filter(
|
||||
(r) => (r.updated_at as string) > watermark || ((r.updated_at as string) === watermark && (r.id as string) > id),
|
||||
);
|
||||
}
|
||||
const sorted = [...filtered].sort((a, b) => {
|
||||
const w = (a.updated_at as string).localeCompare(b.updated_at as string);
|
||||
return w !== 0 ? w : (a.id as string).localeCompare(b.id as string);
|
||||
});
|
||||
const limit = Number(params[params.length - 1]);
|
||||
return sorted.slice(0, limit);
|
||||
};
|
||||
return { query, calls };
|
||||
}
|
||||
|
||||
const ROWS: SourceRow[] = [
|
||||
{ id: "a1", vehicle_id: "v1", updated_at: "2026-09-01T00:00:00.000Z" },
|
||||
{ id: "a2", vehicle_id: "v2", updated_at: "2026-09-01T00:00:00.000Z" }, // same updated_at as a1 — the tie-break case
|
||||
{ id: "a3", vehicle_id: "v1", updated_at: "2026-09-02T00:00:00.000Z" },
|
||||
{ id: "a4", vehicle_id: "v3", updated_at: "2026-09-03T00:00:00.000Z" },
|
||||
];
|
||||
|
||||
describe("PostgresWatermarkConnector — validate()", () => {
|
||||
it("rejects a config pointed at the 98M-row vehicle table", () => {
|
||||
const connector = new PostgresWatermarkConnector(fixtureQuery([]).query);
|
||||
const verdict = connector.validate({ ...CONFIG, table: "vehiq.vehicle" });
|
||||
expect(verdict.valid).toBe(false);
|
||||
expect(verdict.errors.join(" ")).toMatch(/vehicle/i);
|
||||
});
|
||||
|
||||
it("rejects SELECT * (an empty/missing columns list, or a literal '*')", () => {
|
||||
const connector = new PostgresWatermarkConnector(fixtureQuery([]).query);
|
||||
expect(connector.validate({ ...CONFIG, columns: [] }).valid).toBe(false);
|
||||
expect(connector.validate({ ...CONFIG, columns: ["*"] }).valid).toBe(false);
|
||||
});
|
||||
|
||||
it("accepts a well-formed config", () => {
|
||||
const connector = new PostgresWatermarkConnector(fixtureQuery([]).query);
|
||||
expect(connector.validate(CONFIG).valid).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("PostgresWatermarkConnector — watermark + limit correctness", () => {
|
||||
it("first read (no cursor) honours LIMIT and orders by (updated_at, id)", async () => {
|
||||
const { query } = fixtureQuery(ROWS);
|
||||
const connector = new PostgresWatermarkConnector(query);
|
||||
await connector.connect(CONFIG, { tenant_id: "t", project_id: "p", source_id: "s", run_id: "r" });
|
||||
const result = await connector.read({ partition_key: "dealer_stock_entry", limit: 2 });
|
||||
expect(result.records.map((r) => r.record_key)).toEqual(["a1", "a2"]);
|
||||
expect(result.exhausted).toBe(false); // 2 rows returned == limit, more may exist
|
||||
});
|
||||
|
||||
it("a same-updated_at tie at the page boundary is not skipped on the next page (the whole reason for the tuple WHERE)", async () => {
|
||||
const { query } = fixtureQuery(ROWS);
|
||||
const connector = new PostgresWatermarkConnector(query);
|
||||
await connector.connect(CONFIG, { tenant_id: "t", project_id: "p", source_id: "s", run_id: "r" });
|
||||
const page1 = await connector.read({ partition_key: "dealer_stock_entry", limit: 1 }); // just a1
|
||||
expect(page1.records.map((r) => r.record_key)).toEqual(["a1"]);
|
||||
const page2 = await connector.read({ partition_key: "dealer_stock_entry", cursor: page1.next_cursor, limit: 1 });
|
||||
// A naive `updated_at > $1` would skip a2 here (same updated_at as a1).
|
||||
expect(page2.records.map((r) => r.record_key)).toEqual(["a2"]);
|
||||
});
|
||||
|
||||
it("reads the full table across pages in strict watermark order, once per row", async () => {
|
||||
const { query } = fixtureQuery(ROWS);
|
||||
const connector = new PostgresWatermarkConnector(query);
|
||||
await connector.connect(CONFIG, { tenant_id: "t", project_id: "p", source_id: "s", run_id: "r" });
|
||||
let cursor: string | undefined;
|
||||
const seen: string[] = [];
|
||||
for (let i = 0; i < 10; i++) {
|
||||
const page = await connector.read({ partition_key: "dealer_stock_entry", cursor, limit: 2 });
|
||||
seen.push(...page.records.map((r) => r.record_key));
|
||||
cursor = page.next_cursor;
|
||||
if (page.exhausted) break;
|
||||
}
|
||||
expect(seen).toEqual(["a1", "a2", "a3", "a4"]);
|
||||
});
|
||||
|
||||
it("the empty-table case: zero rows, no throw, exhausted:true", async () => {
|
||||
const { query, calls } = fixtureQuery([]);
|
||||
const connector = new PostgresWatermarkConnector(query);
|
||||
await connector.connect(CONFIG, { tenant_id: "t", project_id: "p", source_id: "s", run_id: "r" });
|
||||
const result = await connector.read({ partition_key: "dealer_stock_entry", limit: 100 });
|
||||
expect(result.records).toEqual([]);
|
||||
expect(result.exhausted).toBe(true);
|
||||
expect(calls).toHaveLength(1); // the query WAS issued (a real SELECT against a real, currently-empty table) — it just returned nothing
|
||||
});
|
||||
});
|
||||
|
||||
describe("PostgresWatermarkConnector — checkpoint / resume", () => {
|
||||
it("checkpoint() reflects the in-session cursor after a read", async () => {
|
||||
const { query } = fixtureQuery(ROWS);
|
||||
const connector = new PostgresWatermarkConnector(query);
|
||||
await connector.connect(CONFIG, { tenant_id: "t", project_id: "p", source_id: "s", run_id: "r" });
|
||||
await connector.read({ partition_key: "dealer_stock_entry", limit: 2 });
|
||||
const cp = await connector.checkpoint("dealer_stock_entry");
|
||||
const decoded = decodeCursor(cp.cursor);
|
||||
expect(decoded?.last_id).toBe("a2");
|
||||
expect(decoded?.last_updated_at).toBe("2026-09-01T00:00:00.000Z");
|
||||
expect(decoded?.checksum).toBe(checksumRows(ROWS.slice(0, 2)));
|
||||
});
|
||||
|
||||
it("resume() with a saved checkpoint continues exactly where the checkpoint left off", async () => {
|
||||
const { query } = fixtureQuery(ROWS);
|
||||
const connector = new PostgresWatermarkConnector(query);
|
||||
await connector.connect(CONFIG, { tenant_id: "t", project_id: "p", source_id: "s", run_id: "r" });
|
||||
const cursor = encodeCursor({ last_updated_at: "2026-09-02T00:00:00.000Z", last_id: "a3" });
|
||||
const result = await connector.resume(
|
||||
{ source_id: "s", tenant_id: "t", partition_key: "dealer_stock_entry", cursor, sequence: 3, records_committed: 3, committed_at: new Date().toISOString() },
|
||||
10,
|
||||
);
|
||||
expect(result.records.map((r) => r.record_key)).toEqual(["a4"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("PostgresWatermarkConnector — real Postgres driver value shapes", () => {
|
||||
it("a `timestamptz` column returned as a JS Date (not a string) is encoded as proper ISO-8601 in the cursor, not Date.toString()'s locale form", async () => {
|
||||
// Regression: caught by running this connector against a REAL Postgres
|
||||
// container (postgres.js hands back `timestamptz` as `Date`), not by any
|
||||
// fixture-only test — the fixtures elsewhere in this file all use plain
|
||||
// ISO strings, which never exercised this path. Reproduced here directly
|
||||
// instead of requiring Docker so the regression stays covered in CI.
|
||||
const rowsWithDates: SourceRow[] = [
|
||||
{ id: "a1", vehicle_id: "v1", updated_at: new Date("2026-09-01T00:00:00.000Z") },
|
||||
];
|
||||
const { query } = fixtureQuery(rowsWithDates.map((r) => ({ ...r, updated_at: (r.updated_at as Date).toISOString() })));
|
||||
// fixtureQuery's own row-compare logic needs string dates to sort; feed
|
||||
// it a Date-returning query instead so the connector genuinely sees a
|
||||
// Date object, the same as the real driver.
|
||||
const dateReturningQuery: QueryImpl = async (sql, params) => {
|
||||
const rows = await query(sql, params);
|
||||
return rows.map((r) => ({ ...r, updated_at: new Date(r.updated_at as string) }));
|
||||
};
|
||||
const connector = new PostgresWatermarkConnector(dateReturningQuery);
|
||||
await connector.connect(CONFIG, { tenant_id: "t", project_id: "p", source_id: "s", run_id: "r" });
|
||||
const result = await connector.read({ partition_key: "dealer_stock_entry", limit: 10 });
|
||||
expect(result.records[0].occurred_at).toBe("2026-09-01T00:00:00.000Z");
|
||||
const decoded = decodeCursor(result.next_cursor);
|
||||
expect(decoded?.last_updated_at).toBe("2026-09-01T00:00:00.000Z");
|
||||
expect(decoded?.last_updated_at).not.toMatch(/GMT|\(.*\)/); // not Date.toString()'s form
|
||||
});
|
||||
});
|
||||
|
||||
describe("PostgresWatermarkConnector — never SELECT *", () => {
|
||||
it("every SQL statement the connector builds names the configured columns explicitly", async () => {
|
||||
const { query, calls } = fixtureQuery(ROWS);
|
||||
const connector = new PostgresWatermarkConnector(query);
|
||||
await connector.connect(CONFIG, { tenant_id: "t", project_id: "p", source_id: "s", run_id: "r" });
|
||||
await connector.read({ partition_key: "dealer_stock_entry", limit: 10 });
|
||||
for (const call of calls) {
|
||||
expect(call.sql).not.toMatch(/select\s+\*/i);
|
||||
expect(call.sql).toContain("id, vehicle_id, updated_at");
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,217 @@
|
||||
/**
|
||||
* End-to-end proof for src/ingestion/vehiq-oltp-ingest-runner.ts — the
|
||||
* actual pipeline the scheduled workflow runs, wired through the REAL P062
|
||||
* IngestionRunner (InMemoryIngestionRepository backing it, no real
|
||||
* Postgres/ClickHouse). Four things the task requires are asserted here
|
||||
* specifically, not just "it doesn't throw":
|
||||
*
|
||||
* 1. a batch respects the watermark/limit (proven at the connector layer
|
||||
* already; here proven again at the FULL pipeline layer, envelope by
|
||||
* envelope);
|
||||
* 2. a run resumes correctly from a checkpoint after an interruption (a
|
||||
* sink that throws on its 2nd call, mid-run — same technique
|
||||
* tests/p0/ingestion-runner.test.ts itself uses to prove this for the
|
||||
* generic runner);
|
||||
* 3. the empty-table case (today's actual production state for all three
|
||||
* OLTP tables) completes with zero batches / zero sink calls / no
|
||||
* throw;
|
||||
* 4. the vehicle-dimension lookup only ever touches vehicle rows actually
|
||||
* referenced by dealer_stock_entry in the batch that ran — asserted by
|
||||
* capturing every query the dimension connector issues.
|
||||
*/
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { InMemoryIngestionRepository } from "@/src/repositories/in-memory-ingestion-repository";
|
||||
import { SchemaRegistry } from "@/src/schema/schema-registry";
|
||||
import {
|
||||
runVehiqOltpIngestion,
|
||||
previewVehiqOltpSource,
|
||||
VEHIQ_OLTP_SOURCES,
|
||||
type VehiqOltpIngestDeps,
|
||||
} from "@/src/ingestion/vehiq-oltp-ingest-runner";
|
||||
import type { QueryImpl, SourceRow } from "@/src/connectors/postgres-watermark-connector";
|
||||
import type { IngestionEnvelope } from "@/src/types/ingestion";
|
||||
|
||||
/**
|
||||
* A fake source Postgres. One table per fixture entry, keyed by the
|
||||
* connector's own `table` name. Generic across whichever watermark/
|
||||
* record-key column names the real connector puts in the SQL text — parsed
|
||||
* out of the statement itself (`ORDER BY <watermark>, <key>`) rather than
|
||||
* hardcoded, since `dealer_stock_entry` (watermark `listed_at`, key `id`)
|
||||
* and `deal` (watermark `created_at`, key `id`) use different column names.
|
||||
*/
|
||||
function fixtureVehiqDb(tables: Record<string, SourceRow[]>): { query: QueryImpl; calls: Array<{ sql: string; params: unknown[] }> } {
|
||||
const calls: Array<{ sql: string; params: unknown[] }> = [];
|
||||
const query: QueryImpl = async (sql, params) => {
|
||||
calls.push({ sql, params });
|
||||
const tableName = Object.keys(tables).find((t) => sql.includes(`FROM ${t}`));
|
||||
const rows = tableName ? tables[tableName] : [];
|
||||
const orderBy = /ORDER BY (\w+), (\w+)/.exec(sql);
|
||||
if (sql.includes("= ANY($1)")) {
|
||||
const keyColMatch = /WHERE (\w+) = ANY/.exec(sql);
|
||||
const keyCol = keyColMatch ? keyColMatch[1] : "id";
|
||||
const [ids] = params as [Array<string | number>];
|
||||
return rows.filter((r) => ids.includes(r[keyCol] as string));
|
||||
}
|
||||
if (!orderBy) return rows.slice(0, Number(params[params.length - 1]));
|
||||
const [, watermarkCol, keyCol] = orderBy;
|
||||
let filtered = rows;
|
||||
if (sql.includes("WHERE")) {
|
||||
const [watermark, id] = params as [string, string, number];
|
||||
filtered = rows.filter(
|
||||
(r) => (r[watermarkCol] as string) > watermark || ((r[watermarkCol] as string) === watermark && (r[keyCol] as string) > id),
|
||||
);
|
||||
}
|
||||
const sorted = [...filtered].sort((a, b) => {
|
||||
const w = (a[watermarkCol] as string).localeCompare(b[watermarkCol] as string);
|
||||
return w !== 0 ? w : (a[keyCol] as string).localeCompare(b[keyCol] as string);
|
||||
});
|
||||
const limit = Number(params[params.length - 1]);
|
||||
return sorted.slice(0, limit);
|
||||
};
|
||||
return { query, calls };
|
||||
}
|
||||
|
||||
function baseDeps(query: QueryImpl, sink: (e: IngestionEnvelope[]) => Promise<void>): VehiqOltpIngestDeps {
|
||||
const repo = new InMemoryIngestionRepository();
|
||||
return {
|
||||
repo,
|
||||
schemas: new SchemaRegistry(repo),
|
||||
sourceQuery: query,
|
||||
oltpSink: sink,
|
||||
dimensionSink: sink,
|
||||
tenantId: "t_vehiq",
|
||||
projectId: "vehiq",
|
||||
pageSize: 2,
|
||||
};
|
||||
}
|
||||
|
||||
describe("runVehiqOltpIngestion — empty-table case (today's actual production state)", () => {
|
||||
it("completes with zero batches, zero sink calls, no error, for all three OLTP tables", async () => {
|
||||
const { query } = fixtureVehiqDb({ "app.dealer_stock_entry": [], "app.listing": [], "app.deal": [] });
|
||||
const seen: IngestionEnvelope[] = [];
|
||||
const summary = await runVehiqOltpIngestion(baseDeps(query, async (e) => void seen.push(...e)));
|
||||
|
||||
expect(seen).toHaveLength(0);
|
||||
expect(summary.referenced_vehicle_ids).toBe(0);
|
||||
expect(summary.dimension).toBeUndefined(); // no referenced vehicles => the keyed-lookup connector is never even constructed
|
||||
for (const report of summary.oltp) {
|
||||
expect(report.status).toBe("succeeded");
|
||||
expect(report.partitions.every((p) => p.records_read === 0)).toBe(true);
|
||||
expect(report.partitions.every((p) => p.envelopes_emitted === 0)).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("previewVehiqOltpSource against empty tables never commits a checkpoint and reports would_read: 0", async () => {
|
||||
const { query } = fixtureVehiqDb({ "app.dealer_stock_entry": [] });
|
||||
const repo = new InMemoryIngestionRepository();
|
||||
const source = VEHIQ_OLTP_SOURCES.find((s) => s.partition_key === "dealer_stock_entry")!;
|
||||
const preview = await previewVehiqOltpSource(source, query, repo, {
|
||||
sourceId: "vehiq_oltp",
|
||||
tenantId: "t_vehiq",
|
||||
projectId: "vehiq",
|
||||
});
|
||||
expect(preview.would_read).toBe(0);
|
||||
expect(preview.exhausted).toBe(true);
|
||||
// Never committed — getCheckpoint after the preview still reports nothing.
|
||||
expect(await repo.getCheckpoint("vehiq_oltp", "dealer_stock_entry")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
// Column shape verified live against production (server10, vehiq-db,
|
||||
// information_schema.columns, read-only, 2026-09-25) — see the doc comment
|
||||
// on VEHIQ_OLTP_SOURCES for why there is no `updated_at` here.
|
||||
const DEALER_STOCK_ROWS: SourceRow[] = [
|
||||
{ id: "d1", tenant_id: "t_vehiq", vehicle_id: "v1", lot_location: "Lot A", floorplan_status: "owned", listed_at: "2026-09-01T00:00:00.000Z", sold_at: null },
|
||||
{ id: "d2", tenant_id: "t_vehiq", vehicle_id: "v2", lot_location: "Lot A", floorplan_status: "financed", listed_at: "2026-09-02T00:00:00.000Z", sold_at: null },
|
||||
{ id: "d3", tenant_id: "t_vehiq", vehicle_id: "v1", lot_location: "Lot B", floorplan_status: "owned", listed_at: "2026-09-03T00:00:00.000Z", sold_at: "2026-09-04T00:00:00.000Z" },
|
||||
];
|
||||
const VEHICLE_ROWS: SourceRow[] = [
|
||||
{ vehicle_id: "v1", make: "Volvo", model: "XC60", model_year: 2022, fuel_type: "electric" },
|
||||
{ vehicle_id: "v2", make: "Toyota", model: "Corolla", model_year: 2021, fuel_type: "petrol" },
|
||||
{ vehicle_id: "v9", make: "Never", model: "Referenced", model_year: 2020, fuel_type: "diesel" }, // must never be queried
|
||||
];
|
||||
|
||||
describe("runVehiqOltpIngestion — watermark/limit correctness with real data", () => {
|
||||
it("emits envelopes for every row, in watermark order, and derives exactly the referenced vehicle ids", async () => {
|
||||
const { query } = fixtureVehiqDb({
|
||||
"app.dealer_stock_entry": DEALER_STOCK_ROWS,
|
||||
"app.listing": [],
|
||||
"app.deal": [],
|
||||
"vehiq.vehicle": VEHICLE_ROWS,
|
||||
});
|
||||
const seen: IngestionEnvelope[] = [];
|
||||
const dimensionSeen: IngestionEnvelope[] = [];
|
||||
const deps = baseDeps(query, async (e) => void seen.push(...e));
|
||||
deps.dimensionSink = async (e) => void dimensionSeen.push(...e);
|
||||
|
||||
const summary = await runVehiqOltpIngestion(deps);
|
||||
|
||||
const dealerEnvelopes = seen.filter((e) => e.partition_key === "dealer_stock_entry");
|
||||
expect(dealerEnvelopes.map((e) => e.record_key)).toEqual(["d1", "d2", "d3"]);
|
||||
expect(summary.referenced_vehicle_ids).toBe(2); // v1, v2 — deduped from d1/d2/d3
|
||||
expect(summary.dimension?.status).toBe("succeeded");
|
||||
expect(dimensionSeen.map((e) => e.record_key).sort()).toEqual(["v1", "v2"]);
|
||||
expect(dimensionSeen.map((e) => e.record_key)).not.toContain("v9");
|
||||
});
|
||||
});
|
||||
|
||||
describe("runVehiqOltpIngestion — resume from checkpoint after an interruption", () => {
|
||||
it("a sink that dies mid-run leaves the checkpoint unmoved for the failed page; the NEXT run resumes with no gap and no duplicate landing", async () => {
|
||||
const { query } = fixtureVehiqDb({
|
||||
"app.dealer_stock_entry": DEALER_STOCK_ROWS,
|
||||
"app.listing": [],
|
||||
"app.deal": [],
|
||||
"vehiq.vehicle": VEHICLE_ROWS,
|
||||
});
|
||||
const repo = new InMemoryIngestionRepository();
|
||||
const schemas = new SchemaRegistry(repo);
|
||||
const landed: IngestionEnvelope[] = [];
|
||||
let call = 0;
|
||||
|
||||
// First run: the sink accepts page 1 (2 rows, page_size=2) then throws on
|
||||
// page 2 — simulating the process dying after the first page's checkpoint
|
||||
// committed but before the second page's did.
|
||||
const crashingSink = async (batch: IngestionEnvelope[]): Promise<void> => {
|
||||
call++;
|
||||
if (call === 2) throw new Error("simulated crash mid-run");
|
||||
landed.push(...batch);
|
||||
};
|
||||
// Matches the generic runner's own documented behaviour (tests/p0/
|
||||
// ingestion-runner.test.ts "a run that dies inside the sink..."): a
|
||||
// throwing sink REJECTS the run, it does not resolve with status:"failed".
|
||||
// dealer_stock_entry is the FIRST source this pipeline processes, so its
|
||||
// page-2 crash is what aborts runVehiqOltpIngestion() itself here.
|
||||
await expect(
|
||||
runVehiqOltpIngestion({
|
||||
repo,
|
||||
schemas,
|
||||
sourceQuery: query,
|
||||
oltpSink: crashingSink,
|
||||
dimensionSink: async () => undefined,
|
||||
tenantId: "t_vehiq",
|
||||
projectId: "vehiq",
|
||||
pageSize: 2,
|
||||
}),
|
||||
).rejects.toThrow(/simulated crash mid-run/);
|
||||
expect(landed.map((e) => e.record_key)).toEqual(["d1", "d2"]); // page 1 landed before the crash
|
||||
// The durable checkpoint reflects page 1 only — page 2's checkpoint never committed.
|
||||
const afterCrash = await repo.getCheckpoint("vehiq_oltp_dealer_stock_entry", "dealer_stock_entry");
|
||||
expect(afterCrash?.records_committed).toBe(2);
|
||||
|
||||
// Second run, same repo (durable checkpoint survives the "restart"): must
|
||||
// NOT re-deliver d1/d2, and MUST deliver d3.
|
||||
const run2 = await runVehiqOltpIngestion({
|
||||
repo,
|
||||
schemas,
|
||||
sourceQuery: query,
|
||||
oltpSink: async (batch) => void landed.push(...batch),
|
||||
dimensionSink: async () => undefined,
|
||||
tenantId: "t_vehiq",
|
||||
projectId: "vehiq",
|
||||
pageSize: 2,
|
||||
});
|
||||
const dealerReport2 = run2.oltp.find((r) => r.partitions[0]?.partition_key === "dealer_stock_entry");
|
||||
expect(dealerReport2?.status).toBe("succeeded");
|
||||
expect(landed.map((e) => e.record_key)).toEqual(["d1", "d2", "d3"]); // exactly once each, no gap, no dupe
|
||||
});
|
||||
});
|
||||
@@ -114,6 +114,21 @@ describe("checkEngine", () => {
|
||||
});
|
||||
expect(result.status).toBe("error");
|
||||
});
|
||||
|
||||
it("REGRESSION: reports error (with the abort message in detail) when the probe times out via AbortController -- the exact 2026-09-30 prod symptom", async () => {
|
||||
// Mätt i prod 2026-09-30: ENGINE_URL satt i Coolify-env mot en
|
||||
// oprovisionerad host -> AbortController:n nbröt efter 2 s och
|
||||
// /api/health rapporterade {"engine":"error",
|
||||
// "detail":{"engine":"This operation was aborted"}}. Kollen ska
|
||||
// vara ÄRLIG här: URL:en ÄR satt, så detta är error -- inte
|
||||
// not_configured och aldrig ok.
|
||||
process.env.ENGINE_URL = "http://0pt-engine:8000";
|
||||
const result = await checkEngine(async () => {
|
||||
throw new DOMException("This operation was aborted", "AbortError");
|
||||
});
|
||||
expect(result.status).toBe("error");
|
||||
expect(result.detail).toContain("aborted");
|
||||
});
|
||||
});
|
||||
|
||||
describe("checkStripe", () => {
|
||||
@@ -179,7 +194,31 @@ describe("hasDegradedDependency / isReady", () => {
|
||||
expect(isReady(deps({ db: { status: "error" } }))).toBe(false);
|
||||
});
|
||||
|
||||
it("isReady is true once db is ok, regardless of optional integrations", () => {
|
||||
it("isReady is true once db is ok and no configured integration is failing", () => {
|
||||
expect(isReady(deps({ db: { status: "ok" } }))).toBe(true);
|
||||
});
|
||||
|
||||
it("isReady stays true for integrations that are merely absent (0pt boots without them)", () => {
|
||||
expect(
|
||||
isReady(
|
||||
deps({
|
||||
db: { status: "ok" },
|
||||
engine: { status: "not_configured" },
|
||||
stripe: { status: "not_configured" },
|
||||
queue: { status: "not_integrated" },
|
||||
}),
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("REGRESSION: isReady is false when a CONFIGURED integration is failing its live round trip", () => {
|
||||
// Mätt i prod 2026-09-25: ENGINE_URL pekade på 0pt-engine:8000, en värd
|
||||
// utan DNS-post någonstans i flottan, och /api/ready svarade ändå
|
||||
// {"ready":true,...,"engine":"error"}. En konfigurerad-men-död motor är
|
||||
// inte "valfri" -- någon har bett om den och den svarar inte.
|
||||
expect(isReady(deps({ db: { status: "ok" }, engine: { status: "error", detail: "fetch failed" } }))).toBe(
|
||||
false,
|
||||
);
|
||||
expect(isReady(deps({ db: { status: "ok" }, stripe: { status: "error", detail: "bad key" } }))).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
/**
|
||||
* Klient-adaptern (kernel-transport över host-API): offline = no-op + varning
|
||||
* (INGEN fetch, inget tyst förlorat), 409 -> ConflictError med aktuell
|
||||
* version, presence via kernelns upsertPresence.
|
||||
*/
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import {
|
||||
OfflineError,
|
||||
OrderNotesClient,
|
||||
OrderNotesTransport,
|
||||
} from "@/lib/order-notes-client";
|
||||
|
||||
describe("OrderNotesTransport (kernel CollaborationTransport)", () => {
|
||||
it("send() i offline-läge kastar OfflineError utan nätverksanrop (no-op, ej tyst)", async () => {
|
||||
const t = new OrderNotesTransport("admin-simon");
|
||||
t.setOffline(true);
|
||||
await expect(
|
||||
t.send({ id: "n1", actorId: "x", clock: 1, type: "note.update", payload: { body: "y" } }),
|
||||
).rejects.toBeInstanceOf(OfflineError);
|
||||
});
|
||||
|
||||
it("connect/disconnect växlar presence via upsertPresence (online -> offline)", async () => {
|
||||
const t = new OrderNotesTransport("admin-simon");
|
||||
await t.connect();
|
||||
expect(t.getPresence().find((p) => p.participantId === "admin-simon")?.status).toBe("online");
|
||||
await t.disconnect();
|
||||
expect(t.getPresence().find((p) => p.participantId === "admin-simon")?.status).toBe("offline");
|
||||
});
|
||||
|
||||
it("observeRemote advanserar Lamport-klockan och notifyar listeners", async () => {
|
||||
const t = new OrderNotesTransport("me");
|
||||
const seen: string[] = [];
|
||||
t.onOperation((op) => seen.push(`${op.id}@${op.clock}`));
|
||||
t.observeRemote({ id: "n1", actorId: "anna", clock: 4, type: "note.upsert", payload: { body: "x" } });
|
||||
expect(seen).toEqual(["n1@4"]);
|
||||
expect(t.getPresence().find((p) => p.participantId === "anna")?.status).toBe("online");
|
||||
});
|
||||
});
|
||||
|
||||
describe("OrderNotesClient över host-API", () => {
|
||||
beforeEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("offline-läge: add() är no-op — fetch anropas EJ, OfflineError kastas (ingen tyst förlust)", async () => {
|
||||
const fetchSpy = vi.spyOn(globalThis, "fetch");
|
||||
const client = new OrderNotesClient("admin-simon");
|
||||
client.transport.setOffline(true);
|
||||
await expect(client.add("order-1", "utkast")).rejects.toBeInstanceOf(OfflineError);
|
||||
expect(fetchSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("409 från servern -> ConflictError med serverns aktuella version", async () => {
|
||||
vi.spyOn(globalThis, "fetch").mockResolvedValue(
|
||||
new Response(JSON.stringify({ error: "version_conflict", current_version: 3 }), { status: 409 }),
|
||||
);
|
||||
const client = new OrderNotesClient("admin-simon");
|
||||
await expect(
|
||||
client.update(
|
||||
{ note_id: "n1", order_id: "o1", author_id: "a", body: "old", version: 1, clock: 1, updated_at: "" },
|
||||
"new",
|
||||
1,
|
||||
),
|
||||
).rejects.toMatchObject({ currentVersion: 3 });
|
||||
});
|
||||
|
||||
it("load(): varje servernot replays som kernel-operation (klockor advanserar)", async () => {
|
||||
vi.spyOn(globalThis, "fetch").mockResolvedValue(
|
||||
new Response(
|
||||
JSON.stringify({
|
||||
notes: [
|
||||
{ note_id: "n1", order_id: "o1", author_id: "anna", body: "A", version: 1, clock: 2, updated_at: "" },
|
||||
{ note_id: "n2", order_id: "o1", author_id: "bosse", body: "B", version: 2, clock: 5, updated_at: "" },
|
||||
],
|
||||
}),
|
||||
{ status: 200 },
|
||||
),
|
||||
);
|
||||
const client = new OrderNotesClient("admin-simon");
|
||||
const { notes, versions } = await client.load("o1");
|
||||
expect(notes).toHaveLength(2);
|
||||
expect(versions.get("n2")).toBe(2);
|
||||
expect(client.transport.getPresence().find((p) => p.participantId === "bosse")).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,105 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import {
|
||||
ORDER_STATUSES,
|
||||
NEXT_STATUS,
|
||||
isKnownOrderStatus,
|
||||
nextOrderStatus,
|
||||
resolveAdvanceAction,
|
||||
buildOrderInboxItem,
|
||||
} from "@/lib/order-flow";
|
||||
|
||||
describe("order-flow — server-renderad advance-resolution (ADR-0001)", () => {
|
||||
it("flödets fem steg har nästa status", () => {
|
||||
expect(NEXT_STATUS.draft).toBe("quoted");
|
||||
expect(NEXT_STATUS.accepted).toBe("delivered");
|
||||
});
|
||||
|
||||
it("aktivt steg → available", () => {
|
||||
expect(resolveAdvanceAction("draft")).toMatchObject({ state: "available" });
|
||||
expect(resolveAdvanceAction("review")).toMatchObject({ state: "available" });
|
||||
});
|
||||
|
||||
it("terminalt steg → read_only/read_only_resource (inte fake available)", () => {
|
||||
expect(resolveAdvanceAction("delivered")).toMatchObject({ state: "read_only", reason: "read_only_resource" });
|
||||
expect(resolveAdvanceAction("cancelled")).toMatchObject({ state: "read_only", reason: "read_only_resource" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("order-flow — kanonisk statusmaskin (ADR-0002, C3)", () => {
|
||||
const TERMINAL: ReadonlySet<string> = new Set(["delivered", "cancelled"]);
|
||||
const EXPECTED_NEXT: Record<string, string | null> = {
|
||||
draft: "quoted",
|
||||
quoted: "in_production",
|
||||
in_production: "review",
|
||||
review: "accepted",
|
||||
accepted: "delivered",
|
||||
delivered: null,
|
||||
cancelled: null,
|
||||
};
|
||||
|
||||
it("ORDER_STATUSES är utömmande: varje känd status → korrekt nästa/read_only", () => {
|
||||
expect(ORDER_STATUSES).toHaveLength(7);
|
||||
for (const status of ORDER_STATUSES) {
|
||||
expect(isKnownOrderStatus(status), `känd status: ${status}`).toBe(true);
|
||||
if (TERMINAL.has(status)) {
|
||||
expect(nextOrderStatus(status), `${status} är terminal`).toBeNull();
|
||||
expect(resolveAdvanceAction(status)).toMatchObject({ state: "read_only", reason: "read_only_resource" });
|
||||
expect(buildOrderInboxItem({ order_id: "abcd1234-5678", status, flow: "custom" })).toBeNull();
|
||||
} else {
|
||||
expect(nextOrderStatus(status)).toBe(EXPECTED_NEXT[status]);
|
||||
expect(resolveAdvanceAction(status)).toMatchObject({ state: "available" });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it("flödet är en linjedraft→…→delivered-kedja utan genvägar", () => {
|
||||
let cursor: string | null = "draft";
|
||||
const walked: string[] = [];
|
||||
while (cursor) {
|
||||
walked.push(cursor);
|
||||
cursor = nextOrderStatus(cursor);
|
||||
}
|
||||
expect(walked).toEqual(["draft", "quoted", "in_production", "review", "accepted", "delivered"]);
|
||||
});
|
||||
|
||||
it("okänd status (fri text ur DB) → read_only, ingen nästa, ingen inbox-post (guardad)", () => {
|
||||
for (const status of ["klar", "Klar", "done", "", "in prod", "cancelled "]) {
|
||||
expect(isKnownOrderStatus(status)).toBe(false);
|
||||
expect(nextOrderStatus(status)).toBeNull();
|
||||
expect(resolveAdvanceAction(status)).toMatchObject({ state: "read_only", reason: "read_only_resource" });
|
||||
expect(buildOrderInboxItem({ order_id: "abcd1234-5678", status, flow: "custom" })).toBeNull();
|
||||
}
|
||||
});
|
||||
|
||||
it("cancelled ingår i unionen (används som terminal status i tests/produkten)", () => {
|
||||
expect(ORDER_STATUSES).toContain("cancelled");
|
||||
});
|
||||
});
|
||||
|
||||
describe("order-flow — inbox-projektion (våg 3.3)", () => {
|
||||
const order = (over: Partial<{ order_id: string; status: string; flow: string; total_ore: number }> = {}) => ({
|
||||
order_id: "abcd1234-5678",
|
||||
status: "review",
|
||||
flow: "custom",
|
||||
total_ore: 500000,
|
||||
...over,
|
||||
});
|
||||
|
||||
it("review-order → approval-post med hög prioritet och advance-action", () => {
|
||||
const item = buildOrderInboxItem(order());
|
||||
expect(item).toMatchObject({ kind: "approval", priority: "high", state: "open" });
|
||||
expect(item!.actions?.[0].label).toContain("accepted");
|
||||
expect(item!.entity).toMatchObject({ type: "order", id: "abcd1234-5678" });
|
||||
});
|
||||
|
||||
it("levererad order → ingen post (inkorgen är öppna arbetsobjekt)", () => {
|
||||
expect(buildOrderInboxItem(order({ status: "delivered" }))).toBeNull();
|
||||
expect(buildOrderInboxItem(order({ status: "cancelled" }))).toBeNull();
|
||||
});
|
||||
|
||||
it("draft-order → task med normal prioritet", () => {
|
||||
const item = buildOrderInboxItem(order({ status: "draft" }));
|
||||
expect(item).toMatchObject({ kind: "task", priority: "normal" });
|
||||
expect(item!.subject).toContain("draft");
|
||||
});
|
||||
});
|
||||
@@ -29,7 +29,7 @@ describe("DocumentCaptureService.captureFromMailbox", () => {
|
||||
it("downloads PDF attachments, persists bytes, registers a ST0RE artifact, enqueues an N0D extraction job, and emits AUD0 events", async () => {
|
||||
const pdfBytes = Buffer.from("%PDF-1.4 real bytes for test");
|
||||
const pdfBase64 = pdfBytes.toString("base64");
|
||||
const aud0Calls: { url: string; body: any }[] = [];
|
||||
const aud0Calls: { url: string; body: Record<string, unknown> }[] = [];
|
||||
|
||||
const fetchMock = vi.fn(async (url: string, init?: RequestInit) => {
|
||||
if (url.includes("login.microsoftonline.com")) return jsonResponse({ access_token: "tok", expires_in: 3600 });
|
||||
@@ -93,13 +93,13 @@ describe("DocumentCaptureService.captureFromMailbox", () => {
|
||||
const storedBytes = await service.getDocumentBytes(doc.document_id);
|
||||
expect(storedBytes.equals(pdfBytes)).toBe(true);
|
||||
|
||||
const captured = aud0Calls.find((c) => c.body?.event_type === "0pt.document_captured");
|
||||
const captured = aud0Calls.find((c) => c.body?.event_type === "0pt.document_captured") as { url: string; body: { event_type: string; capability_id: string; payload: { filename: string } } } | undefined;
|
||||
expect(captured).toBeTruthy();
|
||||
expect(captured!.body.payload.filename).toBe("doc.pdf");
|
||||
expect(captured!.body.capability_id).toBe("0pt.capture_document");
|
||||
// Lineage now goes to AUD0's REAL ingest contract: an OpenLineage
|
||||
// RunEvent on /v1/aud0/openlineage.
|
||||
const lineage = aud0Calls.find((c) => c.url.includes("openlineage"));
|
||||
const lineage = aud0Calls.find((c) => c.url.includes("openlineage")) as { url: string; body: { outputs: { name: string }[] } } | undefined;
|
||||
expect(lineage).toBeTruthy();
|
||||
expect(lineage!.body.outputs[0].name).toContain(doc.document_id);
|
||||
});
|
||||
|
||||
@@ -18,7 +18,7 @@ function jsonResponse(body: unknown, opts: { status?: number; headers?: Record<s
|
||||
|
||||
describe("DocumentExtractionService — evidence-based extraction", () => {
|
||||
let tmpDir: string;
|
||||
let aud0Calls: { url: string; body: any }[];
|
||||
let aud0Calls: { url: string; body: Record<string, unknown> }[];
|
||||
|
||||
beforeEach(() => {
|
||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "0pt-blob-"));
|
||||
@@ -133,7 +133,7 @@ describe("DocumentExtractionService — evidence-based extraction", () => {
|
||||
const updatedDoc = await captureService.getDocument(document.document_id);
|
||||
expect(updatedDoc.status).toBe("extracted");
|
||||
|
||||
const extractedEvent = aud0Calls.find((c) => c.body?.event_type === "0pt.document_extracted");
|
||||
const extractedEvent = aud0Calls.find((c) => c.body?.event_type === "0pt.document_extracted") as { url: string; body: { event_type: string; capability_id: string; payload: { fields_found_count: number; fields_total_count: number } } } | undefined;
|
||||
expect(extractedEvent).toBeTruthy();
|
||||
expect(extractedEvent!.body.capability_id).toBe("0pt.extract_document");
|
||||
expect(extractedEvent!.body.payload.fields_found_count).toBe(2);
|
||||
|
||||
@@ -2,6 +2,7 @@ import {describe,it,expect} from "vitest";
|
||||
|
||||
describe("0pt Enterprise Data OS Expansion (PROMPT 2)",()=>{
|
||||
it("iceberg_table schema has required fields",()=>{
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports -- fixture-laddning via require
|
||||
const schema=require("../../packages/schemas/0pt.iceberg_table.schema.json");
|
||||
expect(schema.required).toContain("table_id");
|
||||
expect(schema.required).toContain("table_name");
|
||||
@@ -10,6 +11,7 @@ describe("0pt Enterprise Data OS Expansion (PROMPT 2)",()=>{
|
||||
});
|
||||
|
||||
it("signal schema has entity and strength",()=>{
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports -- fixture-laddning via require
|
||||
const schema=require("../../packages/schemas/0pt.signal.schema.json");
|
||||
expect(schema.required).toContain("signal_id");
|
||||
expect(schema.required).toContain("signal_type");
|
||||
@@ -18,6 +20,7 @@ describe("0pt Enterprise Data OS Expansion (PROMPT 2)",()=>{
|
||||
});
|
||||
|
||||
it("lineage_event schema has input/output assets",()=>{
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports -- fixture-laddning via require
|
||||
const schema=require("../../packages/schemas/0pt.lineage_event.schema.json");
|
||||
expect(schema.required).toContain("input_assets");
|
||||
expect(schema.required).toContain("output_assets");
|
||||
@@ -32,6 +35,7 @@ describe("0pt Enterprise Data OS Expansion (PROMPT 2)",()=>{
|
||||
// predated that spec. See src/types/source-rate-policy.ts and
|
||||
// src/services/source-rate-policy-service.ts for the real, tested implementation.
|
||||
it("source_rate_policy schema has rate limits",()=>{
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports -- fixture-laddning via require
|
||||
const schema=require("../../packages/schemas/0pt.source_rate_policy.schema.json");
|
||||
expect(schema.required).toContain("rate_limit_per_minute");
|
||||
expect(schema.required).toContain("crawl_budget");
|
||||
|
||||
@@ -59,6 +59,7 @@ function baseRequest(overrides: Partial<ExternalWriteRequest> = {}): ExternalWri
|
||||
/** Stubs global fetch to answer CL0UD policy evaluation (always "allow" unless `denyOrApproval`
|
||||
* is set) and swallow AUD0 event/lineage POSTs, recording them for assertions. */
|
||||
function stubCl0udAllowAndAud0(opts: { decision?: "allow" | "deny" | "approval_required" } = {}) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- AUD0-ingest-body är fri nyttolast
|
||||
const aud0Calls: { url: string; body: any }[] = [];
|
||||
const fetchMock = vi.fn(async (url: string, init?: RequestInit) => {
|
||||
// REAL CL0UD wire contract (cl0ud main): POST /api/policy/evaluate with a
|
||||
|
||||
@@ -257,6 +257,7 @@ describe("OdooBillsAdapter — partner resolution wired into write()", () => {
|
||||
|
||||
it("calls the injected matchPartner when partner_id is absent but vendor_org_nr is present, and forwards the resolved partner_id to Odoo", async () => {
|
||||
const matchPartner = vi.fn(async () => ({ partner_id: 99, partner_name: "Acme AB" }));
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- AUD0-ingest-body är fri nyttolast
|
||||
let capturedBody: any;
|
||||
const fetchMock = vi.fn(async (_url: string, init?: RequestInit) => {
|
||||
capturedBody = JSON.parse(init!.body as string);
|
||||
@@ -278,6 +279,7 @@ describe("OdooBillsAdapter — partner resolution wired into write()", () => {
|
||||
|
||||
it("posts without a partner_id when no match is found — 'skickar utan partner-koppling'", async () => {
|
||||
const matchPartner = vi.fn(async () => ({}));
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- AUD0-ingest-body är fri nyttolast
|
||||
let capturedBody: any;
|
||||
const fetchMock = vi.fn(async (_url: string, init?: RequestInit) => {
|
||||
capturedBody = JSON.parse(init!.body as string);
|
||||
|
||||
Reference in New Issue
Block a user