fix: make admin tests boot without native ZIM loading

This commit is contained in:
ming 2026-08-31 05:38:58 +08:00
parent 0bd1c6f4f9
commit 26e95e3160
3 changed files with 34 additions and 8 deletions

View File

@ -1,8 +1,12 @@
import { XMLBuilder, XMLParser } from 'fast-xml-parser'
import { readFile, writeFile, rename, readdir } from 'fs/promises'
import { join } from 'path'
import { Archive } from '@openzim/libzim'
import { KIWIX_LIBRARY_XML_PATH, ZIM_STORAGE_PATH, ensureDirectoryExists, isValidZimFile } from '../utils/fs.js'
import { readFile, writeFile, rename, readdir } from 'node:fs/promises'
import { join } from 'node:path'
import {
KIWIX_LIBRARY_XML_PATH,
ZIM_STORAGE_PATH,
ensureDirectoryExists,
isValidZimFile,
} from '../utils/fs.js'
import logger from '@adonisjs/core/services/logger'
import { randomUUID } from 'node:crypto'
@ -60,6 +64,7 @@ export class KiwixLibraryService {
logger.warn(`[KiwixLibraryService] Skipping invalid/corrupted ZIM file: ${zimFilePath}`)
return null
}
const { Archive } = await import('@openzim/libzim')
const archive = new Archive(zimFilePath)
const getMeta = (key: string): string | undefined => {
@ -179,8 +184,7 @@ export class KiwixLibraryService {
faviconMimeType: b['@_faviconMimeType'],
favicon: b['@_favicon'],
date: b['@_date'],
articleCount:
b['@_articleCount'] !== undefined ? Number(b['@_articleCount']) : undefined,
articleCount: b['@_articleCount'] !== undefined ? Number(b['@_articleCount']) : undefined,
mediaCount: b['@_mediaCount'] !== undefined ? Number(b['@_mediaCount']) : undefined,
size: b['@_size'] !== undefined ? Number(b['@_size']) : undefined,
}))

View File

@ -1,4 +1,4 @@
import { Archive, Entry } from '@openzim/libzim'
import type { Archive, Entry } from '@openzim/libzim'
import * as cheerio from 'cheerio'
import { HTML_SELECTORS_TO_REMOVE, NON_CONTENT_HEADING_PATTERNS } from '../../constants/zim_extraction.js'
import { extractStructuredContent } from '../utils/zim_html.js'
@ -63,6 +63,7 @@ export class ZIMExtractionService {
throw new Error(`ZIM file is invalid or corrupted: ${filePath}`)
}
const { Archive } = await import('@openzim/libzim')
const archive = new Archive(filePath)
// Extract archive-level metadata once
@ -265,4 +266,4 @@ export class ZIMExtractionService {
return false;
}
}
}
}

View File

@ -12,6 +12,27 @@
process.env.NODE_ENV = 'test'
// Keep a clean checkout able to boot the test runner without requiring a
// developer's database/Redis credentials. A checked-in .env.test still wins
// through Adonis' normal environment loading; these values only fill gaps.
const testDefaults: Record<string, string> = {
PORT: '3333',
APP_KEY: 'test-only-app-key-not-for-production',
HOST: '127.0.0.1',
URL: 'http://127.0.0.1:3333',
LOG_LEVEL: 'error',
DB_HOST: '127.0.0.1',
DB_PORT: '3306',
DB_USER: 'root',
DB_DATABASE: 'nomad_test',
REDIS_HOST: '127.0.0.1',
REDIS_PORT: '6379',
}
for (const [key, value] of Object.entries(testDefaults)) {
process.env[key] ??= value
}
import 'reflect-metadata'
import { Ignitor, prettyPrintError } from '@adonisjs/core'
import { configure, processCLIArgs, run } from '@japa/runner'