|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * Multi-currency M1-2 (epic #8345): a finance team loads a year of exchange |
| 5 | + * rates from one CSV through the GENERIC async import door |
| 6 | + * (`POST /data/sys_exchange_rate/import/jobs`, `sys_import_job`) — no importer |
| 7 | + * of its own. This file pins that the generic door serves the platform's real |
| 8 | + * `sys_exchange_rate` (`@objectstack/platform-objects`, not a mirror), on a |
| 9 | + * real sqlite engine, end to end: |
| 10 | + * |
| 11 | + * - a CSV of `currency, rate, effective_date` lands one row per line, with the |
| 12 | + * rate's twelve decimals intact and `source` at its declared default; |
| 13 | + * - a `source` column, when the file carries one, is written as given — which |
| 14 | + * is how an imported row says it was imported; |
| 15 | + * - a line repeating a `(currency, effective_date)` already on file is refused |
| 16 | + * for that row alone (`UNIQUE_VIOLATION`, the object's organization-scoped |
| 17 | + * unique key) while the other lines of the same file still land. |
| 18 | + */ |
| 19 | + |
| 20 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 21 | +import { ObjectQL } from '@objectstack/objectql'; |
| 22 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 23 | +import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol'; |
| 24 | +import { SysMetadataAuditObject, SysMetadataCommitObject, SysMetadataHistoryObject, SysMetadataObject } from '@objectstack/metadata-core'; |
| 25 | +import { SysImportJob } from '@objectstack/platform-objects/audit'; |
| 26 | +import { SysExchangeRate } from '@objectstack/platform-objects/system'; |
| 27 | +import { RestServer } from './rest-server'; |
| 28 | +import { DEFAULT_ORGANIZATION_ID, installDefaultOrganization } from './default-organization.testkit.js'; |
| 29 | + |
| 30 | +const liveEngines: ObjectQL[] = []; |
| 31 | +afterEach(async () => { |
| 32 | + while (liveEngines.length) { |
| 33 | + try { await liveEngines.pop()?.destroy(); } catch { /* noop */ } |
| 34 | + } |
| 35 | +}); |
| 36 | + |
| 37 | +function createMockServer() { |
| 38 | + const noop = () => {}; |
| 39 | + return { get: noop, post: noop, put: noop, delete: noop, patch: noop, use: noop, listen: async () => {}, close: async () => {} }; |
| 40 | +} |
| 41 | + |
| 42 | +function makeRes() { |
| 43 | + const res: any = { |
| 44 | + write: () => true, end: () => {}, |
| 45 | + header: () => res, |
| 46 | + status: (code: number) => { res._status = code; return res; }, |
| 47 | + json: (body: any) => { res._json = body; return res; }, |
| 48 | + }; |
| 49 | + return res; |
| 50 | +} |
| 51 | + |
| 52 | +async function boot() { |
| 53 | + const driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }); |
| 54 | + const engine = new ObjectQL(); |
| 55 | + liveEngines.push(engine); |
| 56 | + engine.registerDriver(driver, true); |
| 57 | + await engine.init(); |
| 58 | + engine.registry.registerObject(SysExchangeRate as any); |
| 59 | + engine.registry.registerObject(SysImportJob as any); |
| 60 | + await engine.syncSchemas(); |
| 61 | + await installDefaultOrganization(engine); |
| 62 | + for (const o of [SysMetadataObject, SysMetadataHistoryObject, SysMetadataAuditObject, SysMetadataCommitObject]) { |
| 63 | + if (!engine.registry.getObject(o.name)) engine.registry.registerObject(o as any); |
| 64 | + } |
| 65 | + const protocol = new ObjectStackProtocolImplementation(engine as any); |
| 66 | + const rest = new RestServer(createMockServer() as any, protocol as any, { api: { requireAuth: false } } as any); |
| 67 | + (rest as any).resolveExecCtx = async () => ({ userId: 'finance-admin', organizationId: DEFAULT_ORGANIZATION_ID }); |
| 68 | + rest.registerRoutes(); |
| 69 | + const routes = rest.getRoutes(); |
| 70 | + const find = (method: string, path: string) => { |
| 71 | + const route = routes.find((r: any) => r.method === method && r.path === path); |
| 72 | + if (!route) throw new Error(`route not registered: ${method} ${path}`); |
| 73 | + return route; |
| 74 | + }; |
| 75 | + return { |
| 76 | + engine, |
| 77 | + create: find('POST', '/api/v1/data/:object/import/jobs'), |
| 78 | + progress: find('GET', '/api/v1/data/import/jobs/:jobId'), |
| 79 | + results: find('GET', '/api/v1/data/import/jobs/:jobId/results'), |
| 80 | + }; |
| 81 | +} |
| 82 | + |
| 83 | +type Booted = Awaited<ReturnType<typeof boot>>; |
| 84 | + |
| 85 | +/** POST the CSV to the async door and wait for the job's terminal state. */ |
| 86 | +async function importCsv(ctx: Booted, csv: string): Promise<{ job: any; results: any[] }> { |
| 87 | + const res = makeRes(); |
| 88 | + await ctx.create.handler({ params: { object: 'sys_exchange_rate' }, body: { format: 'csv', csv } } as any, res); |
| 89 | + expect(res._status).toBe(201); |
| 90 | + const jobId = res._json.jobId; |
| 91 | + for (let i = 0; i < 200; i++) { |
| 92 | + const p = makeRes(); |
| 93 | + await ctx.progress.handler({ params: { jobId }, query: {} } as any, p); |
| 94 | + if (['succeeded', 'failed', 'cancelled'].includes(p._json?.status)) { |
| 95 | + const r = makeRes(); |
| 96 | + await ctx.results.handler({ params: { jobId }, query: {} } as any, r); |
| 97 | + return { job: p._json, results: r._json.results }; |
| 98 | + } |
| 99 | + await new Promise((resolve) => setTimeout(resolve, 5)); |
| 100 | + } |
| 101 | + throw new Error(`import job ${jobId} did not finish`); |
| 102 | +} |
| 103 | + |
| 104 | +const rates = async (ctx: Booted) => |
| 105 | + (await ctx.engine.find('sys_exchange_rate', {})) |
| 106 | + .map((r: any) => ({ currency: r.currency, rate: Number(r.rate), effective_date: String(r.effective_date).slice(0, 10), source: r.source })) |
| 107 | + .sort((a: any, b: any) => `${a.currency}${a.effective_date}`.localeCompare(`${b.currency}${b.effective_date}`)); |
| 108 | + |
| 109 | +describe('sys_exchange_rate — CSV through the generic async import door (multi-currency M1-2)', () => { |
| 110 | + let ctx: Booted; |
| 111 | + beforeEach(async () => { |
| 112 | + ctx = await boot(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('lands one rate per CSV line, twelve decimals intact, source at its default', async () => { |
| 116 | + const { job } = await importCsv(ctx, [ |
| 117 | + 'currency,rate,effective_date', |
| 118 | + 'EUR,0.92,2026-01-01', |
| 119 | + 'JPY,149.123456789012,2026-01-01', |
| 120 | + 'GBP,0.79,2026-02-01', |
| 121 | + ].join('\n')); |
| 122 | + |
| 123 | + expect(job).toMatchObject({ status: 'succeeded', total: 3, created: 3, errors: 0 }); |
| 124 | + expect(await rates(ctx)).toEqual([ |
| 125 | + { currency: 'EUR', rate: 0.92, effective_date: '2026-01-01', source: 'manual' }, |
| 126 | + { currency: 'GBP', rate: 0.79, effective_date: '2026-02-01', source: 'manual' }, |
| 127 | + { currency: 'JPY', rate: 149.123456789012, effective_date: '2026-01-01', source: 'manual' }, |
| 128 | + ]); |
| 129 | + }); |
| 130 | + |
| 131 | + it('writes a source column as given, so a file can mark its rows imported', async () => { |
| 132 | + const { job } = await importCsv(ctx, 'currency,rate,effective_date,source\nCHF,0.88,2026-01-01,import\n'); |
| 133 | + |
| 134 | + expect(job).toMatchObject({ status: 'succeeded', created: 1, errors: 0 }); |
| 135 | + expect(await rates(ctx)).toEqual([{ currency: 'CHF', rate: 0.88, effective_date: '2026-01-01', source: 'import' }]); |
| 136 | + }); |
| 137 | + |
| 138 | + it('refuses a line repeating a (currency, effective_date) already on file, and lands the rest of the file', async () => { |
| 139 | + await importCsv(ctx, 'currency,rate,effective_date\nEUR,0.92,2026-01-01\n'); |
| 140 | + |
| 141 | + const { job, results } = await importCsv(ctx, [ |
| 142 | + 'currency,rate,effective_date', |
| 143 | + 'EUR,0.93,2026-01-01', // same currency, same date: refused |
| 144 | + 'EUR,0.94,2026-02-01', // same currency, next month: a new rate |
| 145 | + ].join('\n')); |
| 146 | + |
| 147 | + expect(job).toMatchObject({ status: 'succeeded', total: 2, created: 1, errors: 1 }); |
| 148 | + expect(results[0]).toMatchObject({ row: 1, ok: false, code: 'UNIQUE_VIOLATION' }); |
| 149 | + expect(results[1]).toMatchObject({ row: 2, ok: true, action: 'created' }); |
| 150 | + // The stored January rate is the first file's, untouched by the refused line. |
| 151 | + expect(await rates(ctx)).toEqual([ |
| 152 | + { currency: 'EUR', rate: 0.92, effective_date: '2026-01-01', source: 'manual' }, |
| 153 | + { currency: 'EUR', rate: 0.94, effective_date: '2026-02-01', source: 'manual' }, |
| 154 | + ]); |
| 155 | + }); |
| 156 | +}); |
0 commit comments