|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- import { afterAll, describe, it } from 'vitest';
- import { createReadStream, createWriteStream } from 'fs';
- import { unlink } from 'fs/promises';
- import { createDownloader, createXmlToJsonLines, JMdict } from '../../src';
-
- const DOWNLOAD_FILENAME = 'jmdict.xml' as const;
- const PROCESS_OUTPUT_FILENAME = 'jmdict.jsonl' as const;
-
- // FIXME use tests that do not download the source data
-
- describe.skip('jmdict', () => {
- afterAll(async () => {
- try {
- await unlink(DOWNLOAD_FILENAME);
- } catch {
- // noop
- }
- });
-
- afterAll(async () => {
- try {
- await unlink(PROCESS_OUTPUT_FILENAME);
- } catch {
- // noop
- }
- });
-
- describe('downloader', () => {
- it('downloads the source data', async () => {
- const readStream = await createDownloader({
- type: JMdict.SOURCE_ID,
- });
-
- return new Promise<void>((resolve, reject) => {
- const out = createWriteStream(DOWNLOAD_FILENAME);
-
- readStream.on('error', (err) => {
- reject(err);
- });
-
- out.on('error', (err) => {
- reject(err);
- });
-
- out.on('finish', () => {
- resolve();
- });
-
- readStream.pipe(out);
- });
- }, { timeout: 30000 });
- });
-
- describe('converter', () => {
- it('converts XML to JSON', () => new Promise<void>((resolve, reject) => {
- const readStream = createReadStream(DOWNLOAD_FILENAME, { encoding: 'utf-8' });
-
- readStream.on('error', (err) => {
- reject(err);
- });
-
- const transform = createXmlToJsonLines({
- entryTagName: 'entry',
- });
-
- transform.on('error', (err) => {
- reject(err);
- });
-
- const out = createWriteStream(PROCESS_OUTPUT_FILENAME);
-
- out.on('error', (err) => {
- reject(err);
- });
-
- out.on('finish', () => {
- resolve();
- });
-
- readStream
- .pipe(transform)
- .pipe(out);
- }), { timeout: 30000 });
- });
- });
|