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

Commit

Permalink
test(tk:backend): defined search parser e2e suite
Browse files Browse the repository at this point in the history
  • Loading branch information
ascariandrea committed Oct 4, 2022
1 parent 1f91f53 commit b3223da
Show file tree
Hide file tree
Showing 3 changed files with 1,275 additions and 2 deletions.

Large diffs are not rendered by default.

125 changes: 125 additions & 0 deletions platforms/tktrex/backend/__tests__/search.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import {
readFixtureJSON,
readFixtureJSONPaths,
runParserTest,
} from '@shared/test/utils/parser.utils';
import { v4 as uuid } from 'uuid';
import { TKMetadata } from '@tktrex/shared/models';
import { parsers } from '@tktrex/shared/parser/parsers';
import base58 from 'bs58';
import { parseISO, subMinutes } from 'date-fns';
import path from 'path';
import nacl from 'tweetnacl';
import { GetTest, Test } from '../test/Test';
import {
addDom,
getLastHTMLs,
getMetadata,
getMetadataSchema,
getSourceSchema,
parserConfig,
updateMetadataAndMarkHTML,
} from '../lib/parser';
import { HTMLSource } from '@tktrex/shared/parser/source';
import { toMetadata } from '@tktrex/shared/parser/metadata';

describe('Parser: "search"', () => {
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('Nature Search', () => {
jest.setTimeout(20 * 1000);

const history = readFixtureJSONPaths(
path.resolve(__dirname, 'fixtures/search')
);

test.each(history)(
'Should correctly parse "search" contribution from path %s',
async (fixturePath) => {
const { sources: _sources, metadata } = readFixtureJSON(
fixturePath,
publicKey
);
const sources = _sources.map((s: any) => ({
html: {
...s,
id: uuid(),
clientTime: parseISO(s.clientTime ?? new Date().toISOString()),
savingTime: subMinutes(new Date(), 2),
},
supporter: { version: process.env.VERSION },
}));

await runParserTest({
name: 'native-parser',
log: appTest.logger,
parsers: parsers,
db,
codecs: {
contribution: HTMLSource,
metadata: TKMetadata.TKMetadata,
},
addDom,
sourceSchema: getSourceSchema(),
metadataSchema: getMetadataSchema(),
getEntryId: (e) => e.html.id,
getEntryDate: (e) => e.html.savingTime,
getEntryNatureType: (e) => e.html.type,
getContributions: getLastHTMLs(db),
getMetadata: getMetadata(db),
saveResults: updateMetadataAndMarkHTML(db),
buildMetadata: toMetadata,
config: parserConfig,
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,
...expectedM
} = expectedMetadata as any;

expect(receivedM).toMatchObject(expectedM);
},
})({ sources, metadata });
}
);
});
});
14 changes: 12 additions & 2 deletions platforms/tktrex/shared/src/models/Metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,25 @@ export const FollowingVideoMetadata = t.intersection(

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

const SearchMetadataResult = t.type(
{
order: t.number,
video: t.any,
textdesc: t.string,
linked: t.array(t.any),
thumbnail: t.string,
publishingDate: t.string,
},
'SearchMetadataResult',
);
export const SearchMetadata = t.intersection(
[
MetadataBase,
SearchN,
t.type({ nature: SearchN }),
t.type(
{
results: t.array(t.any),
query: t.string,
results: t.array(SearchMetadataResult),
thumbnails: t.array(
t.type({
downloaded: t.boolean,
Expand Down

0 comments on commit b3223da

Please sign in to comment.