From 645ce77c5178d307654fc2b18e49f4d1a2974cae Mon Sep 17 00:00:00 2001 From: Marnus Weststrate Date: Wed, 1 Dec 2021 14:20:26 +0000 Subject: [PATCH] Add test: filter fails when piped to stringify() --- test-utils.ts | 8 ++++++++ test/filter.test.ts | 41 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/test-utils.ts b/test-utils.ts index ba51f36..dedf92f 100644 --- a/test-utils.ts +++ b/test-utils.ts @@ -60,3 +60,11 @@ export const pipeline = (stream: Readable): Promise => stream.on('error', reject) stream.on('finish', () => resolve(buffer)) }) + +export const streamToString = (stream: Readable): Promise => + new Promise((resolve, reject) => { + let buffer = '' + stream.on('data', (chunk: string) => buffer += chunk) + stream.on('error', reject) + stream.on('finish', () => resolve(buffer)) + }) diff --git a/test/filter.test.ts b/test/filter.test.ts index c5e9dc5..0350bc4 100644 --- a/test/filter.test.ts +++ b/test/filter.test.ts @@ -1,5 +1,5 @@ -import { createStreamFromString, pipeline } from '../test-utils' -import { filter, parse } from '../src' +import { createStreamFromString, pipeline, streamToString } from '../test-utils' +import { filter, parse, stringify } from '../src' test('filter nodes', async () => { const stream = createStreamFromString(` @@ -44,3 +44,40 @@ Welcome to the Planet.\n`) ] `) }) + +test('filter nodes and forward to the next stream', async () => { + const stream = createStreamFromString(` +1 +02:12:34,647 --> 02:12:35,489 +Hi. + +2 +02:12:36,415 --> 02:12:37,758 +𝅘𝅥𝅮 Some Music 𝅘𝅥𝅮 + +3 +02:12:38,584 --> 02:12:40,120 +Welcome to the Planet.\n`) + + const data = await streamToString( + stream + .pipe(parse()) + .pipe( + filter(node => !(node.type === 'cue' && node.data.text.includes('𝅘𝅥𝅮'))) + ) + .pipe(stringify({ format: 'WebVTT' })) + ) + + expect(data).toMatchInlineSnapshot(` + "WEBVTT + + 1 + 02:12:34.647 --> 02:12:35.489 + Hi. + + 2 + 02:12:38.584 --> 02:12:40.120 + Welcome to the Planet. + " + `) +})