import { createHash } from 'crypto'; import { generateFileName } from '../../../../cms/services/generateFileName.js'; import { categoryValidator } from '../../../services/entities/category/categoryValidator.js'; import { customerValidator } from '../../../services/entities/customer/customerValidator.js'; import { customerAddressValidator } from '../../../services/entities/customerAddress/customerAddressValidator.js'; import { productValidator } from '../../../services/entities/product/productValidator.js'; import type { CategoryPlan } from '../../../services/plan/categoryPlan.js'; import { planCategories } from '../../../services/plan/categoryPlan.js'; import { planAddresses } from '../../../services/plan/customerAddressPlan.js'; import { planCustomers } from '../../../services/plan/customerPlan.js'; import { planProducts } from '../../../services/plan/productPlan.js'; import type { ImageFetcher } from '../../../services/images/types.js'; import type { StoreFixture } from '../../../services/ports/lookup.js'; import type { CsvLimits } from '../../../services/reader/readCsv.js'; import type { ImportMode } from '../../../services/types.js'; import { indexFile } from '../../../services/validate/validateFile.js'; import { writeCategories } from '../../../services/write/categoryWriter.js'; import type { CategoryWriterArgs, WriteOutcome } from '../../../services/write/categoryWriter.js'; import { writeCustomerAddresses } from '../../../services/write/customerAddressWriter.js'; import { writeCustomers } from '../../../services/write/customerWriter.js'; import { writeProducts } from '../../../services/write/productWriter.js'; import { FakeClock } from '../../../testing/FakeClock.js'; import { FakeImageFetcher } from '../../../testing/FakeImageFetcher.js'; import { MemoryLookup } from '../../../testing/MemoryLookup.js'; import { MemoryServices } from '../../../testing/MemoryServices.js'; import { RecordingServices } from '../../../testing/RecordingServices.js'; import { chunked } from './readAll.js'; export const keyDeps = { sha256Hex: (text: string) => createHash('sha256').update(text).digest('hex'), sanitizeFileName: generateFileName as (original: string) => string }; export interface ImportRun { outcomes: WriteOutcome[]; plan: CategoryPlan; lookup: MemoryLookup; memory: MemoryServices; services: RecordingServices; images: FakeImageFetcher; clock: FakeClock; } export interface ImportOptions { mode?: ImportMode; store?: StoreFixture; resumed?: boolean; include?: CategoryWriterArgs['include']; batchSize?: number; limits?: Partial; signal?: AbortSignal; /** Set up failures before the run. */ prepare?: (run: Omit) => void; /** Stop after this many outcomes (the way the runner stops at the tenth failure). */ stopAfter?: number; /** Images fetched ahead of their rows at the same time (the writers' default is 1). */ imageConcurrency?: number; /** What the writer fetches images with, made from the fake (to measure the fetches). */ wrapImages?: (inner: FakeImageFetcher) => ImageFetcher; /** The page addresses, for the category writer's wait for the address of a new parent. */ pages?: CategoryWriterArgs['pages']; } /** * Index a category file, plan it and write it on an in-memory store, the way the * runner will: no validation first, so that the writer is shown on its own. */ export async function importCategories( file: string, options: ImportOptions = {} ): Promise { const lookup = new MemoryLookup(options.store); const memory = new MemoryServices(lookup); const services = new RecordingServices(memory); const clock = new FakeClock(); const images = new FakeImageFetcher(keyDeps).hasImage( 'https://example.com/women.jpg' ); const run = { lookup, memory, services, images, clock }; options.prepare?.(run); const context = { lookup, mode: options.mode ?? 'upsert', now: () => clock.now(), sha256Hex: keyDeps.sha256Hex }; const open = () => chunked(file); const indexed = await indexFile({ validator: categoryValidator, context, open, limits: options.limits }); if (indexed.header === null) throw new Error('The test file has no header'); const plan = planCategories(indexed.state); const outcomes: WriteOutcome[] = []; for await (const outcome of writeCategories({ open, limits: options.limits, header: indexed.header, state: indexed.state, plan, context, services, images: options.wrapImages ? options.wrapImages(images) : images, imageConcurrency: options.imageConcurrency, clock, pages: options.pages, resumed: options.resumed, include: options.include, signal: options.signal, batchSize: options.batchSize })) { outcomes.push(outcome); if ( options.stopAfter !== undefined && outcomes.length >= options.stopAfter ) { break; } } return { ...run, outcomes, plan }; } /** The same for a products file: indexed, planned and written, without a check first. */ export async function importProducts( file: string, options: ImportOptions = {} ): Promise> { const lookup = new MemoryLookup(options.store); const memory = new MemoryServices(lookup); const services = new RecordingServices(memory); const clock = new FakeClock(); const images = new FakeImageFetcher(keyDeps) .hasImage('https://example.com/tee.jpg') .hasImage('https://example.com/a.jpg') .hasImage('https://example.com/b.jpg'); const run = { lookup, memory, services, images, clock }; options.prepare?.(run); const context = { lookup, mode: options.mode ?? 'upsert', now: () => clock.now(), sha256Hex: keyDeps.sha256Hex }; const open = () => chunked(file); const indexed = await indexFile({ validator: productValidator, context, open, limits: options.limits }); if (indexed.header === null) throw new Error('The test file has no header'); const plan = planProducts(indexed.state); const outcomes: WriteOutcome[] = []; for await (const outcome of writeProducts({ open, limits: options.limits, header: indexed.header, state: indexed.state, plan, context, services, images: options.wrapImages ? options.wrapImages(images) : images, imageConcurrency: options.imageConcurrency, clock, resumed: options.resumed, include: options.include, signal: options.signal, batchSize: options.batchSize })) { outcomes.push(outcome); if ( options.stopAfter !== undefined && outcomes.length >= options.stopAfter ) { break; } } return { ...run, outcomes }; } /** The same for a customers file. */ export async function importCustomers( file: string, options: ImportOptions = {} ): Promise> { const lookup = new MemoryLookup(options.store); const memory = new MemoryServices(lookup); const services = new RecordingServices(memory); const clock = new FakeClock(); const images = new FakeImageFetcher(keyDeps); const run = { lookup, memory, services, images, clock }; options.prepare?.(run); const context = { lookup, mode: options.mode ?? 'upsert', now: () => clock.now(), sha256Hex: keyDeps.sha256Hex }; const open = () => chunked(file); const indexed = await indexFile({ validator: customerValidator, context, open, limits: options.limits }); if (indexed.header === null) throw new Error('The test file has no header'); const plan = planCustomers(indexed.state); const outcomes: WriteOutcome[] = []; for await (const outcome of writeCustomers({ open, limits: options.limits, header: indexed.header, state: indexed.state, plan, context, services, clock, resumed: options.resumed, include: options.include, signal: options.signal, batchSize: options.batchSize })) { outcomes.push(outcome); if ( options.stopAfter !== undefined && outcomes.length >= options.stopAfter ) { break; } } return { ...run, outcomes }; } /** The same for a customer addresses file. */ export async function importAddresses( file: string, options: ImportOptions = {} ): Promise> { const lookup = new MemoryLookup(options.store); const memory = new MemoryServices(lookup); const services = new RecordingServices(memory); const clock = new FakeClock(); const images = new FakeImageFetcher(keyDeps); const run = { lookup, memory, services, images, clock }; options.prepare?.(run); const context = { lookup, mode: options.mode ?? 'upsert', now: () => clock.now(), sha256Hex: keyDeps.sha256Hex }; const open = () => chunked(file); const indexed = await indexFile({ validator: customerAddressValidator, context, open, limits: options.limits }); if (indexed.header === null) throw new Error('The test file has no header'); const outcomes: WriteOutcome[] = []; for await (const outcome of writeCustomerAddresses({ open, limits: options.limits, header: indexed.header, state: indexed.state, plan: planAddresses(), context, services, clock, resumed: options.resumed, include: options.include, signal: options.signal, batchSize: options.batchSize })) { outcomes.push(outcome); if ( options.stopAfter !== undefined && outcomes.length >= options.stopAfter ) { break; } } return { ...run, outcomes }; }