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 pathparser.utils.ts
143 lines (127 loc) · 3.56 KB
/
parser.utils.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import * as E from 'fp-ts/lib/Either';
import * as fs from 'fs';
import * as t from 'io-ts';
import { PathReporter } from 'io-ts/lib/PathReporter';
import path from 'path';
import { Logger } from '../../logger';
import {
parseContributions,
ParserFn,
ParserProviderContext,
} from '../../providers/parser.provider';
/**
* Read fixtures file from path
*
* @param fixtureDir
* @returns string
*/
export const readFixtureJSONPaths = (fixtureDir: string): any[] => {
return fs
.readdirSync(fixtureDir, 'utf-8')
.filter((file) => file.includes('.json'))
.map((fp) => path.resolve(fixtureDir, fp));
};
/**
* Read the parsed metadata history from '../fixtures/${nature}'
*
* @param fixtureFilePath
* @param publicKey Supporter public key
*/
export const readFixtureJSON = (
fixtureFilePath: string,
publicKey: string
): { sources: any[]; metadata: any } => {
// eslint-disable-next-line
console.log(
'reading content from ',
path.relative(process.cwd(), fixtureFilePath)
);
const content = fs.readFileSync(fixtureFilePath, 'utf-8');
const mt = JSON.parse(content);
return {
...mt,
sources: mt.sources.map((h: any) => ({
...h,
publicKey,
})),
metadata: { ...mt.metadata, publicKey },
};
};
// type MetadataResult<T, S> = T & { sources: S[]; _id: string };
export const runParserTest =
<
S extends t.Mixed,
M extends t.Mixed,
PP extends Record<string, ParserFn<t.TypeOf<S>, any>>
>({
codecs,
expectMetadata,
expectSources,
sourceSchema,
metadataSchema,
...opts
}: {
log: Logger;
parsers: PP;
sourceSchema: string;
metadataSchema: string;
expectMetadata: (
received: Array<t.TypeOf<M> & { _id: string }>,
expected: Array<t.TypeOf<M> & { _id: string }>
) => void;
expectSources: (s: Array<t.TypeOf<S>>) => void;
} & ParserProviderContext<S, M, PP>) =>
async ({ sources, metadata }: any) => {
// opts.log.debug('Sources %d', sources.length);
// insert the sources in the db
await opts.db.api.insertMany(
opts.db.write,
sourceSchema,
sources.map((s: any) => s.html)
);
const result = await parseContributions<S, M, PP>({ codecs, ...opts })({
overflow: false,
errors: 0,
sources,
});
opts.log.debug('Result length %d', result.length);
// check data in db has changed
const updatedSources = await opts.db.api.aggregate(
opts.db.read,
sourceSchema,
[
{ $match: { id: { $in: sources.map((h: any) => h.id) } } },
{ $sort: { clientTime: -1 } },
]
);
expectSources(updatedSources);
// check stored metadata from db
const metadataIds = result
.map((m) => m.metadata)
.filter((m) => m !== null && m !== undefined)
.map((m: any) => m.id);
opts.log.debug('metadata ids count %d', metadataIds.length);
const metadataResults = await opts.db.api.aggregate(
opts.db.read,
metadataSchema,
[
{
$match: {
id: {
$in: metadataIds,
},
},
},
]
);
// ensure metadata returned has the same length as input ids
expect(metadataResults.length).toBe(1);
// test metadata has been saved correctly in DB
const decodeResult = t.array(codecs.metadata).decode(metadataResults);
if (decodeResult._tag === 'Left') {
// eslint-disable-next-line
throw new Error(PathReporter.report(decodeResult).join('\n'));
}
expect(E.isRight(decodeResult)).toBe(true);
expectMetadata(metadataResults[0], metadata);
};