Initial commit: CraftDock as of 2026-11-01
The previous GitHub history was lost with the account (the local .git was missing its refs and most objects), so this repository starts from the existing code. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
640e52c0c9
99 files changed
+12115
No files matched your search
@@ -0,0 +1,22 @@
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { registerSockets } from '../../src/sockets/index.js';
|
||||
|
||||
describe('registerSockets', () => {
|
||||
it('creates /logs and /status namespaces with auth middleware', () => {
|
||||
const namespaces = {};
|
||||
const io = {
|
||||
of: vi.fn((name) => {
|
||||
const ns = { use: vi.fn(), on: vi.fn() };
|
||||
namespaces[name] = ns;
|
||||
return ns;
|
||||
}),
|
||||
};
|
||||
registerSockets(io, {
|
||||
dockerService: {}, appState: {}, sessionMiddleware: (req, res, next) => next(),
|
||||
});
|
||||
expect(io.of).toHaveBeenCalledWith('/logs');
|
||||
expect(io.of).toHaveBeenCalledWith('/status');
|
||||
expect(namespaces['/logs'].use).toHaveBeenCalled();
|
||||
expect(namespaces['/status'].use).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,56 @@
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { EventEmitter } from 'node:events';
|
||||
import { PassThrough } from 'node:stream';
|
||||
import { registerLogsSocket } from '../../src/sockets/logsSocket.js';
|
||||
|
||||
function fakeNamespace() {
|
||||
const ns = new EventEmitter();
|
||||
ns.on = ns.on.bind(ns);
|
||||
return ns;
|
||||
}
|
||||
function fakeSocket() {
|
||||
const s = new EventEmitter();
|
||||
s.emit = s.emit.bind(s);
|
||||
s.emitted = [];
|
||||
const origEmit = s.emit;
|
||||
s.emit = (ev, payload) => { s.emitted.push([ev, payload]); return origEmit(ev, payload); };
|
||||
return s;
|
||||
}
|
||||
|
||||
describe('logsSocket', () => {
|
||||
it('streams log chunks to the client', async () => {
|
||||
const logStream = new PassThrough();
|
||||
const dockerService = { logStream: vi.fn().mockResolvedValue(logStream) };
|
||||
const appState = { getAdapter: vi.fn().mockResolvedValue({ sendCommand: vi.fn() }) };
|
||||
const ns = fakeNamespace();
|
||||
registerLogsSocket(ns, { dockerService, appState });
|
||||
|
||||
const socket = fakeSocket();
|
||||
ns.emit('connection', socket);
|
||||
await new Promise((r) => setTimeout(r, 10)); // allow async logStream()
|
||||
|
||||
logStream.write('hello world');
|
||||
await new Promise((r) => setTimeout(r, 5));
|
||||
|
||||
const logs = socket.emitted.filter(([ev]) => ev === 'log').map(([, p]) => p);
|
||||
expect(logs.join('')).toContain('hello world');
|
||||
});
|
||||
|
||||
it('routes command events to adapter.sendCommand', async () => {
|
||||
const logStream = new PassThrough();
|
||||
const sendCommand = vi.fn().mockResolvedValue('command result');
|
||||
const dockerService = { logStream: vi.fn().mockResolvedValue(logStream) };
|
||||
const appState = { getAdapter: vi.fn().mockResolvedValue({ sendCommand }) };
|
||||
const ns = fakeNamespace();
|
||||
registerLogsSocket(ns, { dockerService, appState });
|
||||
|
||||
const socket = fakeSocket();
|
||||
ns.emit('connection', socket);
|
||||
await new Promise((r) => setTimeout(r, 10));
|
||||
|
||||
socket.emit('command', 'list');
|
||||
await new Promise((r) => setTimeout(r, 5));
|
||||
|
||||
expect(sendCommand).toHaveBeenCalledWith('list');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,33 @@
|
||||
import { describe, it, expect, vi } from 'vitest';
|
||||
import { buildStatusPayload } from '../../src/sockets/statusSocket.js';
|
||||
|
||||
describe('buildStatusPayload', () => {
|
||||
it('aggregates state, stats and players when running', async () => {
|
||||
const dockerService = {
|
||||
inspect: vi.fn().mockResolvedValue({ state: 'running', type: 'PAPER' }),
|
||||
stats: vi.fn().mockResolvedValue({ cpuPct: 5, memUsedMb: 256, memPct: 25 }),
|
||||
};
|
||||
const appState = {
|
||||
getAdapter: vi.fn().mockResolvedValue({
|
||||
listPlayers: vi.fn().mockResolvedValue({ online: 2, max: 20, players: ['a', 'b'] }),
|
||||
}),
|
||||
};
|
||||
const payload = await buildStatusPayload({ dockerService, appState });
|
||||
expect(payload).toMatchObject({
|
||||
state: 'running', cpuPct: 5, memPct: 25,
|
||||
players: { online: 2, max: 20, players: ['a', 'b'] },
|
||||
});
|
||||
});
|
||||
|
||||
it('returns zeros and empty players when not running', async () => {
|
||||
const dockerService = {
|
||||
inspect: vi.fn().mockResolvedValue({ state: 'exited', type: 'PAPER' }),
|
||||
stats: vi.fn(),
|
||||
};
|
||||
const appState = { getAdapter: vi.fn() };
|
||||
const payload = await buildStatusPayload({ dockerService, appState });
|
||||
expect(payload.state).toBe('exited');
|
||||
expect(payload.players).toEqual({ online: 0, max: 0, players: [] });
|
||||
expect(dockerService.stats).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user