-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.ts
328 lines (295 loc) · 10.7 KB
/
test.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import {describe, expect, test} from '@jest/globals';
import fetch from 'node-fetch';
import { generateApolloClient } from '@deep-foundation/hasura/client';
import { DeepClient } from '@deep-foundation/deeplinks/imports/client';
import config from './config.json';
import { gql } from "@apollo/client/index.js";
let apollo = null;
let deep = null;
const searchNpmPackages = async (query) => {
const deepPackageKeyword = 'deep-package';
const textParameter = encodeURIComponent(`${query} keywords:${deepPackageKeyword}`);
const url = `https://registry.npmjs.com/-/v1/search?text=${textParameter}`;
const response = await fetch(url);
const data = await response.json();
return data;
};
const getDeepPackagesVersions = async (packagesNames) => {
const { data: data } = await deep.apolloClient.query({
query: gql`query GetPackagesVersionsByName($packageVersionTypeId: bigint, $packageNamespaceTypeId: bigint, $packageActiveTypeId: bigint, $packagesNames: [String]) {
namespaces: links(where: {type_id: {_eq: $packageNamespaceTypeId}, string: { value: {_in: $packagesNames }}}) {
id
name: value
versions: out(where: {type_id: {_eq: $packageVersionTypeId}, string: {value: {_is_null: false}}}) {
id
version: value
packageId: to_id
}
active: out(where: {type_id: {_eq: $packageVersionTypeId}, string: {value: {_is_null: false}}}) {
id
version: value
packageId: to_id
}
}
}`,
variables: {
"packageVersionTypeId": await deep.id('@deep-foundation/core', 'PackageVersion'),
"packageNamespaceTypeId": await deep.id('@deep-foundation/core', 'PackageNamespace'),
"packageActiveTypeId": await deep.id('@deep-foundation/core', 'PackageNamespace'),
"packagesNames": packagesNames
},
});
console.log(JSON.stringify(data, null, 2));
return data.namespaces.map(namespace => {
const activeVersion = namespace.active.map(version => {
return {
packageId: version?.packageId,
version: version?.version?.value
}
})[0];
return {
namespaceId: namespace.id,
name: namespace.name.value,
activeVersion: activeVersion,
versions: namespace.versions.map(version => {
return {
packageId: version?.packageId,
version: version?.version?.value,
isActive: version?.packageId === activeVersion?.packageId
}
})
}
})
};
const combinedPackagesSearch = async (query) => {
const remotePackages = await searchNpmPackages(query);
const packagesNames = remotePackages.objects.map(rp => rp.package.name);
const localPackages = await await getDeepPackagesVersions(packagesNames);
const localPackagesHash = {};
for (const localPackage of localPackages) {
localPackagesHash[localPackage.name] = localPackage;
}
const packages = remotePackages.objects.map(rp => {
return {
remotePackage: rp.package,
localPackage: localPackagesHash[rp.package.name],
}
});
return {
installedPackages: packages.filter(p => !!p.localPackage),
notInstalledPackages: packages.filter(p => !p.localPackage)
};
};
const getPackageFromNpm = async (packageName) => {
const url = `https://registry.npmjs.com/${packageName}`;
const response = await fetch(url);
const data = await response.json();
return data;
};
const ensureDeepClientCreated = async () => {
if (!deep) {
apollo = generateApolloClient(config.endpoint);
deep = new DeepClient({ apolloClient: apollo });
}
}
describe('packager tests', () => {
it('npm packages search', async () => {
const query1 = '123456789';
const data1 = await searchNpmPackages(query1) as any;
console.log(JSON.stringify(data1, null, 2));
expect(data1.objects.length).toBe(0);
const query2 = '@deep-foundation/pow';
const data2 = await searchNpmPackages(query2) as any;
console.log(JSON.stringify(data2, null, 2));
expect(data2.objects.length).toBe(1);
const query3 = '';
const data3 = await searchNpmPackages(query3) as any;
console.log(JSON.stringify(data3, null, 2));
const npmPackagerPackages = data3.objects.filter(p => p.package.name === '@deep-foundation/npm-packager')
expect(npmPackagerPackages.length).toBe(1);
});
it.skip('package versions', async () => {
await ensureDeepClientCreated();
const namespaces = await getDeepPackagesVersions(["@deep-foundation/core"]);
console.log(JSON.stringify(namespaces, null, 2));
expect(namespaces.length).toBe(1);
const firstNamespace = namespaces[0];
expect(firstNamespace.versions[0].version).toBe("0.0.0");
});
it.skip('combined packages search', async () => {
await ensureDeepClientCreated();
const packages1 = await combinedPackagesSearch("@deep-foundation/pow");
console.log(JSON.stringify(packages1, null, 2));
expect(packages1.installedPackages.length).toBe(1);
const localPackage1 = packages1.installedPackages[0].localPackage;
expect(localPackage1.versions[0].version).toBe("0.0.7");
const packages2 = await combinedPackagesSearch("");
console.log(JSON.stringify(packages2, null, 2));
expect(packages2.installedPackages.length).toBe(1);
expect(packages2.notInstalledPackages.length).toBe(3);
const localPackage2 = packages2.installedPackages[0].localPackage;
expect(localPackage2.versions[0].version).toBe("0.0.7");
});
it.skip('npm package', async () => {
await ensureDeepClientCreated();
const packageName = '@deep-foundation/pow';
const data = await getPackageFromNpm(packageName) as any;
console.log(JSON.stringify(data, null, 2));
const versions = Object.keys(data.versions);
versions.sort();
console.log(versions);
expect(versions[0]).toBe("0.0.0");
});
it.skip('deep.linkId bug', async () => {
await ensureDeepClientCreated();
expect(deep.linkId).toBe(config.userId);
});
it.skip('GPT prompt', async () => {
await ensureDeepClientCreated();
const userId = config.userId;
// Create a node-link type with name Example1.
await deep.insert({
type_id: await deep.id('@deep-foundation/core', 'Type'),
in: {
data: {
from_id: userId,
type_id: await deep.id('@deep-foundation/core', 'Contain'),
string: { data: { value: 'Example1' } },
}
},
});
// Create a node-link type with name Example2.
// It has attached value of String type.
await deep.insert({
type_id: await deep.id('@deep-foundation/core', 'Type'),
in: {
data: {
from_id: userId,
type_id: await deep.id('@deep-foundation/core', 'Contain'),
string: { data: { value: 'Example2' } },
}
},
out: {
data: {
type_id: await deep.id('@deep-foundation/core', 'Value'),
to_id: await deep.id('@deep-foundation/core', 'String'),
}
}
});
// Create a node-link type with name Human.
// It has attached properties such as age (Number type), gender (String type), and name (String type).
await deep.insert({
type_id: await deep.id('@deep-foundation/core', 'Type'),
in: {
data: [
{
from_id: userId,
type_id: await deep.id('@deep-foundation/core', 'Contain'),
string: { data: { value: 'Human' } },
},
{
from_id: await deep.id('@deep-foundation/core', 'Any'),
type_id: await deep.id('@deep-foundation/core', 'Type'),
in: {
data: {
from_id: userId,
type_id: await deep.id('@deep-foundation/core', 'Contain'),
string: { data: { value: 'Age' } },
}
},
out: {
data: {
type_id: await deep.id('@deep-foundation/core', 'Value'),
to_id: await deep.id('@deep-foundation/core', 'Number'),
}
}
},
{
from_id: await deep.id('@deep-foundation/core', 'Any'),
type_id: await deep.id('@deep-foundation/core', 'Type'),
in: {
data: {
from_id: userId,
type_id: await deep.id('@deep-foundation/core', 'Contain'),
string: { data: { value: 'Name' } },
}
},
out: {
data: {
type_id: await deep.id('@deep-foundation/core', 'Value'),
to_id: await deep.id('@deep-foundation/core', 'String'),
}
}
},
{
from_id: await deep.id('@deep-foundation/core', 'Any'),
type_id: await deep.id('@deep-foundation/core', 'Type'),
in: {
data: {
from_id: userId,
type_id: await deep.id('@deep-foundation/core', 'Contain'),
string: { data: { value: 'Gender' } },
}
},
out: {
data: {
type_id: await deep.id('@deep-foundation/core', 'Value'),
to_id: await deep.id('@deep-foundation/core', 'String'),
}
}
},
]
}
});
});
it.skip('GPT result 1', async () => {
await ensureDeepClientCreated();
const userId = config.userId;
// Create Vehicle type.
await deep.insert({
type_id: await deep.id('@deep-foundation/core', 'Type'),
in: {
data: {
from_id: userId,
type_id: await deep.id('@deep-foundation/core', 'Contain'),
string: { data: { value: 'Vehicle' } },
}
},
});
});
it.skip('GPT result 2', async () => {
await ensureDeepClientCreated();
const userId = config.userId;
// Create a node-link type with name Vehicle.
// It has attached properties such as NumberOfWheels (Number type).
await deep.insert({
type_id: await deep.id('@deep-foundation/core', 'Type'),
in: {
data: [
{
from_id: userId,
type_id: await deep.id('@deep-foundation/core', 'Contain'),
string: { data: { value: 'Vehicle' } },
},
{
from_id: await deep.id('@deep-foundation/core', 'Any'),
type_id: await deep.id('@deep-foundation/core', 'Type'),
in: {
data: {
from_id: userId,
type_id: await deep.id('@deep-foundation/core', 'Contain'),
string: { data: { value: 'NumberOfWheels' } },
}
},
out: {
data: {
type_id: await deep.id('@deep-foundation/core', 'Value'),
to_id: await deep.id('@deep-foundation/core', 'Number'),
}
}
},
]
}
});
});
});