@constl/utils-tests 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.aegir.js +120 -0
- package/.eslintignore +3 -0
- package/.eslintrc.cjs +30 -0
- package/LICENSE +661 -0
- package/README.md +55 -0
- package/dist/@types.d.ts +6 -0
- package/dist/@types.js +2 -0
- package/dist/@types.js.map +1 -0
- package/dist/attente.d.ts +32 -0
- package/dist/attente.js +134 -0
- package/dist/attente.js.map +1 -0
- package/dist/client.d.ts +14 -0
- package/dist/client.js +94 -0
- package/dist/client.js.map +1 -0
- package/dist/dossiers.d.ts +8 -0
- package/dist/dossiers.js +36 -0
- package/dist/dossiers.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +81 -0
- package/dist/index.js.map +1 -0
- package/dist/orbitDbTestUtils.d.ts +5 -0
- package/dist/orbitDbTestUtils.js +46 -0
- package/dist/orbitDbTestUtils.js.map +1 -0
- package/dist/sfip.d.ts +7 -0
- package/dist/sfip.js +17 -0
- package/dist/sfip.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +3 -0
- package/dist/version.js.map +1 -0
- package/package.json +97 -0
- package/scripts/node-globals.js +3 -0
- package/src/@types/shims-orbit-db.d.ts +1 -0
- package/src/@types.ts +7 -0
- package/src/attente.ts +147 -0
- package/src/client.ts +127 -0
- package/src/dossiers.ts +46 -0
- package/src/index.ts +111 -0
- package/src/orbitDbTestUtils.ts +55 -0
- package/src/sfip.ts +20 -0
- package/src/version.ts +2 -0
- package/tsconfig.build.json +4 -0
- package/tsconfig.json +38 -0
package/src/client.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import OrbitDB from "orbit-db";
|
|
2
|
+
import { initierSFIP, arrêterSFIP } from "@/sfip.js";
|
|
3
|
+
import type { IPFS } from "ipfs-core";
|
|
4
|
+
import { client, générerClient } from "@constl/ipa";
|
|
5
|
+
import { isBrowser } from "wherearewe";
|
|
6
|
+
|
|
7
|
+
import { connectPeers } from "@/orbitDbTestUtils.js";
|
|
8
|
+
import { dossierTempoTests } from "@/dossiers.js";
|
|
9
|
+
|
|
10
|
+
const { ClientConstellation } = client;
|
|
11
|
+
|
|
12
|
+
export const générerOrbites = async (
|
|
13
|
+
n = 1
|
|
14
|
+
): Promise<{ orbites: OrbitDB[]; fOublier: () => Promise<void> }> => {
|
|
15
|
+
const sfips: IPFS[] = [];
|
|
16
|
+
const orbites: OrbitDB[] = [];
|
|
17
|
+
|
|
18
|
+
const {
|
|
19
|
+
dossier: racineDossierOrbite,
|
|
20
|
+
fEffacer: fEffacerRacineDossierOrbite,
|
|
21
|
+
} = await dossierTempoTests();
|
|
22
|
+
|
|
23
|
+
fEffacerRacineDossierOrbite();
|
|
24
|
+
|
|
25
|
+
const _générer = async (i: number): Promise<void> => {
|
|
26
|
+
const racineDossier = `${racineDossierOrbite}/${i}`;
|
|
27
|
+
const sfip = await initierSFIP(`${racineDossier}/sfip`);
|
|
28
|
+
const orbite = await OrbitDB.createInstance(sfip, {
|
|
29
|
+
directory: `${racineDossier}/orbite`,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
for (const ip of sfips) {
|
|
33
|
+
await connectPeers(sfip, ip);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
sfips.push(sfip);
|
|
37
|
+
orbites.push(orbite);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
await Promise.all([...Array(n).keys()].map((i) => _générer(i)));
|
|
41
|
+
|
|
42
|
+
const fOublier = async () => {
|
|
43
|
+
await Promise.all(
|
|
44
|
+
orbites.map(async (orbite) => {
|
|
45
|
+
await orbite.stop();
|
|
46
|
+
})
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
await Promise.all(
|
|
50
|
+
sfips.map(async (d) => {
|
|
51
|
+
await arrêterSFIP(d);
|
|
52
|
+
})
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
fEffacerRacineDossierOrbite();
|
|
56
|
+
};
|
|
57
|
+
return { orbites, fOublier };
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export type typeClient = "directe" | "proc" | "travailleur";
|
|
61
|
+
|
|
62
|
+
export const générerClients = async (
|
|
63
|
+
n = 1,
|
|
64
|
+
type: typeClient = "directe"
|
|
65
|
+
): Promise<{
|
|
66
|
+
clients: client.ClientConstellation[];
|
|
67
|
+
fOublier: () => Promise<void>;
|
|
68
|
+
}> => {
|
|
69
|
+
const clients: client.ClientConstellation[] = [];
|
|
70
|
+
const fsOublier: (() => Promise<void>)[] = [];
|
|
71
|
+
// Nécessaire pour Playwright
|
|
72
|
+
if (isBrowser) window.localStorage.clear();
|
|
73
|
+
|
|
74
|
+
if (type === "directe" || type == "proc") {
|
|
75
|
+
const { orbites, fOublier: fOublierOrbites } = await générerOrbites(n);
|
|
76
|
+
fsOublier.push(fOublierOrbites);
|
|
77
|
+
|
|
78
|
+
for (const i in [...Array(n).keys()]) {
|
|
79
|
+
let client: client.ClientConstellation;
|
|
80
|
+
switch (type) {
|
|
81
|
+
case "directe": {
|
|
82
|
+
client = await ClientConstellation.créer({
|
|
83
|
+
orbite: orbites[i],
|
|
84
|
+
});
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
case "proc": {
|
|
89
|
+
client = générerClient({
|
|
90
|
+
opts: { orbite: orbites[i] },
|
|
91
|
+
mandataire: "proc"
|
|
92
|
+
});
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
default:
|
|
97
|
+
throw new Error(type);
|
|
98
|
+
}
|
|
99
|
+
clients.push(client);
|
|
100
|
+
}
|
|
101
|
+
} else if (type === "travailleur") {
|
|
102
|
+
let client: client.ClientConstellation;
|
|
103
|
+
for (const i in [...Array(n).keys()]) {
|
|
104
|
+
client = générerClient({
|
|
105
|
+
opts: { orbite: { dossier: String(i) } },
|
|
106
|
+
mandataire: "travailleur"
|
|
107
|
+
});
|
|
108
|
+
clients.push(client);
|
|
109
|
+
}
|
|
110
|
+
} else {
|
|
111
|
+
throw new Error(type);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const fOublier = async () => {
|
|
115
|
+
if (isBrowser) return; // Mystère et boule de gomme !!
|
|
116
|
+
await Promise.all(clients.map((client) => client.fermer()));
|
|
117
|
+
await Promise.all(fsOublier.map((f) => f()));
|
|
118
|
+
};
|
|
119
|
+
return { fOublier, clients };
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
export const typesClients: typeClient[] =
|
|
123
|
+
process.env.MANDATAIRE === "TOUS"
|
|
124
|
+
? ["directe", "proc"]
|
|
125
|
+
: process.env.MANDATAIRE === "PROC"
|
|
126
|
+
? ["proc"]
|
|
127
|
+
: ["directe"];
|
package/src/dossiers.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { isNode, isElectronMain } from "wherearewe";
|
|
2
|
+
import { v4 as uuidv4 } from "uuid";
|
|
3
|
+
|
|
4
|
+
export const dossierTempoTests = async (): Promise<{
|
|
5
|
+
dossier: string;
|
|
6
|
+
fEffacer: () => void;
|
|
7
|
+
}> => {
|
|
8
|
+
if (isNode || isElectronMain) {
|
|
9
|
+
const fs = await import("fs");
|
|
10
|
+
const path = await import("path");
|
|
11
|
+
const os = await import("os");
|
|
12
|
+
const sync = (await import("rimraf")).sync;
|
|
13
|
+
|
|
14
|
+
const dossier = fs.mkdtempSync(path.join(os.tmpdir(), "constl-ipa"));
|
|
15
|
+
const fEffacer = () => sync(dossier);
|
|
16
|
+
return { dossier, fEffacer };
|
|
17
|
+
} else {
|
|
18
|
+
const dossier = uuidv4() + "/constl-ipa";
|
|
19
|
+
return {
|
|
20
|
+
dossier,
|
|
21
|
+
fEffacer: () => {
|
|
22
|
+
// Rien à faire, je crois
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const obtDirTempoPourTest = async ({
|
|
29
|
+
base,
|
|
30
|
+
nom,
|
|
31
|
+
}: {
|
|
32
|
+
base: string;
|
|
33
|
+
nom?: string;
|
|
34
|
+
}): Promise<string> => {
|
|
35
|
+
if (isNode || isElectronMain) {
|
|
36
|
+
const fs = await import("fs");
|
|
37
|
+
const path = await import("path");
|
|
38
|
+
|
|
39
|
+
const dossier = path.resolve(base, (nom || "") + uuidv4());
|
|
40
|
+
fs.mkdirSync(dossier, { recursive: true });
|
|
41
|
+
return dossier;
|
|
42
|
+
} else {
|
|
43
|
+
const dossier = base + "/" + (nom || "") + uuidv4();
|
|
44
|
+
return dossier;
|
|
45
|
+
}
|
|
46
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { once } from "events";
|
|
2
|
+
|
|
3
|
+
import OrbitDB from "orbit-db";
|
|
4
|
+
import type Store from "orbit-db-store";
|
|
5
|
+
import type KeyValueStore from "orbit-db-kvstore";
|
|
6
|
+
import type FeedStore from "orbit-db-feedstore";
|
|
7
|
+
|
|
8
|
+
import type {client, réseau} from "@constl/ipa";
|
|
9
|
+
|
|
10
|
+
import { AttendreRésultat } from "@/attente.js";
|
|
11
|
+
import type ContrôleurConstellation from "@constl/ipa/dist/src/accès/cntrlConstellation.js";
|
|
12
|
+
|
|
13
|
+
export * as sfip from "@/sfip.js";
|
|
14
|
+
export * as attente from "@/attente.js";
|
|
15
|
+
export * as client from "@/client.js";
|
|
16
|
+
export * as dossiers from "@/dossiers.js";
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
const attendreInvité = (bd: Store, idInvité: string): Promise<void> =>
|
|
20
|
+
new Promise<void>((resolve) => {
|
|
21
|
+
const testAutorisé = async () => {
|
|
22
|
+
const autorisé = await (
|
|
23
|
+
bd.access as unknown as ContrôleurConstellation
|
|
24
|
+
).estAutorisé(idInvité);
|
|
25
|
+
if (autorisé) {
|
|
26
|
+
clearInterval(interval);
|
|
27
|
+
resolve();
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const interval = setInterval(testAutorisé, 100);
|
|
31
|
+
testAutorisé();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
export const clientConnectéÀ = async (
|
|
35
|
+
client1: client.ClientConstellation,
|
|
36
|
+
client2: client.ClientConstellation
|
|
37
|
+
): Promise<void> => {
|
|
38
|
+
const dispositifsConnectés = new AttendreRésultat<réseau.statutDispositif[]>();
|
|
39
|
+
const idCompte2 = await client2.obtIdCompte();
|
|
40
|
+
|
|
41
|
+
const fOublier = await client1.réseau!.suivreConnexionsDispositifs({
|
|
42
|
+
f: dispositifs => dispositifsConnectés.mettreÀJour(dispositifs),
|
|
43
|
+
});
|
|
44
|
+
await dispositifsConnectés.attendreQue(dispositifs=>{
|
|
45
|
+
return !!dispositifs.find(
|
|
46
|
+
(d) => d.infoDispositif.idCompte === idCompte2
|
|
47
|
+
);
|
|
48
|
+
})
|
|
49
|
+
await fOublier();
|
|
50
|
+
return;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export const clientsConnectés = async (
|
|
54
|
+
...clients: client.ClientConstellation[]
|
|
55
|
+
): Promise<void> => {
|
|
56
|
+
if (clients.length < 2) return;
|
|
57
|
+
const promesses: Promise<void>[] = []
|
|
58
|
+
for (let i = 1; i < clients.length; i++) {
|
|
59
|
+
for (let j = 0; j < i; j++) {
|
|
60
|
+
promesses.push(clientConnectéÀ(clients[i], clients[j]));
|
|
61
|
+
promesses.push(clientConnectéÀ(clients[j], clients[i]));
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
await Promise.all(promesses);
|
|
65
|
+
return;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export const attendreSync = async (bd: Store): Promise<void> => {
|
|
69
|
+
const accès = bd.access as unknown as ContrôleurConstellation;
|
|
70
|
+
await once(accès.bd!.events, "peer.exchanged");
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export const peutÉcrire = async (
|
|
74
|
+
bd: KeyValueStore<{ test: number }> | FeedStore<string>,
|
|
75
|
+
attendre?: OrbitDB
|
|
76
|
+
): Promise<boolean> => {
|
|
77
|
+
if (attendre) {
|
|
78
|
+
await attendreInvité(bd, attendre.identity.id);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
if (bd.type === "keyvalue") {
|
|
83
|
+
const CLEF = "test";
|
|
84
|
+
const VAL = 123;
|
|
85
|
+
|
|
86
|
+
await (bd as KeyValueStore<{ test: number }>).set(CLEF, VAL);
|
|
87
|
+
const val = bd.get(CLEF);
|
|
88
|
+
|
|
89
|
+
await (bd as KeyValueStore<{ test: number }>).del(CLEF);
|
|
90
|
+
return val === VAL;
|
|
91
|
+
} else if (bd.type === "feed") {
|
|
92
|
+
const VAL = "test";
|
|
93
|
+
|
|
94
|
+
await (bd as FeedStore<string>).add(VAL);
|
|
95
|
+
const éléments = (bd as FeedStore<string>)
|
|
96
|
+
.iterator({ limit: -1 })
|
|
97
|
+
.collect();
|
|
98
|
+
|
|
99
|
+
const autorisé =
|
|
100
|
+
éléments.length === 1 && éléments[0].payload.value === VAL;
|
|
101
|
+
if (éléments.length === 1) {
|
|
102
|
+
await (bd as FeedStore<string>).remove(éléments[0].hash);
|
|
103
|
+
}
|
|
104
|
+
return autorisé;
|
|
105
|
+
} else {
|
|
106
|
+
throw new Error(`Type de BD ${bd.type} non supporté par ce test.`);
|
|
107
|
+
}
|
|
108
|
+
} catch {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/*
|
|
2
|
+
MIT License
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2019 Haja Networks Oy
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import type { IPFS } from "ipfs-core";
|
|
26
|
+
|
|
27
|
+
const defaultFilter = () => true;
|
|
28
|
+
|
|
29
|
+
export const connectPeers = async (
|
|
30
|
+
ipfs1: IPFS,
|
|
31
|
+
ipfs2: IPFS,
|
|
32
|
+
options = {
|
|
33
|
+
filter: defaultFilter,
|
|
34
|
+
}
|
|
35
|
+
) => {
|
|
36
|
+
const id1 = await ipfs1.id();
|
|
37
|
+
const id2 = await ipfs2.id();
|
|
38
|
+
|
|
39
|
+
const addresses1 = id1.addresses.filter(options.filter);
|
|
40
|
+
const addresses2 = id2.addresses.filter(options.filter);
|
|
41
|
+
if (addresses1.length && addresses2.length) {
|
|
42
|
+
await ipfs1.swarm.connect(addresses2[0]);
|
|
43
|
+
await ipfs2.swarm.connect(addresses1[0]);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export const tousConnecter = async (sfips: IPFS[]) => {
|
|
48
|
+
const connectés: IPFS[] = [];
|
|
49
|
+
for (const sfip of sfips) {
|
|
50
|
+
for (const autre of connectés) {
|
|
51
|
+
await connectPeers(sfip, autre);
|
|
52
|
+
}
|
|
53
|
+
connectés.push(sfip);
|
|
54
|
+
}
|
|
55
|
+
};
|
package/src/sfip.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { create } from "ipfs-core";
|
|
2
|
+
import type { IPFS } from "ipfs-core";
|
|
3
|
+
|
|
4
|
+
export const config = {
|
|
5
|
+
patience: 10 * 1000,
|
|
6
|
+
patienceInit: 60 * 1000,
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const initierSFIP = async (dossier = ""): Promise<IPFS> => {
|
|
10
|
+
return create({
|
|
11
|
+
repo: dossier,
|
|
12
|
+
init: {
|
|
13
|
+
profiles: ["test"],
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const arrêterSFIP = async (sfip: IPFS) => {
|
|
19
|
+
await sfip.stop();
|
|
20
|
+
};
|
package/src/version.ts
ADDED
package/tsconfig.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
// "extends": "aegir/src/config/tsconfig.aegir.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"baseUrl": ".",
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"incremental": true,
|
|
8
|
+
"importHelpers": true,
|
|
9
|
+
"jsx": "react",
|
|
10
|
+
"lib": ["dom", "esnext"],
|
|
11
|
+
"module": "esnext",
|
|
12
|
+
"moduleResolution": "node",
|
|
13
|
+
"noFallthroughCasesInSwitch": true,
|
|
14
|
+
"noImplicitReturns": true,
|
|
15
|
+
"noUnusedLocals": false,
|
|
16
|
+
"noUnusedParameters": false,
|
|
17
|
+
"paths": {
|
|
18
|
+
"@/*": ["src/*"]
|
|
19
|
+
},
|
|
20
|
+
"outDir": "./dist",
|
|
21
|
+
"sourceMap": true,
|
|
22
|
+
"strict": true,
|
|
23
|
+
"strictPropertyInitialization": true,
|
|
24
|
+
"strictNullChecks": true,
|
|
25
|
+
"target": "esnext",
|
|
26
|
+
"useUnknownInCatchVariables": false,
|
|
27
|
+
|
|
28
|
+
"skipLibCheck": true,
|
|
29
|
+
"experimentalDecorators": true,
|
|
30
|
+
"importsNotUsedAsValues": "remove",
|
|
31
|
+
|
|
32
|
+
"plugins": [
|
|
33
|
+
{ "transform": "typescript-transform-paths" },
|
|
34
|
+
{ "transform": "typescript-transform-paths", "afterDeclarations": true }
|
|
35
|
+
]
|
|
36
|
+
},
|
|
37
|
+
"include": ["src", "@types", "test"]
|
|
38
|
+
}
|