project-nomad/admin/tests/unit/drug_install_row_reconcile.spec.ts
jakeaturner 5bc47a4aff fix(drug-reference): backfill the missing dataset install row on boot
On 1.34.0 every FDA ingest finished with its labels loaded but lost the
installed_resources 'dataset' row, because resource_type was still
enum('zim','map'). The enum migration in #1243 fixes new writes but can't recreate
the lost row. Re-selecting the tier doesn't fix it either, because ZimService
skips a dataset that already has rows. So Medicine never resolved its
Standard tier.

- Add DrugInstallRowProvider. On each boot it backfills the row when an
  ingest has finished (rows present, export date set, no download
  marker, no drug job running) and no row exists.
- Put the decision in decideDrugRowReconcile() so it has unit tests.
- Add DrugReferenceService.recordInstalledRow() as the one writer used
  by the ingest job, the provider, and ZimService.
- ZimService now writes the row when a Medicine tier re-select finds a
  finished ingest with no row.

Manual ingests and reset-and-reingest runs, which dispatch without
resourceMeta, also get the row on the next boot.
2026-09-29 15:54:41 -07:00

94 lines
2.9 KiB
TypeScript

import * as assert from 'node:assert/strict'
import { test } from 'node:test'
import {
decideDrugRowReconcile,
findDatasetCategorySlug,
type DrugRowReconcileInput,
} from '../../app/utils/drug_install_row_reconcile.js'
import type { ZimCategoriesSpec } from '../../types/collections.js'
// The state a 1.34.0 box is left in: full ingest, failed row write.
const stuck: DrugRowReconcileInput = {
rowCount: 261671,
hasInstallRow: false,
lastUpdatedExportDate: '2026-08-01',
hasDownloadMarker: false,
jobInFlight: false,
}
test('a completed ingest with no install row is backfilled at its export date', () => {
assert.deepEqual(decideDrugRowReconcile(stuck), { action: 'backfill', version: '2026-08-01' })
})
test('an existing install row is left alone', () => {
assert.equal(decideDrugRowReconcile({ ...stuck, hasInstallRow: true }).action, 'skip')
})
test('an empty drug_labels table is not backfilled', () => {
assert.equal(decideDrugRowReconcile({ ...stuck, rowCount: 0 }).action, 'skip')
})
test('a partial ingest (no export date) is not backfilled', () => {
assert.equal(decideDrugRowReconcile({ ...stuck, lastUpdatedExportDate: null }).action, 'skip')
assert.equal(decideDrugRowReconcile({ ...stuck, lastUpdatedExportDate: ' ' }).action, 'skip')
})
test('a surviving download marker means the ingest has not finished', () => {
assert.equal(decideDrugRowReconcile({ ...stuck, hasDownloadMarker: true }).action, 'skip')
})
test('an in-flight job is left to write its own row', () => {
assert.equal(decideDrugRowReconcile({ ...stuck, jobInFlight: true }).action, 'skip')
})
const spec = {
spec_version: '1',
categories: [
{
name: 'Survival',
slug: 'survival',
icon: '',
description: '',
language: 'en',
tiers: [{ name: 'Essential', slug: 'survival-essential', description: '', resources: [] }],
},
{
name: 'Medicine',
slug: 'medicine',
icon: '',
description: '',
language: 'en',
tiers: [
{ name: 'Essential', slug: 'medicine-essential', description: '', resources: [] },
{
name: 'Standard',
slug: 'medicine-standard',
description: '',
includesTier: 'medicine-essential',
resources: [
{
id: 'openfda-drug-labels',
type: 'dataset',
version: '2025-01',
title: 'FDA Drug Reference',
description: '',
url: 'https://api.fda.gov/download.json',
size_mb: 1700,
},
],
},
],
},
],
} as unknown as ZimCategoriesSpec
test('the dataset resolves to the category whose tier lists it', () => {
assert.equal(findDatasetCategorySlug(spec, 'openfda-drug-labels'), 'medicine')
})
test('an unknown dataset or missing spec yields null', () => {
assert.equal(findDatasetCategorySlug(spec, 'nope'), null)
assert.equal(findDatasetCategorySlug(null, 'openfda-drug-labels'), null)
})