Skip to content
This repository was archived by the owner on Aug 15, 2023. It is now read-only.

Commit d2e7b1b

Browse files
committed
test(tk:backend): defined search parser e2e suite
1 parent ff65a76 commit d2e7b1b

File tree

4 files changed

+1325
-25
lines changed

4 files changed

+1325
-25
lines changed

platforms/tktrex/backend/__tests__/fixtures/search/812b7e93d62ad6fdde6630c34c5ecfe7720474bb.json

Lines changed: 1138 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import {
2+
readFixtureJSON,
3+
readFixtureJSONPaths,
4+
runParserTest,
5+
} from '@shared/test/utils/parser.utils';
6+
import { TKMetadata } from '@tktrex/shared/models';
7+
import { toMetadata } from '@tktrex/shared/parser/metadata';
8+
import { parsers } from '@tktrex/shared/parser/parsers';
9+
import { HTMLSource } from '@tktrex/shared/parser/source';
10+
import base58 from 'bs58';
11+
import { parseISO, subMinutes } from 'date-fns';
12+
import path from 'path';
13+
import nacl from 'tweetnacl';
14+
import { v4 as uuid } from 'uuid';
15+
import {
16+
addDom,
17+
getLastHTMLs,
18+
getMetadata,
19+
getMetadataSchema,
20+
getSourceSchema,
21+
parserConfig,
22+
updateMetadataAndMarkHTML,
23+
} from '../lib/parser';
24+
import { GetTest, Test } from '../test/Test';
25+
26+
describe('Parser: "search"', () => {
27+
let appTest: Test;
28+
const newKeypair = nacl.sign.keyPair();
29+
const publicKey = base58.encode(newKeypair.publicKey);
30+
31+
let db: any;
32+
beforeAll(async () => {
33+
appTest = await GetTest();
34+
35+
db = {
36+
api: appTest.mongo3,
37+
read: appTest.mongo,
38+
write: appTest.mongo,
39+
};
40+
});
41+
42+
afterEach(async () => {
43+
await appTest.mongo3.deleteMany(
44+
appTest.mongo,
45+
appTest.config.get('schema').htmls,
46+
{
47+
publicKey: {
48+
$eq: publicKey,
49+
},
50+
}
51+
);
52+
});
53+
54+
describe('Nature Search', () => {
55+
jest.setTimeout(20 * 1000);
56+
57+
const history = readFixtureJSONPaths(
58+
path.resolve(__dirname, 'fixtures/search')
59+
);
60+
61+
test.each(history)(
62+
'Should correctly parse "search" contribution from path %s',
63+
async (fixturePath) => {
64+
const { sources: _sources, metadata } = readFixtureJSON(
65+
fixturePath,
66+
publicKey
67+
);
68+
const sources = _sources.map((s: any) => ({
69+
html: {
70+
...s,
71+
id: uuid(),
72+
clientTime: parseISO(s.clientTime ?? new Date().toISOString()),
73+
savingTime: subMinutes(new Date(), 2),
74+
},
75+
supporter: { version: process.env.VERSION },
76+
}));
77+
78+
await runParserTest({
79+
name: 'native-parser',
80+
log: appTest.logger,
81+
parsers: parsers,
82+
db,
83+
codecs: {
84+
contribution: HTMLSource,
85+
metadata: TKMetadata.TKMetadata,
86+
},
87+
addDom,
88+
sourceSchema: getSourceSchema(),
89+
metadataSchema: getMetadataSchema(),
90+
getEntryId: (e) => e.html.id,
91+
getEntryDate: (e) => e.html.savingTime,
92+
getEntryNatureType: (e) => e.html.type,
93+
getContributions: getLastHTMLs(db),
94+
getMetadata: getMetadata(db),
95+
saveResults: updateMetadataAndMarkHTML(db),
96+
buildMetadata: toMetadata,
97+
config: parserConfig,
98+
expectSources: (receivedSources) => {
99+
receivedSources.forEach((s) => {
100+
expect((s as any).processed).toBe(true);
101+
});
102+
},
103+
expectMetadata: (
104+
receivedMetadata: TKMetadata.SearchMetadata,
105+
expectedMetadata: TKMetadata.SearchMetadata
106+
) => {
107+
const {
108+
_id: received_Id,
109+
id: receivedId,
110+
clientTime: clientTimeR,
111+
savingTime: savingTimeR,
112+
results: resultsR,
113+
...receivedM
114+
} = receivedMetadata;
115+
116+
const {
117+
_id: _received_Id,
118+
id: _receivedId,
119+
clientTime: clientTimeExp,
120+
savingTime: savingTimeExp,
121+
results: resultsExp,
122+
...expectedM
123+
} = expectedMetadata;
124+
125+
expect(receivedM).toMatchObject(expectedM);
126+
expect(
127+
resultsR.map((r) => ({
128+
...r,
129+
publishingDate: parseISO(r.publishingDate),
130+
}))
131+
).toMatchObject(
132+
resultsExp.map((r) => ({
133+
...r,
134+
publishingDate: expect.any(Date),
135+
}))
136+
);
137+
},
138+
})({ sources, metadata });
139+
}
140+
);
141+
});
142+
});

