-
Notifications
You must be signed in to change notification settings - Fork 121
/
GuardedStream.test.ts
61 lines (51 loc) · 1.87 KB
/
GuardedStream.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import {
RepresentationMetadata,
TypedRepresentationConverter,
readableToString,
ChainedConverter,
guardedStreamFrom,
RdfToQuadConverter,
BasicRepresentation,
getLoggerFor,
INTERNAL_QUADS,
} from '../../src';
import type { Representation,
RepresentationConverterArgs,
Logger } from '../../src';
jest.mock('../../src/logging/LogUtil', (): any => {
const logger: Logger =
{ error: jest.fn(), debug: jest.fn(), warn: jest.fn(), info: jest.fn(), log: jest.fn() } as any;
return { getLoggerFor: (): Logger => logger };
});
const logger: jest.Mocked<Logger> = getLoggerFor('GuardedStream') as any;
class DummyConverter extends TypedRepresentationConverter {
public constructor() {
super('*/*', 'custom/type');
}
public async getInputTypes(): Promise<Record<string, number>> {
return { [INTERNAL_QUADS]: 1 };
}
public async getOutputTypes(): Promise<Record<string, number>> {
return { 'x/x': 1 };
}
public async handle({ representation }: RepresentationConverterArgs): Promise<Representation> {
const data = guardedStreamFrom([ 'dummy' ]);
const metadata = new RepresentationMetadata(representation.metadata, 'x/x');
return { binary: true, data, metadata };
}
}
describe('A chained converter where data gets ignored', (): void => {
const identifier = { path: 'http://test.com/' };
const rep = new BasicRepresentation('<a:b> <a:b> c!', identifier, 'text/turtle');
const converter = new ChainedConverter([
new RdfToQuadConverter(),
new DummyConverter(),
]);
it('does not throw on async crash.', async(): Promise<void> => {
jest.useFakeTimers();
const result = await converter.handleSafe({ identifier, representation: rep, preferences: { type: { 'x/x': 1 }}});
expect(await readableToString(result.data)).toBe('dummy');
jest.advanceTimersByTime(1000);
expect(logger.error).toHaveBeenCalledTimes(1);
});
});