node-bible-sync 0.0.0-stage → 0.3.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/.github/workflows/build.yml +39 -0
- package/API.md +81 -0
- package/COPYING +339 -0
- package/README.md +113 -2
- package/biblesync.js +234 -0
- package/binding.gyp +62 -0
- package/examples/biblesync.js +53 -0
- package/index.js +31 -0
- package/package.json +26 -4
- package/scripts/fetch_biblesync.js +80 -0
- package/scripts/ios_framework_postbuild.sh +85 -0
- package/scripts/prepare_biblesync.js +189 -0
- package/src/biblesync_compat/biblesync-version.hh +29 -0
- package/src/biblesync_compat/libintl.h +24 -0
- package/src/biblesync_compat/unistd.h +29 -0
- package/src/biblesync_compat/uuid/uuid.h +61 -0
- package/src/napi_module/biblesync_session.cpp +433 -0
- package/src/napi_module/biblesync_session.hpp +90 -0
- package/src/napi_module/binding.cpp +26 -0
- package/test/biblesync.test.js +142 -0
- package/test/biblesync_example.test.js +59 -0
- package/test/biblesync_fetch.test.js +86 -0
- package/test/biblesync_integration.test.js +267 -0
- package/test/biblesync_native.test.js +162 -0
- package/test/biblesync_prepare.test.js +81 -0
- package/test/fixtures/biblesync_fail_send.c +61 -0
- package/test/fixtures/biblesync_peer.js +53 -0
- package/test/fixtures/biblesync_system_peer.cpp +62 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/* This file is part of node-bible-sync.
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2026 Tobias Klein <contact@tklein.info>
|
|
4
|
+
|
|
5
|
+
node-bible-sync is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 2 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
node-bible-sync is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
13
|
+
See the GNU General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU General Public License
|
|
16
|
+
along with node-bible-sync. See the file COPYING.
|
|
17
|
+
If not, see <http://www.gnu.org/licenses/>. */
|
|
18
|
+
|
|
19
|
+
jest.mock('..');
|
|
20
|
+
|
|
21
|
+
const { EventEmitter } = require('events');
|
|
22
|
+
const BibleSync = require('..');
|
|
23
|
+
|
|
24
|
+
test('follows discovered speakers after startup and reports failures', async () => {
|
|
25
|
+
jest.useFakeTimers();
|
|
26
|
+
const log = jest.spyOn(console, 'log').mockImplementation(() => {});
|
|
27
|
+
const error = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
28
|
+
let finishStart;
|
|
29
|
+
const sync = new EventEmitter();
|
|
30
|
+
sync.start = jest.fn(() => new Promise(resolve => { finishStart = resolve; }));
|
|
31
|
+
sync.stop = jest.fn().mockResolvedValue();
|
|
32
|
+
sync.setSpeakerListening = jest.fn().mockResolvedValueOnce()
|
|
33
|
+
.mockRejectedValueOnce(new Error('speaker expired'));
|
|
34
|
+
BibleSync.mockImplementation(() => sync);
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
jest.isolateModules(() => require('../examples/biblesync'));
|
|
38
|
+
sync.emit('speaker', { speakerId: 'first', user: 'Alice' });
|
|
39
|
+
expect(sync.setSpeakerListening).not.toHaveBeenCalled();
|
|
40
|
+
|
|
41
|
+
finishStart();
|
|
42
|
+
await Promise.resolve();
|
|
43
|
+
await Promise.resolve();
|
|
44
|
+
expect(sync.setSpeakerListening).toHaveBeenCalledWith('first', true);
|
|
45
|
+
|
|
46
|
+
sync.emit('speaker', { speakerId: 'second', user: 'Bob' });
|
|
47
|
+
await Promise.resolve();
|
|
48
|
+
await Promise.resolve();
|
|
49
|
+
expect(sync.setSpeakerListening).toHaveBeenCalledWith('second', true);
|
|
50
|
+
expect(error).toHaveBeenCalledWith('Could not follow Bob (second):', 'speaker expired');
|
|
51
|
+
|
|
52
|
+
await jest.advanceTimersByTimeAsync(30000);
|
|
53
|
+
expect(sync.stop).toHaveBeenCalledTimes(1);
|
|
54
|
+
} finally {
|
|
55
|
+
log.mockRestore();
|
|
56
|
+
error.mockRestore();
|
|
57
|
+
jest.useRealTimers();
|
|
58
|
+
}
|
|
59
|
+
});
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/* This file is part of node-bible-sync.
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2026 Tobias Klein <contact@tklein.info>
|
|
4
|
+
|
|
5
|
+
node-bible-sync is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 2 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
node-bible-sync is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
13
|
+
See the GNU General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU General Public License
|
|
16
|
+
along with node-bible-sync. See the file COPYING.
|
|
17
|
+
If not, see <http://www.gnu.org/licenses/>. */
|
|
18
|
+
|
|
19
|
+
jest.mock('fs');
|
|
20
|
+
jest.mock('child_process');
|
|
21
|
+
|
|
22
|
+
const fs = require('fs');
|
|
23
|
+
const { spawnSync } = require('child_process');
|
|
24
|
+
const { fetchBibleSync } = require('../scripts/fetch_biblesync');
|
|
25
|
+
|
|
26
|
+
const revision = 'f388207c3ddc8c9cd28265dc3255ec0b79d6fbd9';
|
|
27
|
+
|
|
28
|
+
function success(stdout = '') {
|
|
29
|
+
return { status: 0, stdout, stderr: '' };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
beforeEach(() => {
|
|
33
|
+
jest.resetAllMocks();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('reuses an unmodified checkout at the pinned revision', () => {
|
|
37
|
+
fs.existsSync.mockReturnValue(true);
|
|
38
|
+
spawnSync.mockReturnValueOnce(success(revision)).mockReturnValueOnce(success());
|
|
39
|
+
|
|
40
|
+
fetchBibleSync();
|
|
41
|
+
|
|
42
|
+
expect(spawnSync).toHaveBeenCalledTimes(2);
|
|
43
|
+
expect(fs.renameSync).not.toHaveBeenCalled();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test('rejects a checkout at another revision without modifying it', () => {
|
|
47
|
+
fs.existsSync.mockReturnValue(true);
|
|
48
|
+
spawnSync.mockReturnValueOnce(success('different-revision'));
|
|
49
|
+
|
|
50
|
+
expect(fetchBibleSync).toThrow('expected ' + revision);
|
|
51
|
+
expect(fs.rmSync).not.toHaveBeenCalled();
|
|
52
|
+
expect(fs.renameSync).not.toHaveBeenCalled();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test('rejects a checkout with local changes', () => {
|
|
56
|
+
fs.existsSync.mockReturnValue(true);
|
|
57
|
+
spawnSync.mockReturnValueOnce(success(revision)).mockReturnValueOnce(success('?? custom-file'));
|
|
58
|
+
|
|
59
|
+
expect(fetchBibleSync).toThrow('has local changes');
|
|
60
|
+
expect(fs.rmSync).not.toHaveBeenCalled();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test('installs a verified checkout and removes only its temporary directory', () => {
|
|
64
|
+
fs.existsSync.mockReturnValue(false);
|
|
65
|
+
fs.mkdtempSync.mockReturnValue('/tmp/example-fetch');
|
|
66
|
+
spawnSync.mockReturnValueOnce(success()).mockReturnValueOnce(success())
|
|
67
|
+
.mockReturnValueOnce(success(revision)).mockReturnValueOnce(success());
|
|
68
|
+
|
|
69
|
+
fetchBibleSync();
|
|
70
|
+
|
|
71
|
+
expect(fs.renameSync).toHaveBeenCalledWith(
|
|
72
|
+
expect.stringMatching(/example-fetch[/\\]source$/),
|
|
73
|
+
expect.stringMatching(/[/\\]biblesync$/)
|
|
74
|
+
);
|
|
75
|
+
expect(fs.rmSync).toHaveBeenCalledWith('/tmp/example-fetch', { recursive: true, force: true });
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test('reports clone failures and removes only its temporary directory', () => {
|
|
79
|
+
fs.existsSync.mockReturnValue(false);
|
|
80
|
+
fs.mkdtempSync.mockReturnValue('/tmp/example-fetch');
|
|
81
|
+
spawnSync.mockReturnValueOnce({ status: 1, stderr: 'network unavailable', stdout: '' });
|
|
82
|
+
|
|
83
|
+
expect(fetchBibleSync).toThrow('network unavailable');
|
|
84
|
+
expect(fs.renameSync).not.toHaveBeenCalled();
|
|
85
|
+
expect(fs.rmSync).toHaveBeenCalledWith('/tmp/example-fetch', { recursive: true, force: true });
|
|
86
|
+
});
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
/* This file is part of node-bible-sync.
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2026 Tobias Klein <contact@tklein.info>
|
|
4
|
+
|
|
5
|
+
node-bible-sync is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 2 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
node-bible-sync is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
13
|
+
See the GNU General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU General Public License
|
|
16
|
+
along with node-bible-sync. See the file COPYING.
|
|
17
|
+
If not, see <http://www.gnu.org/licenses/>. */
|
|
18
|
+
|
|
19
|
+
const path = require('path');
|
|
20
|
+
const { fork, spawn } = require('child_process');
|
|
21
|
+
const { randomBytes } = require('crypto');
|
|
22
|
+
const readline = require('readline');
|
|
23
|
+
|
|
24
|
+
const integration = process.env.BIBLESYNC_INTEGRATION === '1' ? test : test.skip;
|
|
25
|
+
jest.setTimeout(50000);
|
|
26
|
+
|
|
27
|
+
function peer(mode, name, passphrase, immediate = false, environment = {}) {
|
|
28
|
+
const child = fork(path.join(__dirname, 'fixtures', 'biblesync_peer.js'), [mode, name, passphrase, immediate ? 'immediate' : ''], {
|
|
29
|
+
stdio: ['ignore', 'pipe', 'pipe', 'ipc'],
|
|
30
|
+
env: { ...process.env, ...environment }
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const events = [];
|
|
34
|
+
const waiters = [];
|
|
35
|
+
let nextId = 0;
|
|
36
|
+
child.on('message', message => {
|
|
37
|
+
const index = waiters.findIndex(waiter => waiter.predicate(message));
|
|
38
|
+
if (index >= 0) {
|
|
39
|
+
const waiter = waiters.splice(index, 1)[0];
|
|
40
|
+
clearTimeout(waiter.timer);
|
|
41
|
+
waiter.resolve(message);
|
|
42
|
+
} else {
|
|
43
|
+
events.push(message);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
function waitFor(predicate, timeout = 7000) {
|
|
47
|
+
const index = events.findIndex(predicate);
|
|
48
|
+
if (index >= 0) return Promise.resolve(events.splice(index, 1)[0]);
|
|
49
|
+
return new Promise((resolve, reject) => {
|
|
50
|
+
const waiter = { predicate, resolve };
|
|
51
|
+
waiter.timer = setTimeout(() => {
|
|
52
|
+
const index = waiters.indexOf(waiter);
|
|
53
|
+
if (index >= 0) waiters.splice(index, 1);
|
|
54
|
+
reject(new Error(`Timed out waiting for ${name}; received ${JSON.stringify(events)}`));
|
|
55
|
+
}, timeout);
|
|
56
|
+
waiters.push(waiter);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
async function command(type, data = {}) {
|
|
60
|
+
const id = String(++nextId);
|
|
61
|
+
child.send({ type, id, ...data });
|
|
62
|
+
const result = await waitFor(message => message.type === 'result' && message.id === id);
|
|
63
|
+
if (result.error) throw new Error(result.error);
|
|
64
|
+
}
|
|
65
|
+
return { child, events, waitFor, command };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const faultIntegration = process.env.BIBLESYNC_FAIL_SEND_LIBRARY ? integration : test.skip;
|
|
69
|
+
faultIntegration('startup socket failure rejects without sending an initial beacon', async () => {
|
|
70
|
+
const speaker = peer('speaker', 'fault-test', 'BibleSync', false, {
|
|
71
|
+
LD_PRELOAD: process.env.BIBLESYNC_FAIL_SEND_LIBRARY,
|
|
72
|
+
BIBLESYNC_FAIL_BIND: '1'
|
|
73
|
+
});
|
|
74
|
+
let stderr = '';
|
|
75
|
+
speaker.child.stderr.on('data', data => { stderr += data; });
|
|
76
|
+
const exited = new Promise(resolve => speaker.child.once('exit', resolve));
|
|
77
|
+
try {
|
|
78
|
+
const error = await speaker.waitFor(message => message.type === 'startupError');
|
|
79
|
+
expect(error.message).toContain('bind');
|
|
80
|
+
await exited;
|
|
81
|
+
expect(stderr).not.toContain('unexpected sendto');
|
|
82
|
+
} finally {
|
|
83
|
+
if (speaker.child.connected) speaker.child.disconnect();
|
|
84
|
+
if (speaker.child.exitCode === null) speaker.child.kill();
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
faultIntegration('periodic beacon send failure stops the session and allows restart', async () => {
|
|
89
|
+
const passphrase = `nsi-test-${randomBytes(12).toString('hex')}`;
|
|
90
|
+
const speaker = peer('speaker', 'fault-test', passphrase, false, {
|
|
91
|
+
LD_PRELOAD: process.env.BIBLESYNC_FAIL_SEND_LIBRARY
|
|
92
|
+
});
|
|
93
|
+
try {
|
|
94
|
+
await speaker.waitFor(message => message.type === 'ready');
|
|
95
|
+
await speaker.waitFor(message => message.type === 'error' && message.message.includes('stopped after a network failure'), 15000);
|
|
96
|
+
await speaker.command('start', { mode: 'speaker' });
|
|
97
|
+
await speaker.command('stop');
|
|
98
|
+
} finally {
|
|
99
|
+
if (speaker.child.connected) await speaker.command('stop').catch(() => {});
|
|
100
|
+
if (speaker.child.connected) speaker.child.disconnect();
|
|
101
|
+
if (speaker.child.exitCode === null) speaker.child.kill();
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
const systemIntegration = process.env.BIBLESYNC_SYSTEM_PEER ? integration : test.skip;
|
|
106
|
+
systemIntegration('receives navigation from the system BibleSync library used by Xiphos', async () => {
|
|
107
|
+
const passphrase = `nsi-test-${randomBytes(12).toString('hex')}`;
|
|
108
|
+
const audience = peer('audience', 'audience', passphrase);
|
|
109
|
+
let speaker;
|
|
110
|
+
try {
|
|
111
|
+
await audience.waitFor(message => message.type === 'ready');
|
|
112
|
+
speaker = spawn(process.env.BIBLESYNC_SYSTEM_PEER, [passphrase], { stdio: ['pipe', 'pipe', 'pipe'] });
|
|
113
|
+
const lines = readline.createInterface({ input: speaker.stdout });
|
|
114
|
+
await new Promise((resolve, reject) => {
|
|
115
|
+
lines.once('line', line => line === 'ready' ? resolve() : reject(new Error(line)));
|
|
116
|
+
speaker.once('error', reject);
|
|
117
|
+
speaker.once('exit', code => reject(new Error(`System peer exited before startup (${code})`)));
|
|
118
|
+
});
|
|
119
|
+
const discovery = await audience.waitFor(message => message.type === 'speaker');
|
|
120
|
+
expect(discovery.data.user).toBe('system-peer');
|
|
121
|
+
await audience.command('listen', { speakerId: discovery.data.speakerId, listening: true });
|
|
122
|
+
speaker.stdin.write('send\n');
|
|
123
|
+
const message = await audience.waitFor(event => event.type === 'navigation');
|
|
124
|
+
expect(message.data).toMatchObject({ bible: 'KJV', reference: 'John.3.16' });
|
|
125
|
+
speaker.stdin.write('stop\n');
|
|
126
|
+
} finally {
|
|
127
|
+
if (speaker && speaker.exitCode === null) speaker.kill();
|
|
128
|
+
if (audience.child.connected) await audience.command('stop').catch(() => {});
|
|
129
|
+
if (audience.child.connected) audience.child.disconnect();
|
|
130
|
+
if (audience.child.exitCode === null) audience.child.kill();
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
systemIntegration('sends navigation and optional fields to the system BibleSync library', async () => {
|
|
135
|
+
const passphrase = `nsi-test-${randomBytes(12).toString('hex')}`;
|
|
136
|
+
const audience = spawn(process.env.BIBLESYNC_SYSTEM_PEER, [passphrase, 'audience'],
|
|
137
|
+
{ stdio: ['pipe', 'pipe', 'pipe'] });
|
|
138
|
+
const lines = readline.createInterface({ input: audience.stdout });
|
|
139
|
+
const received = [];
|
|
140
|
+
const waiters = [];
|
|
141
|
+
let peerError;
|
|
142
|
+
function failWaiters(error) {
|
|
143
|
+
peerError = error;
|
|
144
|
+
for (const waiter of waiters.splice(0)) {
|
|
145
|
+
clearTimeout(waiter.timer);
|
|
146
|
+
waiter.reject(error);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
audience.on('error', failWaiters);
|
|
150
|
+
audience.on('exit', code => failWaiters(new Error(`System peer exited (${code})`)));
|
|
151
|
+
lines.on('line', line => {
|
|
152
|
+
const index = waiters.findIndex(waiter => waiter.predicate(line));
|
|
153
|
+
if (index < 0) received.push(line);
|
|
154
|
+
else {
|
|
155
|
+
const waiter = waiters.splice(index, 1)[0];
|
|
156
|
+
clearTimeout(waiter.timer);
|
|
157
|
+
waiter.resolve(line);
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
function waitForLine(predicate) {
|
|
161
|
+
if (peerError) return Promise.reject(peerError);
|
|
162
|
+
const index = received.findIndex(predicate);
|
|
163
|
+
if (index >= 0) return Promise.resolve(received.splice(index, 1)[0]);
|
|
164
|
+
return new Promise((resolve, reject) => {
|
|
165
|
+
const waiter = { predicate, resolve, reject };
|
|
166
|
+
waiter.timer = setTimeout(() => {
|
|
167
|
+
waiters.splice(waiters.indexOf(waiter), 1);
|
|
168
|
+
reject(new Error(`Timed out waiting for system peer; received ${JSON.stringify(received)}`));
|
|
169
|
+
}, 7000);
|
|
170
|
+
waiters.push(waiter);
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
let speaker;
|
|
174
|
+
try {
|
|
175
|
+
await waitForLine(line => line === 'ready');
|
|
176
|
+
speaker = peer('speaker', 'speaker', passphrase);
|
|
177
|
+
await speaker.waitFor(message => message.type === 'ready');
|
|
178
|
+
await waitForLine(line => line === 'speaker');
|
|
179
|
+
await speaker.command('send', { data: {
|
|
180
|
+
bible: 'KJV', reference: 'John.3.16', alternateReference: 'John 3:16', group: '3'
|
|
181
|
+
} });
|
|
182
|
+
expect(await waitForLine(line => line.startsWith('navigation|')))
|
|
183
|
+
.toBe('navigation|KJV|John.3.16|John 3:16|3|BIBLE-VERSE');
|
|
184
|
+
audience.stdin.write('stop\n');
|
|
185
|
+
} finally {
|
|
186
|
+
lines.close();
|
|
187
|
+
if (audience.exitCode === null) audience.kill();
|
|
188
|
+
if (speaker && speaker.child.connected) await speaker.command('stop').catch(() => {});
|
|
189
|
+
if (speaker && speaker.child.connected) speaker.child.disconnect();
|
|
190
|
+
if (speaker && speaker.child.exitCode === null) speaker.child.kill();
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
integration('real peers require opt-in and deliver navigation without truncation', async () => {
|
|
195
|
+
const passphrase = `nsi-test-${randomBytes(12).toString('hex')}`;
|
|
196
|
+
const audience = peer('audience', 'audience', passphrase);
|
|
197
|
+
let speaker;
|
|
198
|
+
let outsider;
|
|
199
|
+
try {
|
|
200
|
+
await audience.waitFor(message => message.type === 'ready');
|
|
201
|
+
speaker = peer('speaker', 'speaker', passphrase, true);
|
|
202
|
+
await speaker.waitFor(message => message.type === 'ready');
|
|
203
|
+
const discovery = await audience.waitFor(message => message.type === 'speaker');
|
|
204
|
+
expect(discovery.data.listening).toBe(false);
|
|
205
|
+
await audience.waitFor(message => message.type === 'diagnostic' && message.data.message.startsWith('sync:'));
|
|
206
|
+
expect(audience.events.filter(message => message.type === 'navigation')).toEqual([]);
|
|
207
|
+
const navigation = { bible: 'KJV', reference: 'John.3.16', alternateReference: 'John.3.17', group: '2' };
|
|
208
|
+
await speaker.command('send', { data: navigation });
|
|
209
|
+
await new Promise(resolve => setTimeout(resolve, 1500));
|
|
210
|
+
expect(audience.events.filter(message => message.type === 'navigation')).toEqual([]);
|
|
211
|
+
await audience.command('listen', { speakerId: discovery.data.speakerId, listening: true });
|
|
212
|
+
await speaker.command('send', { data: navigation });
|
|
213
|
+
const received = await audience.waitFor(message => message.type === 'navigation');
|
|
214
|
+
expect(received.data).toMatchObject({ ...navigation, domain: 'BIBLE-VERSE', speakerId: discovery.data.speakerId });
|
|
215
|
+
await expect(audience.command('send', { data: navigation })).rejects.toThrow('Audience mode');
|
|
216
|
+
await expect(speaker.command('send', { data: { bible: 'KJV', reference: 'å'.repeat(1000) } }))
|
|
217
|
+
.rejects.toThrow('packet size');
|
|
218
|
+
await audience.command('listen', { speakerId: discovery.data.speakerId, listening: false });
|
|
219
|
+
await speaker.command('send', { data: navigation });
|
|
220
|
+
await new Promise(resolve => setTimeout(resolve, 1500));
|
|
221
|
+
expect(audience.events.filter(message => message.type === 'navigation')).toEqual([]);
|
|
222
|
+
outsider = peer('speaker', 'outsider', `${passphrase}-other`);
|
|
223
|
+
await outsider.waitFor(message => message.type === 'ready');
|
|
224
|
+
await audience.waitFor(message => message.type === 'diagnostic' && message.data.message.includes('outsider'));
|
|
225
|
+
expect(audience.events.filter(message => message.type === 'speaker' && message.data.user === 'outsider')).toEqual([]);
|
|
226
|
+
await outsider.command('stop');
|
|
227
|
+
await speaker.command('stop');
|
|
228
|
+
const removed = await audience.waitFor(message => message.type === 'speakerRemoved', 37000);
|
|
229
|
+
expect(removed.data.speakerId).toBe(discovery.data.speakerId);
|
|
230
|
+
} finally {
|
|
231
|
+
if (outsider && outsider.child.connected) await outsider.command('stop').catch(() => {});
|
|
232
|
+
if (speaker && speaker.child.connected) await speaker.command('stop').catch(() => {});
|
|
233
|
+
if (audience.child.connected) await audience.command('stop').catch(() => {});
|
|
234
|
+
if (outsider && outsider.child.connected) outsider.child.disconnect();
|
|
235
|
+
if (speaker && speaker.child.connected) speaker.child.disconnect();
|
|
236
|
+
if (audience.child.connected) audience.child.disconnect();
|
|
237
|
+
if (outsider && outsider.child.exitCode === null) outsider.child.kill();
|
|
238
|
+
if (speaker && speaker.child.exitCode === null) speaker.child.kill();
|
|
239
|
+
if (audience.child.exitCode === null) audience.child.kill();
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
integration('personal peers can explicitly follow one another in both directions', async () => {
|
|
244
|
+
const passphrase = `nsi-test-${randomBytes(12).toString('hex')}`;
|
|
245
|
+
const first = peer('personal', 'first', passphrase);
|
|
246
|
+
let second;
|
|
247
|
+
try {
|
|
248
|
+
await first.waitFor(message => message.type === 'ready');
|
|
249
|
+
second = peer('personal', 'second', passphrase);
|
|
250
|
+
await second.waitFor(message => message.type === 'ready');
|
|
251
|
+
const firstDiscovery = await first.waitFor(message => message.type === 'speaker', 14000);
|
|
252
|
+
const secondDiscovery = await second.waitFor(message => message.type === 'speaker', 14000);
|
|
253
|
+
await first.command('listen', { speakerId: firstDiscovery.data.speakerId, listening: true });
|
|
254
|
+
await second.command('listen', { speakerId: secondDiscovery.data.speakerId, listening: true });
|
|
255
|
+
await first.command('send', { data: { bible: 'KJV', reference: 'John.3.16' } });
|
|
256
|
+
await second.command('send', { data: { bible: 'KJV', reference: 'John.3.17' } });
|
|
257
|
+
expect((await first.waitFor(message => message.type === 'navigation')).data.reference).toBe('John.3.17');
|
|
258
|
+
expect((await second.waitFor(message => message.type === 'navigation')).data.reference).toBe('John.3.16');
|
|
259
|
+
} finally {
|
|
260
|
+
if (second && second.child.connected) await second.command('stop').catch(() => {});
|
|
261
|
+
if (first.child.connected) await first.command('stop').catch(() => {});
|
|
262
|
+
if (second && second.child.connected) second.child.disconnect();
|
|
263
|
+
if (first.child.connected) first.child.disconnect();
|
|
264
|
+
if (second && second.child.exitCode === null) second.child.kill();
|
|
265
|
+
if (first.child.exitCode === null) first.child.kill();
|
|
266
|
+
}
|
|
267
|
+
});
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/* This file is part of node-bible-sync.
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2026 Tobias Klein <contact@tklein.info>
|
|
4
|
+
|
|
5
|
+
node-bible-sync is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 2 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
node-bible-sync is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
13
|
+
See the GNU General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU General Public License
|
|
16
|
+
along with node-bible-sync. See the file COPYING.
|
|
17
|
+
If not, see <http://www.gnu.org/licenses/>. */
|
|
18
|
+
|
|
19
|
+
const { Worker } = require('worker_threads');
|
|
20
|
+
const { spawnSync } = require('child_process');
|
|
21
|
+
const os = require('os');
|
|
22
|
+
const { randomBytes } = require('crypto');
|
|
23
|
+
const BibleSync = require('..');
|
|
24
|
+
const addon = require('../build/Release/node_bible_sync.node');
|
|
25
|
+
|
|
26
|
+
const options = { application: 'Native test', version: '1', user: 'Tester',
|
|
27
|
+
passphrase: `nsi-test-${randomBytes(12).toString('hex')}` };
|
|
28
|
+
|
|
29
|
+
test('native binding rejects invalid construction and command arguments', () => {
|
|
30
|
+
expect(() => new addon.BibleSyncSession({}, () => {})).toThrow('identification fields');
|
|
31
|
+
const native = new addon.BibleSyncSession(options, () => {});
|
|
32
|
+
expect(() => native.start('1', 'invalid')).toThrow('Invalid BibleSync mode');
|
|
33
|
+
expect(() => native.enqueue('2', 'listen', {})).toThrow('speaker ID');
|
|
34
|
+
expect(() => native.enqueue('3', 'send', {})).toThrow('navigation fields');
|
|
35
|
+
expect(() => native.enqueue('4', 'stop', {})).toThrow('not running');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('native sessions enforce process-wide exclusivity and support restart', async () => {
|
|
39
|
+
const first = new BibleSync(options);
|
|
40
|
+
const second = new BibleSync(options);
|
|
41
|
+
await first.start('audience');
|
|
42
|
+
try {
|
|
43
|
+
await expect(second.start('speaker')).rejects.toThrow('already active');
|
|
44
|
+
await expect(first.sendNavigation({ bible: 'KJV', reference: 'John.3.16' }))
|
|
45
|
+
.rejects.toThrow('Audience mode');
|
|
46
|
+
await expect(first.setSpeakerListening('unknown', true)).rejects.toThrow('Unknown or expired');
|
|
47
|
+
} finally {
|
|
48
|
+
await first.stop();
|
|
49
|
+
}
|
|
50
|
+
await second.start('speaker');
|
|
51
|
+
try {
|
|
52
|
+
await expect(second.setSpeakerListening('unknown', true)).rejects.toThrow('Speaker mode');
|
|
53
|
+
await expect(second.sendNavigation({ bible: 'KJV', reference: 'é'.repeat(1000) }))
|
|
54
|
+
.rejects.toThrow('packet size');
|
|
55
|
+
} finally {
|
|
56
|
+
await second.stop();
|
|
57
|
+
}
|
|
58
|
+
await second.start('audience');
|
|
59
|
+
await second.stop();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('failed startup releases its active-session slot', async () => {
|
|
63
|
+
const bad = new BibleSync({ ...options, user: 'x'.repeat(2000) });
|
|
64
|
+
const next = new BibleSync(options);
|
|
65
|
+
await expect(bad.start('speaker')).rejects.toThrow('packet size');
|
|
66
|
+
await next.start('audience');
|
|
67
|
+
await next.stop();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('enforces the actual protocol byte limit without truncating UTF-8', async () => {
|
|
71
|
+
const session = new BibleSync(options);
|
|
72
|
+
const device = process.platform === 'win32' ? 'Windows PC' : `${os.machine()}: ${os.type()} @ ${os.hostname()}`;
|
|
73
|
+
const system = process.platform === 'win32' ? 'Windows' : process.platform === 'linux' ? 'Linux' : 'UNIX';
|
|
74
|
+
const fields = {
|
|
75
|
+
'app.name': options.application, 'app.version': options.version,
|
|
76
|
+
'app.inst.uuid': '0'.repeat(36), 'app.os': system, 'app.device': device,
|
|
77
|
+
'app.user': options.user, 'msg.sync.passPhrase': options.passphrase,
|
|
78
|
+
'msg.sync.bibleAbbrev': 'KJV', 'msg.sync.domain': 'BIBLE-VERSE',
|
|
79
|
+
'msg.sync.group': '1', 'msg.sync.altVerse': '', 'msg.sync.verse': ''
|
|
80
|
+
};
|
|
81
|
+
const overhead = Object.entries(fields).reduce(
|
|
82
|
+
(sum, [key, value]) => sum + Buffer.byteLength(key) + Buffer.byteLength(value) + 2, 0
|
|
83
|
+
);
|
|
84
|
+
const limit = 1248 - overhead;
|
|
85
|
+
expect(limit).toBeGreaterThan(0);
|
|
86
|
+
await session.start('speaker');
|
|
87
|
+
try {
|
|
88
|
+
await session.sendNavigation({ bible: 'KJV', reference: 'x'.repeat(limit) });
|
|
89
|
+
await expect(session.sendNavigation({ bible: 'KJV', reference: 'x'.repeat(limit + 1) }))
|
|
90
|
+
.rejects.toThrow('packet size');
|
|
91
|
+
await expect(session.sendNavigation({ bible: 'KJV', reference: 'é'.repeat(Math.floor(limit / 2) + 1) }))
|
|
92
|
+
.rejects.toThrow('packet size');
|
|
93
|
+
} finally {
|
|
94
|
+
await session.stop();
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test('repeated immediate restarts preserve exclusive ownership', async () => {
|
|
99
|
+
const session = new BibleSync(options);
|
|
100
|
+
const other = new BibleSync(options);
|
|
101
|
+
for (let i = 0; i < 20; i++) {
|
|
102
|
+
await session.start('audience');
|
|
103
|
+
await expect(other.start('audience')).rejects.toThrow('already active');
|
|
104
|
+
await session.stop();
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test('the active-session limit also applies to worker environments', async () => {
|
|
109
|
+
const session = new BibleSync(options);
|
|
110
|
+
await session.start('audience');
|
|
111
|
+
try {
|
|
112
|
+
const worker = new Worker(`
|
|
113
|
+
const { parentPort, workerData } = require('worker_threads');
|
|
114
|
+
const Native = require(workerData);
|
|
115
|
+
const session = new Native({ application: 'worker', version: '1', user: 'worker' });
|
|
116
|
+
session.start('audience').then(
|
|
117
|
+
() => parentPort.postMessage('unexpected success'),
|
|
118
|
+
error => parentPort.postMessage(error.message)
|
|
119
|
+
);
|
|
120
|
+
`, { eval: true, workerData: require.resolve('..') });
|
|
121
|
+
const message = await new Promise((resolve, reject) => {
|
|
122
|
+
worker.once('message', resolve);
|
|
123
|
+
worker.once('error', reject);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
expect(message).toMatch(/already active/);
|
|
127
|
+
await worker.terminate();
|
|
128
|
+
} finally {
|
|
129
|
+
await session.stop();
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test('worker teardown closes an active session and releases its slot', async () => {
|
|
134
|
+
const worker = new Worker(`
|
|
135
|
+
const { parentPort, workerData } = require('worker_threads');
|
|
136
|
+
const Native = require(workerData);
|
|
137
|
+
const session = new Native({ application: 'worker', version: '1', user: 'worker' });
|
|
138
|
+
session.start('audience').then(() => parentPort.postMessage('ready'), error => {
|
|
139
|
+
parentPort.postMessage(error.message);
|
|
140
|
+
});
|
|
141
|
+
`, { eval: true, workerData: require.resolve('..') });
|
|
142
|
+
const message = await new Promise((resolve, reject) => {
|
|
143
|
+
worker.once('message', resolve);
|
|
144
|
+
worker.once('error', reject);
|
|
145
|
+
});
|
|
146
|
+
expect(message).toBe('ready');
|
|
147
|
+
await worker.terminate();
|
|
148
|
+
const next = new BibleSync(options);
|
|
149
|
+
await next.start('audience');
|
|
150
|
+
await next.stop();
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test('explicit process exit does not hang with an active session', () => {
|
|
154
|
+
const script = `
|
|
155
|
+
const Native = require(${JSON.stringify(require.resolve('..'))});
|
|
156
|
+
const session = new Native({ application: 'exit', version: '1', user: 'exit' });
|
|
157
|
+
session.start('audience').then(() => process.exit(0), () => process.exit(1));
|
|
158
|
+
`;
|
|
159
|
+
const result = spawnSync(process.execPath, ['-e', script], { timeout: 5000, encoding: 'utf8' });
|
|
160
|
+
expect(result.error).toBeUndefined();
|
|
161
|
+
expect(result.status).toBe(0);
|
|
162
|
+
});
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/* This file is part of node-bible-sync.
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2026 Tobias Klein <contact@tklein.info>
|
|
4
|
+
|
|
5
|
+
node-bible-sync is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 2 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
node-bible-sync is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
13
|
+
See the GNU General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU General Public License
|
|
16
|
+
along with node-bible-sync. See the file COPYING.
|
|
17
|
+
If not, see <http://www.gnu.org/licenses/>. */
|
|
18
|
+
|
|
19
|
+
jest.mock('fs');
|
|
20
|
+
jest.mock('../scripts/fetch_biblesync');
|
|
21
|
+
|
|
22
|
+
const fs = require('fs');
|
|
23
|
+
const realFs = jest.requireActual('fs');
|
|
24
|
+
const path = require('path');
|
|
25
|
+
const { fetchBibleSync } = require('../scripts/fetch_biblesync');
|
|
26
|
+
|
|
27
|
+
const sourcePath = path.join(__dirname, '..', 'biblesync', 'src', 'biblesync.cc');
|
|
28
|
+
const headerPath = path.join(__dirname, '..', 'biblesync', 'include', 'biblesync.hh');
|
|
29
|
+
const output = path.join('generated', 'biblesync.cc');
|
|
30
|
+
let originalArgv;
|
|
31
|
+
let upstreamSource;
|
|
32
|
+
let upstreamHeader;
|
|
33
|
+
|
|
34
|
+
beforeAll(() => {
|
|
35
|
+
upstreamSource = realFs.readFileSync(sourcePath, 'utf8').replace(/\r\n/g, '\n');
|
|
36
|
+
upstreamHeader = realFs.readFileSync(headerPath, 'utf8').replace(/\r\n/g, '\n');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
jest.resetAllMocks();
|
|
41
|
+
originalArgv = process.argv;
|
|
42
|
+
process.argv = ['node', 'prepare_biblesync.js', output];
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
afterEach(() => {
|
|
46
|
+
process.argv = originalArgv;
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
function prepare(source, header) {
|
|
50
|
+
fs.readFileSync.mockImplementation(file => {
|
|
51
|
+
if (file === sourcePath) return source;
|
|
52
|
+
if (file === headerPath) return header;
|
|
53
|
+
throw new Error(`Unexpected input: ${file}`);
|
|
54
|
+
});
|
|
55
|
+
jest.isolateModules(() => require('../scripts/prepare_biblesync'));
|
|
56
|
+
return fs.writeFileSync.mock.calls;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
test('prepares identical output for LF and CRLF checkouts without changing upstream files', () => {
|
|
60
|
+
const lfOutput = prepare(upstreamSource, upstreamHeader);
|
|
61
|
+
expect(fetchBibleSync).toHaveBeenCalledTimes(1);
|
|
62
|
+
expect(lfOutput.map(([file]) => file)).toEqual([
|
|
63
|
+
output,
|
|
64
|
+
path.join(path.dirname(output), 'biblesync.hh')
|
|
65
|
+
]);
|
|
66
|
+
expect(lfOutput[0][1]).toContain('if (::bind(server_fd, (struct sockaddr*)&server,');
|
|
67
|
+
expect(lfOutput[1][1]).toContain('bool winsock_initialized = false;');
|
|
68
|
+
|
|
69
|
+
fs.writeFileSync.mockClear();
|
|
70
|
+
const crlfOutput = prepare(
|
|
71
|
+
upstreamSource.replace(/\n/g, '\r\n'),
|
|
72
|
+
upstreamHeader.replace(/\n/g, '\r\n')
|
|
73
|
+
);
|
|
74
|
+
expect(crlfOutput).toEqual(lfOutput);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test('still rejects unexpected upstream source', () => {
|
|
78
|
+
const alteredSource = upstreamSource.replace('server_fd = client_fd = -1;', 'server_fd = -1;');
|
|
79
|
+
expect(() => prepare(alteredSource, upstreamHeader)).toThrow('Unexpected BibleSync 2.2.0 source');
|
|
80
|
+
expect(fs.writeFileSync).not.toHaveBeenCalled();
|
|
81
|
+
});
|