190 lines
5.3 KiB
JavaScript
190 lines
5.3 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
|
|
require('../src/common');
|
|
|
|
const { createPlayerActionService } = require('../src/web/lib/player-actions');
|
|
|
|
test('player actions use the configured bridge url for commands', async () => {
|
|
const fetchCalls = [];
|
|
const originalFetch = global.fetch;
|
|
global.fetch = async function (url, init) {
|
|
fetchCalls.push({ url, init });
|
|
return {
|
|
ok: true,
|
|
status: 200,
|
|
headers: {
|
|
get() {
|
|
return null;
|
|
}
|
|
},
|
|
async json() {
|
|
return { ok: true };
|
|
},
|
|
async text() {
|
|
return JSON.stringify({ ok: true });
|
|
}
|
|
};
|
|
};
|
|
|
|
const playerActionService = createPlayerActionService({
|
|
common: {},
|
|
playerInternalBaseUrl: 'http://player:8081'
|
|
});
|
|
|
|
const originalIdentifier = process.env.PLAYER_IDENTIFIER;
|
|
process.env.PLAYER_IDENTIFIER = 'player-local';
|
|
|
|
try {
|
|
const response = await playerActionService.forwardPlayerCommand('demo', { command: 'refresh' });
|
|
|
|
assert.deepEqual(response, { ok: true });
|
|
assert.equal(fetchCalls.length, 1);
|
|
assert.equal(fetchCalls[0].url, 'http://player:8081/api/screens/demo/commands');
|
|
} finally {
|
|
process.env.PLAYER_IDENTIFIER = originalIdentifier;
|
|
global.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test('player actions read screen connections from the bridge base url', async () => {
|
|
const fetchCalls = [];
|
|
const originalFetch = global.fetch;
|
|
global.fetch = async function (url, init) {
|
|
fetchCalls.push({ url, init });
|
|
return {
|
|
ok: true,
|
|
status: 200,
|
|
headers: { get() { return null; } },
|
|
async json() {
|
|
return {
|
|
screenSlug: 'demo',
|
|
connections: [
|
|
{ id: 'bridge-1', playerPublicBaseUrl: 'http://bridge.example' }
|
|
]
|
|
};
|
|
},
|
|
async text() {
|
|
return JSON.stringify({ ok: true });
|
|
}
|
|
};
|
|
};
|
|
|
|
const playerActionService = createPlayerActionService({
|
|
common: {},
|
|
pool: {
|
|
async query() {
|
|
return [[[]]];
|
|
}
|
|
},
|
|
playerInternalBaseUrl: 'http://bridge.example'
|
|
});
|
|
|
|
try {
|
|
const response = await playerActionService.getScreenConnections('demo');
|
|
|
|
assert.equal(response.count, 1);
|
|
assert.deepEqual(response.connections.map(function (connection) { return connection.id; }), ['bridge-1']);
|
|
assert.equal(fetchCalls.length, 1);
|
|
assert.equal(fetchCalls[0].url, 'http://bridge.example/api/screens/demo/connections');
|
|
} finally {
|
|
global.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test('player actions merge bridge and player screen connections', async () => {
|
|
const fetchCalls = [];
|
|
const originalFetch = global.fetch;
|
|
global.fetch = async function (url, init) {
|
|
fetchCalls.push({ url, init });
|
|
if (String(url).indexOf('player-bridge:8090') !== -1) {
|
|
return {
|
|
ok: true,
|
|
status: 200,
|
|
headers: { get() { return null; } },
|
|
async json() {
|
|
return {
|
|
screenSlug: 'demo',
|
|
connections: [
|
|
{ id: 'bridge-1', playerPublicBaseUrl: 'http://bridge.example' }
|
|
]
|
|
};
|
|
},
|
|
async text() {
|
|
return JSON.stringify({ ok: true });
|
|
}
|
|
};
|
|
}
|
|
|
|
return {
|
|
ok: true,
|
|
status: 200,
|
|
headers: { get() { return null; } },
|
|
async json() {
|
|
return {
|
|
screenSlug: 'demo',
|
|
connections: [
|
|
{ id: 'player-1', playerPublicBaseUrl: 'http://player.example' }
|
|
]
|
|
};
|
|
},
|
|
async text() {
|
|
return JSON.stringify({ ok: true });
|
|
}
|
|
};
|
|
};
|
|
|
|
const playerActionService = createPlayerActionService({
|
|
common: {},
|
|
playerInternalBaseUrl: 'http://player:8081',
|
|
bridgeInternalBaseUrl: 'http://player-bridge:8090'
|
|
});
|
|
|
|
try {
|
|
const response = await playerActionService.getScreenConnections('demo');
|
|
|
|
assert.equal(fetchCalls.length, 2);
|
|
assert.deepEqual(fetchCalls.map(function (call) { return call.url; }).sort(), [
|
|
'http://player-bridge:8090/api/screens/demo/connections',
|
|
'http://player:8081/api/screens/demo/connections'
|
|
]);
|
|
assert.equal(response.count, 2);
|
|
assert.deepEqual(response.connections.map(function (connection) { return connection.id; }).sort(), ['bridge-1', 'player-1']);
|
|
} finally {
|
|
global.fetch = originalFetch;
|
|
}
|
|
});
|
|
|
|
test('player actions send device commands through the bridge base url', async () => {
|
|
const fetchCalls = [];
|
|
const originalFetch = global.fetch;
|
|
global.fetch = async function (url, init) {
|
|
fetchCalls.push({ url, init });
|
|
return {
|
|
ok: true,
|
|
status: 200,
|
|
headers: { get() { return null; } },
|
|
async json() {
|
|
return { ok: true, sent: true };
|
|
},
|
|
async text() {
|
|
return JSON.stringify({ ok: true, sent: true });
|
|
}
|
|
};
|
|
};
|
|
|
|
const playerActionService = createPlayerActionService({
|
|
common: {},
|
|
bridgeInternalBaseUrl: 'http://player-bridge:8090'
|
|
});
|
|
|
|
try {
|
|
const response = await playerActionService.forwardPlayerCommandToDevice('device-123', { command: 'pause' });
|
|
|
|
assert.deepEqual(response, { ok: true, sent: true });
|
|
assert.equal(fetchCalls.length, 1);
|
|
assert.equal(fetchCalls[0].url, 'http://player-bridge:8090/api/players/device-123/commands');
|
|
} finally {
|
|
global.fetch = originalFetch;
|
|
}
|
|
}); |