platforms/tktrex/backend/routes/__tests__/metadata.e2e.ts

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,21 @@ describe('Metadata API', () => {
3737
const experimentId = fc.sample(fc.uuid(), 1)[0];
3838
const amount = 10;
3939

40-
const metadataWithExperimentId = fc.sample(MetadataArb, 100).map((m) => ({
41-
...m,
42-
savingTime: new Date(),
43-
experimentId,
44-
}));
45-
46-
const metadataWithResearchTag = fc.sample(MetadataArb, 100).map((m) => ({
47-
...m,
48-
savingTime: new Date(),
49-
researchTag,
50-
}));
40+
const metadataWithExperimentId = fc
41+
.sample(MetadataArb, 100)
42+
.map(({ _id, ...m }) => ({
43+
...m,
44+
savingTime: new Date(),
45+
experimentId,
46+
}));
47+
48+
const metadataWithResearchTag = fc
49+
.sample(MetadataArb, 100)
50+
.map(({ _id, ...m }) => ({
51+
...m,
52+
savingTime: new Date(),
53+
researchTag,
54+
}));
5155

5256
await test.mongo3.insertMany(
5357
test.mongo,
@@ -58,7 +62,7 @@ describe('Metadata API', () => {
5862
const expectedMetadata = metadataWithResearchTag
5963
.sort((a, b) => b.savingTime.getTime() - a.savingTime.getTime())
6064
.slice(0, amount)
61-
.map((m) => {
65+
.map(({ _id, ...m }) => {
6266
return {
6367
...m,
6468
clientTime: m.clientTime.toISOString(),
@@ -95,17 +99,21 @@ describe('Metadata API', () => {
9599
const experimentId = fc.sample(fc.uuid(), 1)[0];
96100
const amount = 10;
97101

98-
const metadataWithExperimentId = fc.sample(MetadataArb, 100).map((m) => ({
99-
...m,
100-
savingTime: new Date(),
101-
experimentId,
102-
}));
103-
104-
const metadataWithResearchTag = fc.sample(MetadataArb, 100).map((m) => ({
105-
...m,
106-
savingTime: new Date(),
107-
researchTag,
108-
}));
102+
const metadataWithExperimentId = fc
103+
.sample(MetadataArb, 100)
104+
.map(({ _id, ...m }) => ({
105+
...m,
106+
savingTime: new Date(),
107+
experimentId,
108+
}));
109+
110+
const metadataWithResearchTag = fc
111+
.sample(MetadataArb, 100)
112+
.map(({ _id, ...m }) => ({
113+
...m,
114+
savingTime: new Date(),
115+
researchTag,
116+
}));
109117

110118
await test.mongo3.insertMany(
111119
test.mongo,

platforms/tktrex/shared/src/models/Metadata.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ export type Metrics = t.TypeOf<typeof Metrics>;
4949

5050
export const MetadataBase = t.type(
5151
{
52+
_id: t.unknown,
5253
id: t.string,
54+
clientTime: t.string,
5355
savingTime: t.string,
5456
publicKey: t.string,
5557
},
@@ -107,15 +109,25 @@ export const FollowingVideoMetadata = t.intersection(
107109

108110
export type FollowingVideoMetadata = t.TypeOf<typeof FollowingVideoMetadata>;
109111

112+
const SearchMetadataResult = t.type(
113+
{
114+
order: t.number,
115+
video: t.any,
116+
textdesc: t.string,
117+
linked: t.array(t.any),
118+
thumbnail: t.string,
119+
publishingDate: t.string,
120+
},
121+
'SearchMetadataResult',
122+
);
110123
export const SearchMetadata = t.intersection(
111124
[
112125
MetadataBase,
113126
SearchN,
114127
t.type({ nature: SearchN }),
115128
t.type(
116129
{
117-
results: t.array(t.any),
118-
query: t.string,
130+
results: t.array(SearchMetadataResult),
119131
thumbnails: t.array(
120132
t.type({
121133
downloaded: t.boolean,

0 commit comments

Comments
 (0)