文件
mynode/packages/utils/ipc.test.ts
chen 301194963b
一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
feat: Initial clean release of CHM
2026-11-04 11:29:39 +08:00

132 行
2.7 KiB
TypeScript

import { readFile } from 'fs/promises';
import { createTempDir } from './fs.test';
import { newHttpError, sendMessage, startServer, stopServer } from './ipc';
const getRandomPort = () => {
return Math.floor(Math.random() * (65535 - 20000 + 1)) + 20000;
};
describe('ipc', () => {
it('should send and receive messages', async () => {
const tempDir = await createTempDir();
const secretFilePath = `${tempDir}/secret.txt`;
const serverPort1 = getRandomPort();
const serverPort2 = serverPort1 + 5;
const server1 = await startServer(serverPort1, secretFilePath, async (request) => {
if (request.action === 'testing') {
return {
text: 'hello1',
};
}
throw newHttpError(404);
});
const server2 = await startServer(serverPort2, secretFilePath, async (request) => {
if (request.action === 'testing') {
return {
text: 'hello2',
};
}
if (request.action === 'ping') {
return {
text: 'pong',
};
}
throw newHttpError(404);
});
const secretKey = await readFile(secretFilePath, 'utf-8');
{
const responses = await sendMessage(serverPort1, {
action: 'testing',
data: {
test: 1234,
},
secretKey,
});
expect(responses).toEqual([
{ port: serverPort1, response: { text: 'hello1' } },
{ port: serverPort2, response: { text: 'hello2' } },
]);
}
{
const responses = await sendMessage(serverPort1, {
action: 'ping',
data: null,
secretKey,
});
expect(responses).toEqual([
{ port: serverPort2, response: { text: 'pong' } },
]);
}
{
const responses = await sendMessage(serverPort1, {
action: 'testing',
data: {
test: 1234,
},
sourcePort: serverPort1,
secretKey,
});
expect(responses).toEqual([
{ port: serverPort2, response: { text: 'hello2' } },
]);
}
await stopServer(server1);
await stopServer(server2);
});
it('should not process message if secret is invalid', async () => {
const tempDir = await createTempDir();
const secretFilePath = `${tempDir}/secret.txt`;
const serverPort = getRandomPort();
const server = await startServer(serverPort, secretFilePath, async (request) => {
if (request.action === 'testing') {
return {
text: 'hello1',
};
}
throw newHttpError(404);
});
const secretKey = await readFile(secretFilePath, 'utf-8');
{
const responses = await sendMessage(serverPort, {
action: 'testing',
data: null,
secretKey: 'wrong_key',
});
expect(responses.length).toBe(0);
}
{
const responses = await sendMessage(serverPort, {
action: 'testing',
data: null,
secretKey,
});
expect(responses.length).toBe(1);
}
await stopServer(server);
});
});