This repository was archived by the owner on Aug 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathnative.e2e.ts
117 lines (108 loc) · 3.26 KB
/
native.e2e.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import {
readFixtureJSON,
readFixtureJSONPaths,
runParserTest,
} from '@shared/test/utils/parser.utils';
import { sanitizeHTML } from '@shared/utils/html.utils';
import { TKMetadata } from '@tktrex/shared/models';
import base58 from 'bs58';
import { parseISO, subMinutes } from 'date-fns';
import { JSDOM } from 'jsdom';
import path from 'path';
import nacl from 'tweetnacl';
import {
buildMetadata,
getLastHTMLs,
HTMLSource,
updateMetadataAndMarkHTML,
} from '../../lib/parser';
import { parsers } from '../../parsers';
import { GetTest, Test } from '../../test/Test';
describe('Parser: "native"', () => {
let appTest: Test;
const newKeypair = nacl.sign.keyPair();
const publicKey = base58.encode(newKeypair.publicKey);
let db: any;
beforeAll(async () => {
appTest = await GetTest();
db = {
api: appTest.mongo3,
read: appTest.mongo,
write: appTest.mongo,
};
});
afterEach(async () => {
await appTest.mongo3.deleteMany(
appTest.mongo,
appTest.config.get('schema').htmls,
{
publicKey: {
$eq: publicKey,
},
}
);
});
describe('Native', () => {
jest.setTimeout(20 * 1000);
const history = readFixtureJSONPaths(
path.resolve(__dirname, 'fixtures/native')
);
test.each(history)(
'Should correctly parse "native" contribution',
async (fixturePath) => {
const { sources: _sources, metadata } = readFixtureJSON(
fixturePath,
publicKey
);
const sources = _sources.map((s: any) => ({
html: {
...s,
clientTime: parseISO(s.clientTime ?? new Date().toISOString()),
savingTime: subMinutes(new Date(), 2),
},
jsdom: new JSDOM(sanitizeHTML(s.html)).window.document,
supporter: { version: process.env.VERSION },
}));
await runParserTest({
log: appTest.logger,
sourceSchema: appTest.config.get('schema').htmls,
metadataSchema: appTest.config.get('schema').metadata,
parsers: parsers,
db,
codecs: {
contribution: HTMLSource,
metadata: TKMetadata.TKMetadata,
},
getEntryId: (e) => e.html.id,
buildMetadata: buildMetadata,
getEntryDate: (e) => e.html.savingTime,
getEntryNatureType: (e) => e.html.type,
getContributions: getLastHTMLs(db),
saveResults: updateMetadataAndMarkHTML(db),
expectSources: (receivedSources) => {
receivedSources.forEach((s) => {
expect((s as any).processed).toBe(true);
});
},
expectMetadata: (receivedMetadata, expectedMetadata) => {
const {
_id: received_Id,
id: receivedId,
savingTime: savingTimeR,
...receivedM
} = receivedMetadata as any;
const {
_id: _received_Id,
id: _receivedId,
clientTime: clientTimeExp,
savingTime: savingTimeExp,
type: typeExp,
...expectedM
} = expectedMetadata as any;
expect(receivedM).toMatchObject(expectedM);
},
})({ sources, metadata });
}
);
});
});