Skip to content

Commit

Permalink
Tests préchargeur passent
Browse files Browse the repository at this point in the history
  • Loading branch information
julienmalard committed Aug 23, 2023
1 parent 7c00f3f commit a676d9f
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 885 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"format": "npx prettier --write \"**/*.{js,mjs,cjs,ts,mts,cts,vue,json}\""
},
"devDependencies": {
"@constl/utils-tests": "^0.1.1",
"@rollup/plugin-node-resolve": "^15.2.1",
"@types/deep-equal": "^1.0.1",
"@types/file-saver": "^2.0.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/preload/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @module preload
*/

export {surLinux, surMac, surWindows} from './so.js';
export {surLinux, surMac, surWindows, plateforme} from './so.js';

export {
envoyerMessageÀConstellation,
Expand Down
1 change: 1 addition & 0 deletions packages/preload/src/so.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const surMac = process.platform === 'darwin';
export const surLinux = process.platform === 'linux';
export const surWindows = process.platform === 'win32';
export const plateforme = process.platform;
109 changes: 99 additions & 10 deletions packages/preload/tests/unit.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,104 @@
import {createHash} from 'crypto';
import {expect, test} from 'vitest';
import {sha256sum, versions} from '../src';
import {expect, test, vi} from 'vitest';

test('versions', async () => {
expect(versions).toBe(process.versions);
import type { IpcRendererEvent } from 'electron';
import type { messageDeServeur, messageInitServeur } from '@constl/mandataire-electron-principal';

import {v4 as uuidv4} from 'uuid';
import EventEmitter from 'events';
import { attente } from '@constl/utils-tests';

import {
envoyerMessageÀConstellation,
écouterMessagesDeConstellation,
envoyerMessageÀServeurConstellation,
écouterMessagesDeServeurConstellation,
surLinux, surMac, surWindows, plateforme,
} from '../src';
import type { mandataire } from '@constl/ipa';

test('plateforme', async () => {
expect(plateforme).toBe(process.platform);

const plateformes: Partial<Record<NodeJS.Platform, boolean>> = {
darwin: surMac,
linux: surLinux,
win32: surWindows,
};

expect(plateformes[process.platform]).toBe(true);

for (const p of Object.keys(plateformes).filter(p => p !== process.platform)) {
expect(plateformes[p]).toBe(false);
}
});

vi.mock('electron', () => {
const événements = new EventEmitter();

const ipcRenderer: Pick<Electron.IpcRenderer, 'on' | 'once' | 'send'> = {
on(channel: string, listener: (event: IpcRendererEvent, ...args: unknown[]) => void) {
if (channel === 'deClient') {
événements.on(channel, (x: [IpcRendererEvent, mandataire.messages.MessageDeTravailleur[]]) => listener(...x));
} else if (channel === 'deServeur') {
événements.on(channel, (x: [IpcRendererEvent, messageDeServeur[]]) => listener(...x));
}
return this;
},
once(channel: string, listener: (event: IpcRendererEvent, ...args: unknown[]) => void) {
if (channel === 'clientPrêt') {
// @ts-expect-error on saute l'événement
listener({});
}
événements.once(channel, listener);
return this;
},
send(channel: string, ...args: unknown[]) {
if (channel === 'pourClient'){
événements.emit('deClient', [{}, args[0]]);
} else if (channel === 'pourServeur'){
événements.emit('deServeur', [{}, args[0]]);
}
return this;
},
};


return {ipcRenderer};
});

test('nodeCrypto', async () => {
// Test hashing a random string.
const testString = Math.random().toString(36).slice(2, 7);
const expectedHash = createHash('sha256').update(testString).digest('hex');
test('messages ipa constellation', async () => {
const résultat = new attente.AttendreRésultat<mandataire.messages.MessageDeTravailleur>();

expect(sha256sum(testString)).toBe(expectedHash);
écouterMessagesDeConstellation(
message => résultat.mettreÀJour(message),
);

const message: mandataire.messages.MessagePourTravailleur = {
type: 'action',
id: uuidv4(),
fonction: ['on', 'test', 'une', 'fonction'],
args: { qui: 'nexiste', pas: 'vraiment' },
};
await envoyerMessageÀConstellation(message);

const val = await résultat.attendreExiste();
expect(val).to.deep.equal(message);
});

test('messages serveur constellation', async () => {
const résultat = new attente.AttendreRésultat<messageDeServeur>();

écouterMessagesDeServeurConstellation(
message => résultat.mettreÀJour(message),
);

const message: messageInitServeur = {
type: 'init',
port: 1234,
};
envoyerMessageÀServeurConstellation(message);

const val = await résultat.attendreExiste();
expect(val).to.deep.equal(message);
});

Loading

0 comments on commit a676d9f

Please sign in to comment.