import assert from 'node:assert/strict'; import test from 'node:test'; import type { ParseRestClient } from '../src/db/parse-rest.client.js'; import { ParseRestDatasetImportService } from '../src/modules/domestic-voc/services/parse-rest-dataset-import.service.js'; test('Parse REST dataset batches update existing natural keys and create only missing rows', async () => { const batches: Array> = []; const client = { findAll: async () => [{ objectId: 'existing-object', naturalKey: 'existing-key', createdAt: '2026-01-01T00:00:00.000Z', updatedAt: '2026-01-01T00:00:00.000Z', }], batch: async (requests: Array<{ method: string; path: string; body?: unknown }>) => { batches.push(requests); }, } as unknown as ParseRestClient; const service = new ParseRestDatasetImportService(client); const upsertMany = (service as unknown as { upsertMany(className: string, objects: Array>): Promise; }).upsertMany.bind(service); await upsertMany('VocProduct', [ { naturalKey: 'existing-key', title: 'updated' }, { naturalKey: 'new-key', title: 'created' }, ]); assert.equal(batches.length, 1); assert.deepEqual(batches[0]!.map((request) => ({ method: request.method, path: request.path })), [ { method: 'PUT', path: '/classes/VocProduct/existing-object' }, { method: 'POST', path: '/classes/VocProduct' }, ]); }); test('Parse REST dataset batches reject rows without a natural key', async () => { const client = { findAll: async () => [], batch: async () => undefined, } as unknown as ParseRestClient; const service = new ParseRestDatasetImportService(client); const upsertMany = (service as unknown as { upsertMany(className: string, objects: Array>): Promise; }).upsertMany.bind(service); await assert.rejects( () => upsertMany('VocProduct', [{ title: 'missing identity' }]), /missing naturalKey/, ); });