Tools for learning Japanese.
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

jmnedict.test.ts 1.9 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { afterAll, describe, it } from 'vitest';
  2. import { createReadStream, createWriteStream } from 'fs';
  3. import { unlink } from 'fs/promises';
  4. import { createDownloader, createXmlToJsonLines, JMnedict } from '../../src';
  5. const DOWNLOAD_FILENAME = 'jmnedict.xml' as const;
  6. const PROCESS_OUTPUT_FILENAME = 'jmnedict.jsonl' as const;
  7. // FIXME use tests that do not download the source data
  8. describe.skip('jmnedict', () => {
  9. afterAll(async () => {
  10. try {
  11. await unlink(DOWNLOAD_FILENAME);
  12. } catch {
  13. // noop
  14. }
  15. });
  16. afterAll(async () => {
  17. try {
  18. await unlink(PROCESS_OUTPUT_FILENAME);
  19. } catch {
  20. // noop
  21. }
  22. });
  23. describe('downloader', () => {
  24. it('downloads the source data', async () => {
  25. const readStream = await createDownloader({
  26. type: JMnedict.SOURCE_ID,
  27. });
  28. return new Promise<void>((resolve, reject) => {
  29. const out = createWriteStream(DOWNLOAD_FILENAME);
  30. readStream.on('error', (err) => {
  31. reject(err);
  32. });
  33. out.on('error', (err) => {
  34. reject(err);
  35. });
  36. out.on('finish', () => {
  37. resolve();
  38. });
  39. readStream.pipe(out);
  40. });
  41. }, { timeout: 30000 });
  42. });
  43. describe('converter', () => {
  44. it('converts XML to JSON', () => new Promise<void>((resolve, reject) => {
  45. const readStream = createReadStream(DOWNLOAD_FILENAME, { encoding: 'utf-8' });
  46. readStream.on('error', (err) => {
  47. reject(err);
  48. });
  49. const transform = createXmlToJsonLines({
  50. entryTagName: 'entry',
  51. });
  52. transform.on('error', (err) => {
  53. reject(err);
  54. });
  55. const out = createWriteStream(PROCESS_OUTPUT_FILENAME);
  56. out.on('error', (err) => {
  57. reject(err);
  58. });
  59. out.on('finish', () => {
  60. resolve();
  61. });
  62. readStream
  63. .pipe(transform)
  64. .pipe(out);
  65. }), { timeout: 45000 });
  66. });
  67. });