30 lines
2.0 KiB
TypeScript
30 lines
2.0 KiB
TypeScript
import './setup_env';
|
|
import { expect, test } from 'bun:test';
|
|
import { LOCK_NAME, TTL_SECONDS, parseLockRequest } from '../src/eddy_locks';
|
|
|
|
test('lock names are short lowercase slugs', () => {
|
|
for (const ok of ['eddy-autorelease', 'a', 'devicesim.publish_2', 'x'.repeat(64)]) expect(LOCK_NAME.test(ok)).toBe(true);
|
|
for (const bad of ['', '-a', 'A', 'a b', 'a/b', '../x', 'x'.repeat(65)]) expect(LOCK_NAME.test(bad)).toBe(false);
|
|
});
|
|
|
|
test('register takes a holder, an optional ttl (default 10 min) and small info', () => {
|
|
expect(parseLockRequest('register', {holder: 'mac-a pid 1'})).toEqual({holder: 'mac-a pid 1', ttlMs: TTL_SECONDS.default * 1000, info: null});
|
|
expect(parseLockRequest('register', {holder: 'h', ttlSeconds: 30, info: {a: 1}}).ttlMs).toBe(30_000);
|
|
expect(parseLockRequest('register', {holder: 'h', ttlSeconds: 3600}).ttlMs).toBe(3_600_000);
|
|
for (const body of [null, [], 'x', {}, {holder: ''}, {holder: 'é'}, {holder: 'h', ttlSeconds: 29}, {holder: 'h', ttlSeconds: 3601},
|
|
{holder: 'h', ttlSeconds: 60.5}, {holder: 'h', ttlSeconds: '60'}, {holder: 'h', info: 'x'}, {holder: 'h', info: [1]}, {holder: 'h', info: {x: 'y'.repeat(4096)}}])
|
|
expect(() => parseLockRequest('register', body)).toThrow();
|
|
});
|
|
|
|
test('heartbeat and unregister need the token; unregister keeps a bounded result', () => {
|
|
const token = 'A'.repeat(43);
|
|
expect(parseLockRequest('heartbeat', {token})).toEqual({token, ttlMs: TTL_SECONDS.default * 1000, status: null});
|
|
expect(parseLockRequest('heartbeat', {token, ttlSeconds: 120, status: {step: 's'}})).toEqual({token, ttlMs: 120_000, status: {step: 's'}});
|
|
expect(parseLockRequest('unregister', {token, result: {ok: true}})).toEqual({token, ttlMs: 0, result: {ok: true}});
|
|
for (const body of [{}, {token: 'short'}, {token: 'bad token with spaces and more than 32 chars'}, {token: 1}]) {
|
|
expect(() => parseLockRequest('heartbeat', body)).toThrow();
|
|
expect(() => parseLockRequest('unregister', body)).toThrow();
|
|
}
|
|
expect(() => parseLockRequest('unregister', {token, result: {x: 'y'.repeat(8192)}})).toThrow();
|
|
});
|