File size: 2,684 Bytes
4e23b01 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | // test/server.test.js
import { test } from 'vitest';
import assert from 'node:assert/strict';
import net from 'node:net';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { startServer } from '../src/server.js';
async function tmpDir() {
return fs.mkdtemp(path.join(os.tmpdir(), 'minidb-srv-'));
}
function encode(...args) {
let s = `*${args.length}\r\n`;
for (const a of args) {
const b = Buffer.from(a);
s += `$${b.length}\r\n${a}\r\n`;
}
return s;
}
function connect(port) {
return new Promise((resolve, reject) => {
const sock = net.connect(port, '127.0.0.1');
sock.once('connect', () => resolve(sock));
sock.once('error', reject);
});
}
// Send one command and resolve with the next response chunk.
function send(sock, cmd) {
return new Promise((resolve) => {
sock.once('data', (d) => resolve(d.toString()));
sock.write(cmd);
});
}
test('RESP server: PING / SET / GET', async () => {
const dir = await tmpDir();
const srv = await startServer({ dir, port: 0, fsyncPolicy: 'no' });
try {
const sock = await connect(srv.port);
assert.equal(await send(sock, encode('PING')), '+PONG\r\n');
assert.equal(await send(sock, encode('SET', 'foo', 'bar')), '+OK\r\n');
assert.equal(await send(sock, encode('GET', 'foo')), '$3\r\nbar\r\n');
assert.equal(await send(sock, encode('GET', 'missing')), '$-1\r\n');
sock.end();
} finally {
await srv.close();
await fs.rm(dir, { recursive: true, force: true });
}
});
test('RESP server: MGET / DEL / DBSIZE', async () => {
const dir = await tmpDir();
const srv = await startServer({ dir, port: 0, fsyncPolicy: 'no' });
try {
const sock = await connect(srv.port);
await send(sock, encode('SET', 'a', '1'));
await send(sock, encode('SET', 'b', '2'));
assert.equal(await send(sock, encode('MGET', 'a', 'b', 'z')), '*3\r\n$1\r\n1\r\n$1\r\n2\r\n$-1\r\n');
assert.equal(await send(sock, encode('DBSIZE')), ':2\r\n');
assert.equal(await send(sock, encode('DEL', 'a')), ':1\r\n');
assert.equal(await send(sock, encode('DBSIZE')), ':1\r\n');
sock.end();
} finally {
await srv.close();
await fs.rm(dir, { recursive: true, force: true });
}
});
test('RESP server: unknown command returns an error', async () => {
const dir = await tmpDir();
const srv = await startServer({ dir, port: 0, fsyncPolicy: 'no' });
try {
const sock = await connect(srv.port);
const r = await send(sock, encode('NOPE'));
assert.ok(r.startsWith('-ERR'));
sock.end();
} finally {
await srv.close();
await fs.rm(dir, { recursive: true, force: true });
}
});
|