diff --git a/.gitignore b/.gitignore index 9f6a659..ac2c14d 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,6 @@ node_modules/ # AI .claude/worktrees + +# Tests +coverage/ diff --git a/CLAUDE.md b/CLAUDE.md index c12c319..4c81ce5 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -14,6 +14,12 @@ Default vocabulary: `needs-triage`, `needs-info`, `ready-for-agent`, `ready-for- Single-context: `GLOSSARY.md` and `docs/adr/` at the repo root. See `docs/agents/domain.md`. +## Tests and coverage + +`pnpm test` fails when coverage of `src/` drops below the thresholds in `vitest.config.ts`. Vitest raises them when a run beats them: commit that change. The aim is 100%. + +Entry points (`scripts/*.ts`, `src/index.ts`) only parse arguments and call code in `src/`. They run as their own process, which coverage can't see, so they are left out of it; logic put there is unmeasured. + ## Database migrations A migration that creates or changes arocapi's models (`prisma/arocapi`, symlinked from the arocapi package) is named after the arocapi version: after an arocapi upgrade that changes the models, run `pnpm db:migrate --name arocapi-`. Name migrations for our own models (`prisma/models`) after what they change. diff --git a/package.json b/package.json index 22d6419..fecf239 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ }, "scripts": { "postinstall": "prisma generate", - "test": "vitest run", + "test": "vitest run --coverage", "test:watch": "vitest", "build": "tailwindcss -i src/admin/admin.css -o src/admin/public/admin.css --minify", "dev": "pnpm run --parallel \"/^dev:/\"", @@ -76,6 +76,7 @@ "@tailwindcss/cli": "^4.3.3", "@types/node": "^26.6.6", "@types/oidc-provider": "^9.12.1", + "@vitest/coverage-v8": "^5.0.5", "knip": "^6.42.1", "lefthook": "^2.2.2", "pino-pretty": "^13.2.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d7c3d62..41a8e14 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -195,10 +195,10 @@ importers: version: 7.10.1 '@prisma/client': specifier: ^7.10.1 - version: 7.10.1(prisma@7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2))(typescript@7.0.2) + version: 7.10.1(prisma@7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(magicast@0.5.5)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2))(typescript@7.0.2) arocapi: specifier: 5.0.0-beta.10 - version: 5.0.0-beta.10(@fastify/swagger@9.9.1)(@ocfl/ocfl-fs@1.0.0-beta.59)(@opensearch-project/opensearch@3.9.0)(@prisma/client@7.10.1(prisma@7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2))(typescript@7.0.2))(fastify@5.12.7)(openapi-types@12.1.3) + version: 5.0.0-beta.10(@fastify/swagger@9.9.1)(@ocfl/ocfl-fs@1.0.0-beta.59)(@opensearch-project/opensearch@3.9.0)(@prisma/client@7.10.1(prisma@7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(magicast@0.5.5)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2))(typescript@7.0.2))(fastify@5.12.7)(openapi-types@12.1.3) eta: specifier: ^4.6.0 version: 4.6.0 @@ -219,7 +219,7 @@ importers: version: 10.4.0 prisma: specifier: ^7.10.1 - version: 7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2) + version: 7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(magicast@0.5.5)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2) ro-crate: specifier: ^3.7.2 version: 3.7.2 @@ -245,6 +245,9 @@ importers: '@types/oidc-provider': specifier: ^9.12.1 version: 9.12.1 + '@vitest/coverage-v8': + specifier: ^5.0.5 + version: 5.0.5(vitest@5.0.5) knip: specifier: ^6.42.1 version: 6.42.1 @@ -262,7 +265,7 @@ importers: version: 7.0.2 vitest: specifier: ^5.0.5 - version: 5.0.5(@types/node@26.6.6)(vite@8.3.2(@types/node@26.6.6)(jiti@2.7.0)(yaml@2.9.1)) + version: 5.0.5(@types/node@26.6.6)(@vitest/coverage-v8@5.0.5)(vite@8.3.2(@types/node@26.6.6)(jiti@2.7.0)(yaml@2.9.1)) packages: @@ -270,10 +273,27 @@ packages: resolution: {integrity: sha512-Aup7aUOfpbAUg2ROOJN6Iw5f9DMBlzu0mIkm/malLQFN/YQgO48wCj0Kxa3sEHJvPVFg7siR+qRInwXd2qhQKw==} engines: {node: '>=6.9.0'} + '@babel/helper-string-parser@7.29.14': + resolution: {integrity: sha512-CXvKMFvM80ihI7Vox9jb+kQjnyjESQNPM+vB7U0Zz6TWuZA+Z/FL0QPMH300h33qJtIx+REeBhXqWOPW48TcAQ==} + engines: {node: '>=6.9.0'} + '@babel/helper-validator-identifier@7.29.7': resolution: {integrity: sha512-qehxGkRj55h/ff8EMaJ+cYhyaKlHIxqYDn682wQD7RNp9UujOQsHog2uS0r2vzr4pW+sXf90NeeayjcNaX3fFg==} engines: {node: '>=6.9.0'} + '@babel/parser@7.29.14': + resolution: {integrity: sha512-VL220vXuyO2z3rNIQwY7ftj9UwpqOt+33sH+sBWkOekbsSR2g7EEMnOx24ZlCH0xOA3DM/33SifVFGbnJD8r/w==} + engines: {node: '>=6.0.0'} + hasBin: true + + '@babel/types@7.29.14': + resolution: {integrity: sha512-UjI/dKmwJwT9pOszJitq2G6EObZPnsZUfdCr92z2TlF3EZK9J3EdfOKUrCzHl6wFthbzwhzl1Soc/UIwxRiq6w==} + engines: {node: '>=6.9.0'} + + '@bcoe/v8-coverage@1.0.2': + resolution: {integrity: sha512-6zABk/ECA/QYSCQ1NGiVwwbQerUCZ+TQbp64Q3AgmfNvurHH0j8TtXa1qbShXA6qqkpAj4V5W8pP6mLe1mcMqA==} + engines: {node: '>=18'} + '@biomejs/biome@2.5.15': resolution: {integrity: sha512-WZTW4slm/pdkh92K6t/3aEN++44JD1PQ7squ7RCMLI1flHGl43DVOIGXvCjHiZgBMO1V4uYxoFNtqrIA4qGuIQ==} engines: {node: '>=14.21.3'} @@ -1451,6 +1471,23 @@ packages: '@visx/vendor@4.0.0-alpha.0': resolution: {integrity: sha512-6I+MuqXBcv9jnlcVowHoHKSdk9gXTWkHLKyqBwRWg7LY6A3Ei8SHfubpqGV5rBUSppxMq2RszPJUS6w+H0YgmQ==} + '@vitest/coverage-v8@5.0.5': + resolution: {integrity: sha512-4KpkGdIrSqBihx/JyP/e+S+IKB98etg1XDQiLAxksLfcShnwi1VReGeOT9NI43hLHbknosKp/1CCLn9Zgcs8mg==} + peerDependencies: + '@vitest/browser': 5.0.5 + vitest: 5.0.5 + peerDependenciesMeta: + '@vitest/browser': + optional: true + + '@vitest/istanbul-lib-coverage@1.0.2': + resolution: {integrity: sha512-9J/JMwOf9AoJhAywhrn7ScKTL38hsWQP/qPG60OtaAFcQ5OXPwKsxZFlbnuCKmZ61m8/lGgHYnFpdyQZUvG/iA==} + engines: {node: '>=22'} + + '@vitest/istanbul-lib-report@1.0.2': + resolution: {integrity: sha512-gUsfXZJbzPamoIY5TvHFiMMoXESBrUMo+xqaj+rYrWI69+EnvRlYBlP96ZnHPY4vX8kyUpgAnFUCg5wZG/HkDQ==} + engines: {node: '>=22'} + '@vitest/mocker@5.0.5': resolution: {integrity: sha512-xLL3lkDegJZw+MGoGaCCsFayDZthGgpAtr3Vziyfc4p7xH89K7KRPcRBBh/kEYm0Aqkt5DtTmkYpBtRV/FrHZg==} peerDependencies: @@ -1522,6 +1559,9 @@ packages: resolution: {integrity: sha512-Izi8RQcffqCeNVgFigKli1ssklIbpHnCYc6AknXGYoB6grJqyeby7jv12JUQgmTAnIDnbck1uxksT4dzN3PWBA==} engines: {node: '>=12'} + ast-v8-to-istanbul@1.0.8: + resolution: {integrity: sha512-lPdpagk13ANXV+HClJEpCZM7kUsl5XcvgqKzZhaj6Ve1ZFICcY2sGhAN1n1vT0tML6EclT7bfVgTxqL57Gkpow==} + atomic-sleep@1.0.0: resolution: {integrity: sha512-kNOjDqAh7px0XWNI+4QbzoiR/nTkHAWNud2uvnJquD1/x5a7EQZMJT0AczqK0Qn67oY/TTQ1LbUKajZpp3I9tQ==} engines: {node: '>=8.0.0'} @@ -2101,6 +2141,9 @@ packages: resolution: {integrity: sha512-34wB/Y7MW7bzjKRjUKTa46I2Z7eV62Rkhva+KkopW7Qvv/OSWBqvkSY7vusOPrNuZcUG3tApvdVgNB8POj3SPw==} engines: {node: '>=10'} + js-tokens@10.0.0: + resolution: {integrity: sha512-lM/UBzQmfJRo9ABXbPWemivdCW8V2G8FHaHdypQaIy523snUjog0W71ayWXTjiR+ixeMyVHN2XcpnTd/liPg/Q==} + js-tokens@4.0.0: resolution: {integrity: sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==} @@ -2374,6 +2417,9 @@ packages: magic-string@1.4.3: resolution: {integrity: sha512-z12OxmaPGE0F4xlpGdjuAUckipLpQlTAuAmyGhNoD7ODpYUzvDfvtxUbjM/jwznyP178oju/KDdyi220wNJ0RQ==} + magicast@0.5.5: + resolution: {integrity: sha512-UicdXN8zQ3JHlxVq+28afMXPr1z7WNY6+7EJnzTdQWkTAlMLF5fNCCKxJHBQwGaNGR11581EiQmQzx73+MvszA==} + media-typer@1.1.1: resolution: {integrity: sha512-yz3xRaG20c6/BOzvYoDaGtPmGscs7YivItZEEqe6GbwNfHuxu9YNmvnEkMzKldAGY4/80pRcQRZSEnhquk9XuQ==} engines: {node: '>= 0.8'} @@ -2842,6 +2888,10 @@ packages: resolution: {integrity: sha512-wXR/dYpcqKmfWpEdZjiKJOwCNFndD0DMnrW/cYjVGttEkBfVgcLFHoNrlj47mjOVic9yyNu65alsgF4NQyTa2g==} engines: {node: '>=12.0.0'} + tinyrainbow@3.2.0: + resolution: {integrity: sha512-LgO3D9yZJjApUiuUfl9iFAwrtaX4+lok3wJIqttGoKCHlWUqHqbQpnxCf82L8FjgKsh4iGo78hqJwgL8F6To2A==} + engines: {node: '>=14.0.0'} + to-regex-range@5.0.1: resolution: {integrity: sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==} engines: {node: '>=8.0'} @@ -3041,8 +3091,21 @@ snapshots: js-tokens: 4.0.0 picocolors: 1.1.1 + '@babel/helper-string-parser@7.29.14': {} + '@babel/helper-validator-identifier@7.29.7': {} + '@babel/parser@7.29.14': + dependencies: + '@babel/types': 7.29.14 + + '@babel/types@7.29.14': + dependencies: + '@babel/helper-string-parser': 7.29.14 + '@babel/helper-validator-identifier': 7.29.7 + + '@bcoe/v8-coverage@1.0.2': {} + '@biomejs/biome@2.5.15': optionalDependencies: '@biomejs/cli-darwin-arm64': 2.5.15 @@ -3551,16 +3614,16 @@ snapshots: '@prisma/client-runtime-utils@7.10.1': {} - '@prisma/client@7.10.1(prisma@7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2))(typescript@7.0.2)': + '@prisma/client@7.10.1(prisma@7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(magicast@0.5.5)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2))(typescript@7.0.2)': dependencies: '@prisma/client-runtime-utils': 7.10.1 optionalDependencies: - prisma: 7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2) + prisma: 7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(magicast@0.5.5)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2) typescript: 7.0.2 - '@prisma/config@7.10.1': + '@prisma/config@7.10.1(magicast@0.5.5)': dependencies: - c12: 3.3.4 + c12: 3.3.4(magicast@0.5.5) deepmerge-ts: 8.0.2 effect: 3.20.0 empathic: 2.0.0 @@ -4100,6 +4163,24 @@ snapshots: d3-time-format: 4.1.0 internmap: 2.0.3 + '@vitest/coverage-v8@5.0.5(vitest@5.0.5)': + dependencies: + '@bcoe/v8-coverage': 1.0.2 + '@vitest/istanbul-lib-coverage': 1.0.2 + '@vitest/istanbul-lib-report': 1.0.2 + ast-v8-to-istanbul: 1.0.8 + magicast: 0.5.5 + obug: 3.0.0 + std-env: 4.3.0 + tinyrainbow: 3.2.0 + vitest: 5.0.5(@types/node@26.6.6)(@vitest/coverage-v8@5.0.5)(vite@8.3.2(@types/node@26.6.6)(jiti@2.7.0)(yaml@2.9.1)) + + '@vitest/istanbul-lib-coverage@1.0.2': {} + + '@vitest/istanbul-lib-report@1.0.2': + dependencies: + '@vitest/istanbul-lib-coverage': 1.0.2 + '@vitest/mocker@5.0.5(vite@8.3.2(@types/node@26.6.6)(jiti@2.7.0)(yaml@2.9.1))': dependencies: '@jridgewell/trace-mapping': 0.3.31 @@ -4137,10 +4218,10 @@ snapshots: argue-cli@3.2.0: {} - arocapi@5.0.0-beta.10(@fastify/swagger@9.9.1)(@ocfl/ocfl-fs@1.0.0-beta.59)(@opensearch-project/opensearch@3.9.0)(@prisma/client@7.10.1(prisma@7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2))(typescript@7.0.2))(fastify@5.12.7)(openapi-types@12.1.3): + arocapi@5.0.0-beta.10(@fastify/swagger@9.9.1)(@ocfl/ocfl-fs@1.0.0-beta.59)(@opensearch-project/opensearch@3.9.0)(@prisma/client@7.10.1(prisma@7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(magicast@0.5.5)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2))(typescript@7.0.2))(fastify@5.12.7)(openapi-types@12.1.3): dependencies: '@fastify/sensible': 6.0.7 - '@prisma/client': 7.10.1(prisma@7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2))(typescript@7.0.2) + '@prisma/client': 7.10.1(prisma@7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(magicast@0.5.5)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2))(typescript@7.0.2) content-disposition: 3.0.0 fastify: 5.12.7 fastify-type-provider-zod: 7.0.0(@fastify/swagger@9.9.1)(fastify@5.12.7)(openapi-types@12.1.3)(zod@4.6.7) @@ -4154,6 +4235,12 @@ snapshots: assertion-error@2.0.1: {} + ast-v8-to-istanbul@1.0.8: + dependencies: + '@jridgewell/trace-mapping': 0.3.31 + estree-walker: 3.0.3 + js-tokens: 10.0.0 + atomic-sleep@1.0.0: {} avvio@9.3.0: @@ -4232,7 +4319,7 @@ snapshots: dependencies: fill-range: 7.1.1 - c12@3.3.4: + c12@3.3.4(magicast@0.5.5): dependencies: chokidar: 5.0.0 confbox: 0.2.4 @@ -4246,6 +4333,8 @@ snapshots: perfect-debounce: 2.1.0 pkg-types: 2.3.3 rc9: 3.1.0 + optionalDependencies: + magicast: 0.5.5 callsites@3.1.0: {} @@ -4658,6 +4747,8 @@ snapshots: joycon@3.1.1: {} + js-tokens@10.0.0: {} + js-tokens@4.0.0: {} js-yaml@4.3.2: @@ -4893,6 +4984,12 @@ snapshots: dependencies: '@jridgewell/sourcemap-codec': 1.6.0 + magicast@0.5.5: + dependencies: + '@babel/parser': 7.29.14 + '@babel/types': 7.29.14 + source-map-js: 1.2.2 + media-typer@1.1.1: {} micromatch@4.0.8: @@ -5157,9 +5254,9 @@ snapshots: postgres@3.4.7: {} - prisma@7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2): + prisma@7.10.1(@types/node@26.6.6)(@types/react@19.3.0)(magicast@0.5.5)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(typescript@7.0.2): dependencies: - '@prisma/config': 7.10.1 + '@prisma/config': 7.10.1(magicast@0.5.5) '@prisma/dev': 0.24.17(typescript@7.0.2) '@prisma/engines': 7.10.1 '@prisma/studio-core': 0.33.0(@types/react@19.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0) @@ -5386,6 +5483,8 @@ snapshots: fdir: 6.5.0(picomatch@4.0.7) picomatch: 4.0.7 + tinyrainbow@3.2.0: {} + to-regex-range@5.0.1: dependencies: is-number: 7.0.0 @@ -5453,7 +5552,7 @@ snapshots: jiti: 2.7.0 yaml: 2.9.1 - vitest@5.0.5(@types/node@26.6.6)(vite@8.3.2(@types/node@26.6.6)(jiti@2.7.0)(yaml@2.9.1)): + vitest@5.0.5(@types/node@26.6.6)(@vitest/coverage-v8@5.0.5)(vite@8.3.2(@types/node@26.6.6)(jiti@2.7.0)(yaml@2.9.1)): dependencies: '@types/chai': 5.2.3 '@vitest/mocker': 5.0.5(vite@8.3.2(@types/node@26.6.6)(jiti@2.7.0)(yaml@2.9.1)) @@ -5471,6 +5570,7 @@ snapshots: why-is-node-running: 3.2.1 optionalDependencies: '@types/node': 26.6.6 + '@vitest/coverage-v8': 5.0.5(vitest@5.0.5) transitivePeerDependencies: - msw diff --git a/scripts/crates-import.ts b/scripts/crates-import.ts index c2aed28..8a59031 100644 --- a/scripts/crates-import.ts +++ b/scripts/crates-import.ts @@ -1,44 +1,10 @@ -import { readdir, readFile } from 'node:fs/promises'; -import { join, resolve } from 'node:path'; -import { OcflError, type StorageRoot, type VersionDetails } from '@ocfl/ocfl-fs'; -import { ROCrate } from 'ro-crate'; +import { resolve } from 'node:path'; import { config } from '../src/configuration.ts'; +import { importCrates } from '../src/crates-import.ts'; import { logger } from '../src/logger.ts'; import { openOrCreateRepository } from '../src/ocfl-repository.ts'; -const source = resolve(process.argv[2] ?? 'test-data'); - -/** Merges the crate at `path` into its OCFL object, creating the object on first import */ -async function importCrate(repository: StorageRoot, crateId: string, path: string) { - const details: VersionDetails = { - message: `Imported from ${path}`, - user: { name: 'ldacapi crates:import', address: 'https://github.com/Language-Research-Technology/ldacapi' }, - }; - try { - return await repository.create(crateId, details, (draft) => draft.import(path)); - } catch (error) { - if (!(error instanceof OcflError && error.code === 'OBJECT_EXISTS')) { - throw error; - } - } - const object = await repository.open(crateId); - return object.update(details, (draft) => draft.import(path)); -} - -const repository = await openOrCreateRepository(); - -const directories = await readdir(source, { withFileTypes: true }); -for (const directory of directories.filter((entry) => entry.isDirectory())) { - const path = join(source, directory.name); - const crate = await ROCrate.create(JSON.parse(await readFile(join(path, 'ro-crate-metadata.json'), 'utf-8'))); - try { - const object = await importCrate(repository, crate.rootId, path); - logger.info({ crateId: crate.rootId, path, version: object.head.number }, 'Imported crate'); - } catch (error) { - // A crate another writer holds, or one that needs recovery, is left for a person; the rest still import - const lockedAt = error instanceof OcflError ? error.lockedAt : undefined; - logger.error({ err: error, crateId: crate.rootId, path, lockedAt }, 'Cannot import crate'); - process.exitCode = 1; - } +if (!(await importCrates(await openOrCreateRepository(), resolve(process.argv[2] ?? 'test-data')))) { + process.exitCode = 1; } logger.info({ root: config.ocfl.root }, 'Imported crates into the OCFL repository'); diff --git a/test/crates-import.test.ts b/src/crates-import.test.ts similarity index 82% rename from test/crates-import.test.ts rename to src/crates-import.test.ts index 3098944..436e373 100644 --- a/test/crates-import.test.ts +++ b/src/crates-import.test.ts @@ -1,9 +1,10 @@ -import { execFileSync } from 'node:child_process'; import { cp, mkdtemp, rm, unlink, writeFile } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { openStorageRoot } from '@ocfl/ocfl-fs'; import { afterEach, beforeEach, describe, expect, it } from 'vitest'; +import { importCrates as importInto } from './crates-import.ts'; +import { openOrCreateRepository } from './ocfl-repository.ts'; const BUNDLED = 'arcp://name,bundled'; @@ -11,8 +12,7 @@ describe('crates:import', () => { let storage: string; let source: string; const root = () => join(storage, 'ocfl'); - const importCrates = () => - execFileSync('node', ['scripts/crates-import.ts', source], { env: { ...process.env, OCFL_ROOT: root(), LOG_LEVEL: 'silent' }, stdio: 'ignore' }); + const importCrates = async () => importInto(await openOrCreateRepository(root()), source); const head = async (id: string) => (await (await openStorageRoot(root())).open(id)).head; beforeEach(async () => { @@ -26,7 +26,7 @@ describe('crates:import', () => { }); it('records who imported each crate, and from where', async () => { - importCrates(); + await importCrates(); expect(await head(BUNDLED)).toMatchObject({ number: 1, @@ -36,20 +36,20 @@ describe('crates:import', () => { }); it('adds no version when a crate is imported again unchanged', async () => { - importCrates(); + await importCrates(); - importCrates(); + await importCrates(); expect((await head(BUNDLED)).number).toBe(1); }); it('merges a changed crate into a new version, keeping files it no longer has', async () => { - importCrates(); + await importCrates(); const before = [...(await head(BUNDLED)).files.keys()]; await writeFile(join(source, 'bundled', 'notes.txt'), 'added'); await unlink(join(source, 'bundled', 'facts.csv')); - importCrates(); + await importCrates(); const after = await head(BUNDLED); expect(after.number).toBe(2); diff --git a/src/crates-import.ts b/src/crates-import.ts new file mode 100644 index 0000000..5208373 --- /dev/null +++ b/src/crates-import.ts @@ -0,0 +1,42 @@ +import { readdir, readFile } from 'node:fs/promises'; +import { join } from 'node:path'; +import { OcflError, type StorageRoot, type VersionDetails } from '@ocfl/ocfl-fs'; +import { ROCrate } from 'ro-crate'; +import { logger } from './logger.ts'; + +/** Merges the crate at `path` into its OCFL object, creating the object on first import */ +async function importCrate(repository: StorageRoot, crateId: string, path: string) { + const details: VersionDetails = { + message: `Imported from ${path}`, + user: { name: 'ldacapi crates:import', address: 'https://github.com/Language-Research-Technology/ldacapi' }, + }; + try { + return await repository.create(crateId, details, (draft) => draft.import(path)); + } catch (error) { + if (!(error instanceof OcflError && error.code === 'OBJECT_EXISTS')) { + throw error; + } + } + const object = await repository.open(crateId); + return object.update(details, (draft) => draft.import(path)); +} + +/** Imports each crate directory under `source` into the repository; false when any crate could not be imported */ +export async function importCrates(repository: StorageRoot, source: string): Promise { + let imported = true; + const directories = await readdir(source, { withFileTypes: true }); + for (const directory of directories.filter((entry) => entry.isDirectory())) { + const path = join(source, directory.name); + const crate = await ROCrate.create(JSON.parse(await readFile(join(path, 'ro-crate-metadata.json'), 'utf-8'))); + try { + const object = await importCrate(repository, crate.rootId, path); + logger.info({ crateId: crate.rootId, path, version: object.head.number }, 'Imported crate'); + } catch (error) { + // A crate another writer holds, or one that needs recovery, is left for a person; the rest still import + const lockedAt = error instanceof OcflError ? error.lockedAt : undefined; + logger.error({ err: error, crateId: crate.rootId, path, lockedAt }, 'Cannot import crate'); + imported = false; + } + } + return imported; +} diff --git a/src/ocfl-repository.ts b/src/ocfl-repository.ts index 317ad50..801fc6d 100644 --- a/src/ocfl-repository.ts +++ b/src/ocfl-repository.ts @@ -16,13 +16,13 @@ export async function openRepository(): Promise { } /** The OCFL repository, created empty if it doesn't exist yet */ -export async function openOrCreateRepository(): Promise { +export async function openOrCreateRepository(root = config.ocfl.root): Promise { try { - return await openStorageRoot(config.ocfl.root); + return await openStorageRoot(root); } catch (error) { if (!isMissing(error)) { throw error; } - return createStorageRoot(config.ocfl.root, { layout: { extensionName: '000N-path-direct-storage-layout' } }); + return createStorageRoot(root, { layout: { extensionName: '000N-path-direct-storage-layout' } }); } } diff --git a/vitest.config.ts b/vitest.config.ts index 3d2da37..c4c2f0b 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -5,5 +5,19 @@ export default defineConfig({ globalSetup: ['test/global-setup.ts'], // Files share one database and search index fileParallelism: false, + coverage: { + provider: 'v8', + // Entry points (scripts/, src/index.ts) run as their own process, unseen by coverage; they only parse arguments and call src/ + include: ['src/**/*.ts'], + exclude: ['src/generated/**', 'src/**/*.test.ts', 'src/index.ts'], + thresholds: { + // Raised by vitest whenever a run beats them; commit the change + autoUpdate: true, + lines: 93.52, + branches: 87.44, + functions: 95.33, + statements: 93.84, + }, + }, }, });