|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { inferFormulaReturn } from '@objectstack/formula'; |
| 5 | + |
| 6 | +import { Account, ExpenseReport, Project } from '../src/data/objects/index.js'; |
| 7 | +import { ShowcaseProjectDataset } from '../src/ui/datasets/index.js'; |
| 8 | +import { CommandCenterPage } from '../src/ui/pages/index.js'; |
| 9 | +import { OpsDashboard } from '../src/ui/dashboards/index.js'; |
| 10 | + |
| 11 | +/** |
| 12 | + * The showcase writes money one way. |
| 13 | + * |
| 14 | + * Every declaration this pins is display metadata the renderer already reads, |
| 15 | + * so leaving one out raises nothing at `os validate` or at boot: the column |
| 16 | + * just reads differently from the one beside it. The four readings it guards |
| 17 | + * against: |
| 18 | + * |
| 19 | + * - the expense report roll-ups summed at their natural precision, so one |
| 20 | + * column read `225.75`, `917` and `1,500.5` — they declare `scale: 2`; |
| 21 | + * - `showcase_project.budget` had no currency and read `200,000.00` beside |
| 22 | + * Accounts' `US$12,000,000.00` — it declares the same fixed currency; |
| 23 | + * - `budget_remaining`, a formula over two amounts, read as a plain number |
| 24 | + * (`200,000`) — it declares a currency result; |
| 25 | + * - the Command Center KPI wrote the total budget as `109万` and Delivery |
| 26 | + * Operations wrote the same sum as `1,090,000` — both write it with one |
| 27 | + * pattern and one currency. |
| 28 | + */ |
| 29 | + |
| 30 | +type AnyRec = Record<string, unknown>; |
| 31 | +const isRec = (v: unknown): v is AnyRec => !!v && typeof v === 'object' && !Array.isArray(v); |
| 32 | + |
| 33 | +const fields = (o: unknown): Record<string, AnyRec> => (o as { fields: Record<string, AnyRec> }).fields; |
| 34 | + |
| 35 | +const USD_FIXED = { currencyMode: 'fixed', defaultCurrency: 'USD' }; |
| 36 | + |
| 37 | +/** The node with this `id`, at any depth. */ |
| 38 | +function nodeById(root: unknown, id: string): AnyRec | undefined { |
| 39 | + let found: AnyRec | undefined; |
| 40 | + const visit = (v: unknown): void => { |
| 41 | + if (found) return; |
| 42 | + if (Array.isArray(v)) { |
| 43 | + for (const item of v) visit(item); |
| 44 | + return; |
| 45 | + } |
| 46 | + if (!isRec(v)) return; |
| 47 | + if (v.id === id) { |
| 48 | + found = v; |
| 49 | + return; |
| 50 | + } |
| 51 | + for (const child of Object.values(v)) visit(child); |
| 52 | + }; |
| 53 | + visit(root); |
| 54 | + return found; |
| 55 | +} |
| 56 | + |
| 57 | +describe('showcase money declarations', () => { |
| 58 | + it('the expense report money roll-ups declare two decimals', () => { |
| 59 | + const f = fields(ExpenseReport); |
| 60 | + for (const name of ['total_amount', 'approved_amount', 'reimbursable_amount']) { |
| 61 | + expect(f[name]?.type, name).toBe('summary'); |
| 62 | + expect(f[name]?.scale, `${name}.scale`).toBe(2); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + it('project budget and spent carry the fixed currency Accounts uses', () => { |
| 67 | + const revenue = fields(Account).annual_revenue?.currencyConfig; |
| 68 | + expect(revenue).toEqual(USD_FIXED); |
| 69 | + const f = fields(Project); |
| 70 | + expect(f.budget?.currencyConfig).toEqual(revenue); |
| 71 | + expect(f.spent?.currencyConfig).toEqual(revenue); |
| 72 | + }); |
| 73 | + |
| 74 | + it('budget_remaining declares a currency result, rounded to cents', () => { |
| 75 | + const remaining = fields(Project).budget_remaining; |
| 76 | + expect(remaining?.type).toBe('formula'); |
| 77 | + expect(remaining?.returnType).toBe('currency'); |
| 78 | + expect(remaining?.currencyConfig).toEqual(USD_FIXED); |
| 79 | + expect(remaining?.scale).toBe(2); |
| 80 | + }); |
| 81 | + |
| 82 | + it('the currency result is the one authoring would stamp for its expression', () => { |
| 83 | + const f = fields(Project); |
| 84 | + const remaining = f.budget_remaining!; |
| 85 | + expect(inferFormulaReturn(remaining.expression as never, f as never)).toEqual({ |
| 86 | + returnType: remaining.returnType, |
| 87 | + currencyConfig: remaining.currencyConfig, |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + it('both dashboards write the total budget with one format and one currency', () => { |
| 92 | + const tile = nodeById(CommandCenterPage.regions, 'cc_k6'); |
| 93 | + const props = (tile?.properties ?? {}) as AnyRec; |
| 94 | + expect(tile?.type).toBe('object-metric'); |
| 95 | + expect(props.aggregate).toEqual({ field: 'budget', function: 'sum' }); |
| 96 | + |
| 97 | + const widget = (OpsDashboard.widgets as AnyRec[]).find((w) => w.id === 'kpi_total_budget'); |
| 98 | + expect(widget?.values).toEqual(['budget_sum']); |
| 99 | + const measure = ShowcaseProjectDataset.measures.find((m) => m.name === 'budget_sum'); |
| 100 | + expect(measure).toMatchObject({ aggregate: 'sum', field: 'budget' }); |
| 101 | + |
| 102 | + // The dataset-bound tile takes its currency from the measure, else from |
| 103 | + // the aggregated field's fixed currency; the object-metric tile from its |
| 104 | + // own `currency` only. |
| 105 | + const datasetCurrency = measure?.currency ?? (fields(Project).budget?.currencyConfig as AnyRec | undefined)?.defaultCurrency; |
| 106 | + |
| 107 | + expect(props.format).toBeDefined(); |
| 108 | + expect(props.format).toBe(measure?.format); |
| 109 | + expect(props.currency).toBeDefined(); |
| 110 | + expect(props.currency).toBe(datasetCurrency); |
| 111 | + }); |
| 112 | +}); |
0 commit comments