forked from mtusi2026/mtusi2026lab7
45 lines
1.4 KiB
TypeScript
45 lines
1.4 KiB
TypeScript
import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
|
|
import { hostname, tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { afterEach, describe, expect, it } from 'vitest';
|
|
import {
|
|
getProcessInstanceId,
|
|
persistInstanceIdFile,
|
|
resetInstanceIdForTest,
|
|
} from './instance-id.js';
|
|
|
|
describe('instance-id (opaque replica alias)', () => {
|
|
const savedInstanceId = process.env.INSTANCE_ID;
|
|
|
|
afterEach(() => {
|
|
if (savedInstanceId === undefined) delete process.env.INSTANCE_ID;
|
|
else process.env.INSTANCE_ID = savedInstanceId;
|
|
resetInstanceIdForTest();
|
|
});
|
|
|
|
it('generates a stable process-singleton opaque alias without exposing os.hostname()', () => {
|
|
delete process.env.INSTANCE_ID;
|
|
resetInstanceIdForTest();
|
|
const first = getProcessInstanceId('rep');
|
|
const second = getProcessInstanceId('rep');
|
|
expect(first).toBe(second);
|
|
expect(first).toMatch(/^rep-[0-9a-f]{12}$/u);
|
|
expect(first).not.toBe(hostname());
|
|
|
|
resetInstanceIdForTest();
|
|
const wrk = getProcessInstanceId('wrk');
|
|
expect(wrk).toMatch(/^wrk-[0-9a-f]{12}$/u);
|
|
});
|
|
|
|
it('persists the opaque alias to disk for stop-one.sh matching', () => {
|
|
const dir = mkdtempSync(join(tmpdir(), 'lab7-inst-'));
|
|
try {
|
|
const file = join(dir, 'instance-id');
|
|
persistInstanceIdFile('rep-a1b2c3d4e5f6', file);
|
|
expect(readFileSync(file, 'utf8').trim()).toBe('rep-a1b2c3d4e5f6');
|
|
} finally {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
});
|