-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharchive-forwarder-strategy-contract.test.ts
More file actions
170 lines (154 loc) · 5.31 KB
/
Copy patharchive-forwarder-strategy-contract.test.ts
File metadata and controls
170 lines (154 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { describe, expect, test } from "bun:test";
import { createHash } from "node:crypto";
import {
classifyStrategyArchiveBatch,
STRATEGY_ARCHIVE_SOURCES,
validateStrategyArchiveBatch,
} from "../services/archive-forwarder/strategy-contract";
import fixture from "./fixtures/archive_forwarder_envelope.json";
import makerOrchestratorFixture from "./fixtures/maker_orchestrator_archive_envelope.json";
const PINNED_MAKER_FIXTURE_SHA256 =
"5c9fd679a5a05ebce5f5158f4cc376360f24a34d9a07edeee43e94e564db3ee7";
const strategyTables = [
"strategy_data.policy_evaluation_events",
"strategy_data.strategy_policy_snapshots",
"strategy_data.market_identity",
"strategy_data.symbol_mapping",
"strategy_data.inventory_settlement_events",
] as const;
function v2Row(table: (typeof strategyTables)[number], seq: number) {
return {
table,
row: {
source: "hb_runtime",
deployment_id: "maker-a",
schema_version: "2",
producer_id: "hb_runtime:maker-a:controller-a",
producer_run_id: "run-a",
stream_name: table,
stream_seq: 1,
seq,
archive_event_id: `run-a:${table}:1`,
},
};
}
function batch(rows: unknown[]) {
return { source: "hb_runtime", deployment_id: "maker-a", rows };
}
describe("Maker strategy archive contract", () => {
test("pins the exact Maker v2 fixture bytes and accepts every row", async () => {
const bytes = await Bun.file(
new URL("./fixtures/archive_forwarder_envelope.json", import.meta.url),
).arrayBuffer();
expect(
createHash("sha256").update(new Uint8Array(bytes)).digest("hex"),
).toBe(PINNED_MAKER_FIXTURE_SHA256);
expect(validateStrategyArchiveBatch(fixture).ok).toBe(true);
});
test("accepts one v2 row for every approved strategy table", () => {
const rows = strategyTables.map((table, index) => v2Row(table, index + 1));
expect(validateStrategyArchiveBatch(batch(rows))).toEqual({ ok: true });
});
test("classifies both admitted strategy producers", () => {
expect(STRATEGY_ARCHIVE_SOURCES).toEqual(
new Set(["hb_runtime", "maker_orchestrator"]),
);
expect(classifyStrategyArchiveBatch(fixture)).toBe("strategy");
expect(classifyStrategyArchiveBatch(makerOrchestratorFixture)).toBe(
"strategy",
);
expect(validateStrategyArchiveBatch(makerOrchestratorFixture)).toEqual({
ok: true,
});
});
test.each([
"unknown_runtime",
"broker_read",
"broker_write",
])("rejects %s carrying strategy rows", (source) => {
expect(
classifyStrategyArchiveBatch({
...makerOrchestratorFixture,
source,
}),
).toBe("invalid_strategy_source");
});
test("rejects cross-producer row and envelope provenance", () => {
const hbEnvelopeWithMakerRow = structuredClone(fixture);
hbEnvelopeWithMakerRow.rows[0].row.source = "maker_orchestrator";
expect(validateStrategyArchiveBatch(hbEnvelopeWithMakerRow).ok).toBe(false);
const makerEnvelopeWithHbRow = structuredClone(makerOrchestratorFixture);
makerEnvelopeWithHbRow.rows[0].row.source = "hb_runtime";
expect(validateStrategyArchiveBatch(makerEnvelopeWithHbRow).ok).toBe(false);
});
test.each([
undefined,
"",
"1",
])("accepts legacy schema version %p without v2 identity", (schemaVersion) => {
const row: Record<string, unknown> = {
source: "hb_runtime",
deployment_id: "maker-a",
seq: 1,
};
if (schemaVersion !== undefined) row.schema_version = schemaVersion;
expect(
validateStrategyArchiveBatch(
batch([{ table: "strategy_data.policy_evaluation_events", row }]),
).ok,
).toBe(true);
});
test.each([
["unknown version", { schema_version: "3" }],
["missing producer id", { producer_id: "" }],
["zero stream sequence", { stream_seq: 0 }],
["fractional seq", { seq: 1.5 }],
])("rejects v2 %s", (_name, override) => {
const entry = v2Row("strategy_data.policy_evaluation_events", 1);
Object.assign(entry.row, override);
expect(validateStrategyArchiveBatch(batch([entry])).ok).toBe(false);
});
test("accepts lossless decimal UInt64 strings and rejects overflow", () => {
const entry = v2Row("strategy_data.policy_evaluation_events", 1);
Object.assign(entry.row, {
stream_seq: "18446744073709551615",
seq: "18446744073709551615",
});
expect(validateStrategyArchiveBatch(batch([entry])).ok).toBe(true);
entry.row.seq = "18446744073709551616";
expect(validateStrategyArchiveBatch(batch([entry])).ok).toBe(false);
});
test("rejects empty envelope identity and empty rows", () => {
expect(validateStrategyArchiveBatch({ ...batch([]), source: " " }).ok).toBe(
false,
);
expect(
validateStrategyArchiveBatch({ ...batch([]), deployment_id: "" }).ok,
).toBe(false);
expect(validateStrategyArchiveBatch(batch([])).ok).toBe(false);
});
test("rejects mixed tables and row deployment mismatch", () => {
const strategy = v2Row("strategy_data.policy_evaluation_events", 1);
expect(
validateStrategyArchiveBatch(
batch([
strategy,
{ table: "market_data.cex_trades", row: { source: "hb_runtime" } },
]),
).ok,
).toBe(false);
strategy.row.deployment_id = "maker-b";
expect(validateStrategyArchiveBatch(batch([strategy])).ok).toBe(false);
});
test("classifies non-strategy traffic for the direct path", () => {
expect(
classifyStrategyArchiveBatch({
source: "broker_read",
deployment_id: "broker-a",
rows: [
{ table: "market_data.cex_trades", row: { source: "broker_read" } },
],
}),
).toBe("direct");
});
});