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

Commit 673bd8d

Browse files
committed
test(tk:backend): defined profile parser e2e suite
1 parent d2e7b1b commit 673bd8d

File tree

7 files changed

+225
-25
lines changed

7 files changed

+225
-25
lines changed

platforms/tktrex/backend/__tests__/fixtures/profile/876a43574215629696bccb876a6f7660d3dc2dc9.json

Lines changed: 72 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import {
2+
readFixtureJSON,
3+
readFixtureJSONPaths,
4+
runParserTest,
5+
} from '@shared/test/utils/parser.utils';
6+
import { v4 as uuid } from 'uuid';
7+
import { TKMetadata } from '@tktrex/shared/models';
8+
import { parsers } from '@tktrex/shared/parser/parsers';
9+
import base58 from 'bs58';
10+
import { parseISO, subMinutes } from 'date-fns';
11+
import path from 'path';
12+
import nacl from 'tweetnacl';
13+
import { GetTest, Test } from '../test/Test';
14+
import {
15+
addDom,
16+
getLastHTMLs,
17+
getMetadata,
18+
getMetadataSchema,
19+
getSourceSchema,
20+
parserConfig,
21+
updateMetadataAndMarkHTML,
22+
} from '../lib/parser';
23+
import { HTMLSource } from '@tktrex/shared/parser/source';
24+
import { toMetadata } from '@tktrex/shared/parser/metadata';
25+
26+
describe('Parser: "profile"', () => {
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 Profile', () => {
55+
jest.setTimeout(20 * 1000);
56+
57+
const history = readFixtureJSONPaths(
58+
path.resolve(__dirname, 'fixtures/profile')
59+
);
60+
61+
test.each(history)(
62+
'Should correctly parse "profile" 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: (receivedMetadata, expectedMetadata) => {
104+
const {
105+
_id: received_Id,
106+
id: receivedId,
107+
savingTime: savingTimeR,
108+
...receivedM
109+
} = receivedMetadata as any;
110+
111+
const {
112+
_id: _received_Id,
113+
id: _receivedId,
114+
clientTime: clientTimeExp,
115+
savingTime: savingTimeExp,
116+
...expectedM
117+
} = expectedMetadata as any;
118+
119+
expect(receivedM).toMatchObject(expectedM);
120+
},
121+
})({ sources, metadata });
122+
}
123+
);
124+
});
125+
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe('Metadata API', () => {
6262
const expectedMetadata = metadataWithResearchTag
6363
.sort((a, b) => b.savingTime.getTime() - a.savingTime.getTime())
6464
.slice(0, amount)
65-
.map(({ _id, ...m }) => {
65+
.map((m) => {
6666
return {
6767
...m,
6868
clientTime: m.clientTime.toISOString(),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import { ContributionEventArb } from '@tktrex/shared/arbitraries/ContributionEve
1212
import { TKMetadata } from '@tktrex/shared/models/Metadata';
1313
import { parsers } from '@tktrex/shared/parser/parsers';
1414
import { HTMLSource } from '@tktrex/shared/parser/source';
15+
import { toMetadata } from '@tktrex/shared/parser/metadata';
1516
import {
1617
addDom,
17-
buildMetadata,
1818
getLastHTMLs,
1919
getMetadata,
2020
updateMetadataAndMarkHTML,
@@ -60,7 +60,7 @@ describe('/v2/personal', () => {
6060
getMetadata: getMetadata(db),
6161
getEntryDate: (e) => e.html.savingTime,
6262
getEntryNatureType: (e) => e.html.type,
63-
buildMetadata: buildMetadata,
63+
buildMetadata: toMetadata,
6464
saveResults: updateMetadataAndMarkHTML(db),
6565
});
6666
});

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as t from 'io-ts';
2-
import { FollowingN, ForYouN, SearchN, NativeVideoN } from './Nature';
2+
import { FollowingN, ForYouN, SearchN, NativeVideoN, ProfileN } from './Nature';
33

44
const Music = t.type(
55
{
@@ -54,6 +54,7 @@ export const MetadataBase = t.type(
5454
clientTime: t.string,
5555
savingTime: t.string,
5656
publicKey: t.string,
57+
timelineId: t.string,
5758
},
5859
'MetadataBase',
5960
);
@@ -165,8 +166,26 @@ export const NativeMetadata = t.intersection(
165166

166167
export type NativeMetadata = t.TypeOf<typeof NativeMetadata>;
167168

169+
export const ProfileMetadata = t.intersection(
170+
[
171+
MetadataBase,
172+
ProfileN,
173+
t.type({
174+
videos: t.array(t.any),
175+
}),
176+
],
177+
'ProfileMetadata',
178+
);
179+
export type ProfileMetadata = t.TypeOf<typeof ProfileMetadata>;
180+
168181
export const TKMetadata = t.union(
169-
[ForYouVideoMetadata, FollowingVideoMetadata, SearchMetadata, NativeMetadata],
182+
[
183+
ForYouVideoMetadata,
184+
FollowingVideoMetadata,
185+
SearchMetadata,
186+
NativeMetadata,
187+
ProfileMetadata,
188+
],
170189
'TKMetadata',
171190
);
172191

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

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,6 @@ export const FollowingN = t.type(
2525

2626
export type FollowingN = t.TypeOf<typeof FollowingN>;
2727

28-
export const CreatorN = t.type(
29-
{
30-
type: CreatorType,
31-
},
32-
'CreatorN',
33-
);
34-
export type CreatorN = t.TypeOf<typeof CreatorN>;
35-
3628
export const VideoN = t.type(
3729
{
3830
type: VideoType,
@@ -65,7 +57,7 @@ export type NativeVideoN = t.TypeOf<typeof NativeVideoN>;
6557

6658
export const ProfileN = t.type(
6759
{
68-
type: t.literal('profile'),
60+
type: t.literal('creator'),
6961
creatorName: t.string,
7062
},
7163
'ProfileN',
@@ -83,16 +75,7 @@ export const HashtagsN = t.type(
8375
export type HashtagsN = t.TypeOf<typeof HashtagsN>;
8476

8577
export const Nature = t.union(
86-
[
87-
ForYouN,
88-
FollowingN,
89-
CreatorN,
90-
VideoN,
91-
SearchN,
92-
ProfileN,
93-
HashtagsN,
94-
NativeVideoN,
95-
],
78+
[ForYouN, FollowingN, VideoN, SearchN, ProfileN, HashtagsN, NativeVideoN],
9679
'Nature',
9780
);
9881

platforms/tktrex/shared/src/parser/metadata.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ export const toMetadata: BuildMetadataFn<HTMLSource, TKMetadata, TKParsers> = (
5454
metadata.nature.query = metadata.query;
5555
break;
5656
}
57-
case 'profile': {
57+
case 'creator': {
5858
const { nature, profile, downloader } = entry.findings;
5959
metadata = {
60+
videos: [],
6061
...metadata,
6162
nature,
6263
...nature,

0 commit comments

Comments
 (0)