import { test } from "node:test";import assert from "node:assert/strict";import { h, load } from "../../lib/harness.mjs";const { escalationEmail } = await load();const u = (id, managerId, email) => ({ id, name: `name ${id}`, managerId: h.maybe(managerId), email: h.maybe(email) });const run = (users, id) => { const r = h.res(() => escalationEmail(users, id)); return r.ok ? { ok: true, value: h.opt(r.value) } : r;};const found = (email) => ({ ok: true, value: { some: true, value: email } });const absent = { ok: true, value: { some: false } };const fail = (kind, id) => ({ ok: false, error: { kind, id } });const org = [ u("ceo", undefined, "ceo@co.test"), u("vp", "ceo", undefined), u("dir", "vp", ""), u("mgr", "dir", "mgr@co.test"), u("eng", "mgr", "eng@co.test"), u("intern", "dir", "intern@co.test"),];test("returns the direct manager's email", () => { assert.deepEqual(run(org, "eng"), found("mgr@co.test"));});test("skips managers with no or empty email", () => { assert.deepEqual(run(org, "intern"), found("ceo@co.test")); assert.deepEqual(run(org, "mgr"), found("ceo@co.test"));});test("the user's own email is never returned", () => { assert.deepEqual(run(org, "vp"), found("ceo@co.test")); assert.deepEqual(run([u("solo", undefined, "solo@co.test")], "solo"), absent);});test("top of chain without an email is absent", () => { assert.deepEqual(run(org, "ceo"), absent); const users = [u("a", "b", undefined), u("b", "c", ""), u("c", undefined, "")]; assert.deepEqual(run(users, "a"), absent);});test("a manager id that matches no user ends the chain", () => { assert.deepEqual(run([u("a", "ghost", "a@co.test")], "a"), absent); assert.deepEqual(run([u("a", "b", undefined), u("b", "nobody", undefined)], "a"), absent); assert.deepEqual(run([u("a", "", undefined)], "a"), absent);});test("unknown user", () => { assert.deepEqual(run(org, "nobody"), fail("unknown_user", "nobody")); assert.deepEqual(run([], "a"), fail("unknown_user", "a")); assert.deepEqual(run(org, ""), fail("unknown_user", ""));});test("a user who manages themselves is a cycle", () => { assert.deepEqual(run([u("a", "a", "a@co.test")], "a"), fail("cycle", "a"));});test("a chain that loops back to the starting user is a cycle", () => { const users = [u("a", "b", "a@co.test"), u("b", "a", "")]; assert.deepEqual(run(users, "a"), fail("cycle", "a")); assert.deepEqual(run(users, "b"), found("a@co.test"));});test("a loop above the starting user reports the first repeated user", () => { const users = [u("a", "b", undefined), u("b", "c", undefined), u("c", "d", ""), u("d", "b", undefined)]; assert.deepEqual(run(users, "a"), fail("cycle", "b"));});test("an email found before the loop wins", () => { const users = [u("a", "b", undefined), u("b", "c", undefined), u("c", "b", "c@co.test")]; assert.deepEqual(run(users, "a"), found("c@co.test"));});test("with duplicate ids the first user counts", () => { const users = [u("a", "m", undefined), u("m", undefined, "first@co.test"), u("m", undefined, "second@co.test"), u("a", undefined, undefined)]; assert.deepEqual(run(users, "a"), found("first@co.test"));});test("ids that look like object properties", () => { assert.deepEqual(run([u("a", "toString", undefined)], "a"), absent); assert.deepEqual(run([u("a", "constructor", undefined)], "constructor"), fail("unknown_user", "constructor")); assert.deepEqual(run([u("constructor", "__proto__", undefined), u("__proto__", undefined, "p@co.test")], "constructor"), found("p@co.test"));});test("input is not modified", () => { const users = org.map((x) => Object.freeze({ ...x })); Object.freeze(users); assert.deepEqual(run(users, "intern"), found("ceo@co.test")); assert.equal(users.length, 6);});