Skip to content

Commit 285f047

Browse files
committed
test(dogfood): record-currency PROBE-A / PROBE-R on both SQLite drivers
Claude-Session: https://claude.ai/code/session_01Utp6CboxRwQPbQZnhxyuzt Co-authored-by: Claude <noreply@anthropic.com>
1 parent 696c3f2 commit 285f047

2 files changed

Lines changed: 149 additions & 1 deletion

File tree

‎packages/objectql/src/validation/record-currency-code.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export class RecordCurrencyCodeRequiredError extends Error {
106106
+ `holds an amount in the record's own currency (currencyMode: 'record'), and its currency code field `
107107
+ `'${first.codeField}' is blank`
108108
+ (hits.length > 1 ? ` (also: ${hits.slice(1).map((h) => `'${h.codeField}' for '${h.amountField}'`).join(', ')})` : '')
109-
+ `. Nothing was written. Write the record's ISO 4217 code (for example 'USD') into '${first.codeField}' `
109+
+ `. The row was not written. Write the record's ISO 4217 code (for example 'USD') into '${first.codeField}' `
110110
+ `in the same write, or leave '${first.amountField}' empty. The server never fills a currency in: a form `
111111
+ `pre-fills the organization's currency, and the stored code is always the one submitted.`,
112112
);
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
//
3+
// [ADR-0144 D6, D8 rule 4] The measurement round's probes (epic #8345, comment
4+
// 6041378776) as fixtures, on the real SQLite drivers, with the record currency
5+
// DECLARED (`currencyMode: 'record'`, the code in `currency_code`):
6+
//
7+
// - PROBE-A: `sum` over a USD row of 100 and an EUR row of 100 answered `200`
8+
// under one label. Now refused `INVALID_FIELD` / 400 unless grouped by the
9+
// code field; grouped (PROBE-C), each currency's total comes back on its own.
10+
// - PROBE-R: a `summary` roll-up over child rows of USD 100 and JPY 15000
11+
// stored `15100`. Now the roll-up is refused `INVALID_METADATA` / 422 when
12+
// the app registers.
13+
// - The write side: an amount with no code is refused
14+
// `ERR_CURRENCY_CODE_REQUIRED` / 400, and the table stays empty.
15+
//
16+
// The engine-door cells over the in-memory aggregation face live beside the
17+
// guards in `packages/objectql/src/record-currency-guards.test.ts`. ⛔ No
18+
// driver-memory arm: its test consumers are a closed census
19+
// (`check:driver-memory-census`), and the project's test backends are sqlite
20+
// `:memory:`.
21+
22+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
23+
import { ObjectQL } from '@objectstack/objectql';
24+
import { SqlDriver } from '@objectstack/driver-sql';
25+
import { SqliteWasmDriver } from '@objectstack/driver-sqlite-wasm';
26+
import { installDefaultOrganization } from './default-organization.testkit.js';
27+
28+
const DRIVERS = [
29+
{
30+
name: 'driver-sql (better-sqlite3 :memory:)',
31+
make: (): SqlDriver =>
32+
new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }),
33+
},
34+
{
35+
name: 'driver-sqlite-wasm (:memory:)',
36+
make: (): SqlDriver => new SqliteWasmDriver({ filename: ':memory:' }),
37+
},
38+
];
39+
40+
const SYS = { context: { isSystem: true, userId: 'usr_system' } };
41+
42+
const RECORD = { currencyMode: 'record', currencyField: 'currency_code' } as const;
43+
const CODE = { name: 'currency_code', type: 'text', valueDomain: 'iso_4217_currency' };
44+
45+
const FX_INVOICE = {
46+
name: 'fx_invoice',
47+
label: 'FX invoice',
48+
fields: {
49+
name: { name: 'name', type: 'text' },
50+
currency_code: CODE,
51+
amount: { name: 'amount', type: 'currency', currencyConfig: RECORD },
52+
},
53+
};
54+
55+
const appWith = (objects: unknown[]) => ({
56+
id: 'com.objectstack.dogfood.record-currency-probes',
57+
name: 'Record currency probes',
58+
version: '1.0.0',
59+
type: 'plugin',
60+
scope: 'system',
61+
objects,
62+
});
63+
64+
interface Refusal { code?: unknown; status?: unknown; field?: unknown; name?: unknown; message?: unknown }
65+
const envelope = (e: Refusal) => ({ code: e.code, status: e.status, field: e.field });
66+
67+
async function refusalOf(run: () => unknown): Promise<Refusal> {
68+
try {
69+
await run();
70+
} catch (e) {
71+
return e as Refusal;
72+
}
73+
throw new Error('expected the engine to refuse, and it answered');
74+
}
75+
76+
describe.each(DRIVERS)('record-currency probes: $name', ({ make }) => {
77+
let driver: SqlDriver;
78+
let engine: ObjectQL;
79+
80+
beforeEach(async () => {
81+
driver = make();
82+
engine = new ObjectQL();
83+
engine.registerDriver(driver, true);
84+
await engine.init();
85+
});
86+
87+
afterEach(async () => {
88+
await engine.destroy();
89+
});
90+
91+
async function boot(objects: unknown[]) {
92+
engine.registerApp(appWith(objects));
93+
await engine.syncSchemas();
94+
await installDefaultOrganization(engine);
95+
}
96+
97+
it('PROBE-A: an ungrouped sum over USD 100 + EUR 100 is refused; grouped by the code, each currency totals alone', async () => {
98+
await boot([FX_INVOICE]);
99+
await engine.insert('fx_invoice', { name: 'i1', currency_code: 'USD', amount: 100 }, SYS);
100+
await engine.insert('fx_invoice', { name: 'i2', currency_code: 'EUR', amount: 100 }, SYS);
101+
102+
for (const fn of ['sum', 'avg', 'min', 'max']) {
103+
const err = await refusalOf(() => engine.aggregate('fx_invoice', {
104+
aggregations: [{ function: fn, field: 'amount', alias: 'v' }],
105+
} as never, SYS));
106+
expect(envelope(err), fn).toEqual({ code: 'INVALID_FIELD', status: 400, field: 'amount' });
107+
}
108+
109+
const rows = await engine.aggregate('fx_invoice', {
110+
groupBy: ['currency_code'],
111+
aggregations: [{ function: 'sum', field: 'amount', alias: 'total' }],
112+
} as never, SYS);
113+
const byCode = Object.fromEntries((rows as Array<Record<string, unknown>>).map((r) => [r.currency_code, Number(r.total)]));
114+
expect(byCode).toEqual({ USD: 100, EUR: 100 });
115+
});
116+
117+
it('the write side: an amount with no code is refused and nothing is stored', async () => {
118+
await boot([FX_INVOICE]);
119+
const err = await refusalOf(() => engine.insert('fx_invoice', { name: 'i1', amount: 100 }, SYS));
120+
expect(envelope(err)).toEqual({ code: 'ERR_CURRENCY_CODE_REQUIRED', status: 400, field: 'currency_code' });
121+
expect(await engine.find('fx_invoice', {}, SYS)).toEqual([]);
122+
});
123+
124+
it('PROBE-R: a summary roll-up over the record-mode amount is refused when the app registers', async () => {
125+
const parent = {
126+
name: 'fx_bill',
127+
label: 'FX bill',
128+
fields: {
129+
name: { name: 'name', type: 'text' },
130+
billed_total: { name: 'billed_total', type: 'summary', summaryOperations: { object: 'fx_bill_line', field: 'amount', function: 'sum' } },
131+
},
132+
};
133+
const line = {
134+
name: 'fx_bill_line',
135+
label: 'FX bill line',
136+
fields: {
137+
currency_code: CODE,
138+
amount: { name: 'amount', type: 'currency', currencyConfig: RECORD },
139+
bill: { name: 'bill', type: 'master_detail', reference: 'fx_bill' },
140+
},
141+
};
142+
const err = await refusalOf(() => engine.registerApp(appWith([parent, line])));
143+
expect({ code: err.code, status: err.status, name: err.name }).toEqual({
144+
code: 'INVALID_METADATA', status: 422, name: 'RecordCurrencySummaryRefusedError',
145+
});
146+
expect(String(err.message)).toContain('rolls up sum(fx_bill_line.amount)');
147+
});
148+
});

0 commit comments

Comments
 (0)