"use strict";
const test = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs/promises");
const os = require("node:os");
const path = require("node:path");
const { createWorkerIdentity, toPublicWorkerIdentity } = require("../lib/workerIdentity");
const {
readActivity, createInventoryReader, readActiveFileSize,
ACTIVE_JOB_LIMIT, WORKER_LIMIT, ACTIVITY_READ_PROJECTION,
} = require("../lib/activity");
const { createDashboardController, RECENT_LIMIT } = require("../controllers/dashboardController");
const { STATUS_READ_PROJECTION } = require("../lib/jobValidation");
const { QUERY_TIMEOUT_MS } = require("../lib/statusQuery");
const HOST = "download-host";
const START = "2026-01-01T12:00:00.000Z";
const CLAIM = "2026-01-01T12:01:00.000Z";
const RUN_ID = "7b3c9e3a-8b0b-41c6-988c-8e9e01dbbf4a";
const identity = (changes = {}) => ({
name: "proxer-worker-1", pid: 1234, host: HOST, run_id: RUN_ID, started_at: START, ...changes,
});
const processRecord = (changes = {}) => ({
name: "proxer-worker-1", pid: 1234, pm_id: "1", status: "online", startedAtISO: START, ...changes,
});
const activeRecord = (changes = {}) => ({
_id: "job-1", filename: "ExampleS001E001_engsub", processing: true, created_at: START,
started_at: CLAIM, url: "https://proxer.me/watch/42/1/engsub", worker: identity(), ...changes,
});
function activityDatabase(records = [], failure) {
const reads = [];
const db = { collection(name) {
assert.equal(name, "queue");
return { find(filter, options) {
const read = { filter, options };
reads.push(read);
const cursor = {
sort(value) { read.sort = value; return cursor; },
limit(value) { read.limit = value; return cursor; },
async toArray() { if (failure) throw failure; return records.slice(0, read.limit); },
};
return cursor;
} };
} };
return { db, reads };
}
async function activityFixture(records, processes = [processRecord()], options = {}) {
const fixture = activityDatabase(records);
const fileChecks = [];
const result = await readActivity(fixture.db, {
host: HOST, listWorkers: async () => processes,
readFileSize: async job => { fileChecks.push(job._id); return 123; }, ...options,
});
return { ...fixture, result, fileChecks };
}
test("worker identity is immutable, names are safe, and each actual process run has a unique ID", () => {
const explicit = createWorkerIdentity({
env: { PROXER_WORKER_NAME: "proxer-worker-9", name: "proxer-worker-1" },
pid: 1234, host: HOST, uuid: () => RUN_ID, now: () => new Date(START),
});
assert.equal(Object.isFrozen(explicit), true);
assert.throws(() => { explicit.pid = 5; }, TypeError);
assert.deepEqual(explicit, identity({ name: "proxer-worker-9" }));
const unsafe = createWorkerIdentity({
env: { PROXER_WORKER_NAME: "", name: "bad name" },
pid: 1234, host: HOST, uuid: () => RUN_ID, now: () => new Date(START),
});
assert.equal(unsafe.name, "worker-1234");
const actual = createWorkerIdentity({ env: {} });
assert.equal(actual.pid, process.pid);
assert.equal(actual.host, os.hostname());
assert.equal(actual.name, `worker-${process.pid}`);
assert.notEqual(actual.run_id, createWorkerIdentity({ env: {} }).run_id);
});
test("worker identity DTO drops extra fields and rejects incomplete or unsafe legacy metadata", () => {
assert.deepEqual(toPublicWorkerIdentity({ ...identity(), token: "secret", env: { PRIVATE: "value" } }), identity());
for (const value of [
null, [], {}, { name: "proxer-worker-1", pid: 1234 },
identity({ pid: "1234" }), identity({ pid: 0 }), identity({ pid: NaN }),
identity({ name: "