Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

60.4 KB
Raw Blame
/**
 * Sign-in data in SQLite (src/server/auth-store.ts): a new install, an older install's auth.json moving in (once, in one
 * transaction), the bridge that keeps auth.json in step for a server of an older build, putting it aside, crashes, a
 * second server starting at the same moment, a file that can't be read or can't go in, and 100,000 accounts.
 * Synthetic data only. Each "server" is the auth modules loaded afresh on a data folder (a restart).
 */
import { spawn, spawnSync } from "node:child_process";
import crypto from "node:crypto";
import fs from "node:fs";
import path from "node:path";
import { pathToFileURL } from "node:url";
import { afterAll, afterEach, describe, expect, it, vi } from "vitest";
import { tempDir } from "./helpers/temp-dir";

delete process.env.FUOBE_PASSWORD;
delete process.env.FUOBE_PUBLIC_SIGNUP;
delete process.env.FUOBE_AUTH_BRIDGE_MS;

const DAY = 86_400_000;
const MIN = 60_000;
const sha = (s: string) => crypto.createHash("sha256").update(s).digest("hex");
const mac = (key: string, payload: string) => crypto.createHmac("sha256", key).update(payload).digest("base64url");
const hashPw = (pw: string) => {
  const salt = crypto.randomBytes(16);
  return `${salt.toString("hex")}:${crypto.scryptSync(pw, salt, 32).toString("hex")}`;
};

/** The auth modules loaded afresh on a data folder: a server (re)starting there. */
async function server(dir: string) {
  globalThis.__fuobeDb?.close();
  globalThis.__fuobeDb = undefined;
  globalThis.__fuobeAuthTablesError = undefined;
  globalThis.__fuobeSecretKey = undefined;
  globalThis.__fuobeAuthReimportTaken = undefined;
  process.env.FUOBE_DATA_DIR = dir;
  vi.resetModules();
  const auth = await import("@/server/auth");
  const store = await import("@/server/auth-store");
  const box = await import("@/server/secret-box");
  const keys = await import("@/server/account-keys");
  const db = () => globalThis.__fuobeDb!;
  const marker = () => db().prepare("SELECT * FROM auth_migration WHERE id = 1").get() as Record<string, unknown> | undefined;
  const count = (t: string) => (db().prepare(`SELECT COUNT(*) AS n FROM ${t}`).get() as { n: number }).n;
  return { auth, store, box, keys, db, marker, count };
}

afterEach(() => {
  globalThis.__fuobeAuthClock = undefined;
  globalThis.__fuobeAuthImportFault = undefined;
  delete process.env.FUOBE_AUTH_BRIDGE_MS;
});
afterAll(() => {
  globalThis.__fuobeDb?.close();
  globalThis.__fuobeDb = undefined;
});

const authFile = (dir: string) => path.join(dir, "auth.json");

/** What a server of an older build reads: the file, or "no file" (an open studio), or a file it can't read (fails closed). */
function legacyRead(dir: string): Record<string, unknown> & { unreadable?: true } {
  try {
    const d = JSON.parse(fs.readFileSync(authFile(dir), "utf8"));
    if (!d || typeof d !== "object" || Array.isArray(d)) throw new Error("not an object");
    return d;
  } catch (e) {
    if ((e as NodeJS.ErrnoException).code === "ENOENT") return { secret: "", tokens: [] };
    return { unreadable: true };
  }
}

/**
 * What a build from before 2026-10-31 (no fail-closed read yet) makes of auth.json: a file it can't parse means no
 * password (an open studio). The note must keep it locked: a password nobody can match, no secret, no tokens, no accounts.
 */
function veryOldLocked(dir: string): boolean {
  let d: { password?: string; secret?: string; tokens?: unknown[]; accounts?: unknown[] };
  try {
    d = JSON.parse(fs.readFileSync(authFile(dir), "utf8"));
  } catch {
    d = { secret: "", tokens: [] };
  }
  const verify = (pw: string) => {
    if (!d.password) return false;
    const [salt, hash] = d.password.split(":");
    if (!salt || !hash) return false;
    const a = crypto.scryptSync(pw, Buffer.from(salt, "hex"), 32);
    const b = Buffer.from(hash, "hex");
    return a.length === b.length && crypto.timingSafeEqual(a, b);
  };
  return !!d.password && !verify("") && !verify("owner password") && !d.secret && !d.tokens?.length && !d.accounts?.length;
}

/** The auth.json files set aside unread after the move (auth.json.stray-<UTC time>). */
const strays = (dir: string) =>
  fs
    .readdirSync(dir)
    .filter((f) => f.startsWith("auth.json.stray-"))
    .sort();

/** The note in place of auth.json: JSON, so every older build reads it, and locked for each of them. */
function isNote(dir: string): boolean {
  const d = JSON.parse(fs.readFileSync(authFile(dir), "utf8"));
  return d.fuobeSignInMovedTo === "fuobe.db" && legacyRead(dir).unreadable === true && veryOldLocked(dir);
}

/** scripts/auth-export.mjs on a data folder (the server stopped first). */
function exportScript(dir: string, env: Record<string, string> = {}) {
  globalThis.__fuobeDb?.close();
  globalThis.__fuobeDb = undefined;
  const r = spawnSync(process.execPath, [path.join(process.cwd(), "scripts", "auth-export.mjs")], {
    env: { ...process.env, FUOBE_DATA_DIR: dir, ...env },
    encoding: "utf8",
  });
  return { code: r.status, out: `${r.stdout}${r.stderr}` };
}

/** A server of an older build writing auth.json (a temporary file, then a rename). */
function legacyWrite(dir: string, data: unknown): void {
  const tmp = `${authFile(dir)}.old.tmp`;
  fs.writeFileSync(tmp, JSON.stringify(data, null, 2), { mode: 0o600 });
  fs.renameSync(tmp, authFile(dir));
}

/** An auth.json as an older install has it, every optional field used, and cookies and tokens made before the move. */
function fixture(seal: (plain: string, context: string) => string) {
  const now = Date.now();
  const secret = crypto.randomBytes(32).toString("hex");
  const owner = hashPw("owner password");
  const ctx = (id: string, p: string) => `fuobe:account-key:v1:${id}:${p}`;
  const accounts = [
    {
      id: "acct00000000000001",
      email: "[email protected]",
      hash: hashPw("first password"),
      createdAt: now - 9 * DAY,
      lastLoginAt: now - DAY,
      gpu: { mode: "link", url: "https://gpu-a.example.com", token: "gpu-link-token-a" },
      keys: {
        fal: { sealed: seal("fal-key-first:ab12", ctx("acct00000000000001", "fal")), last4: "ab12", addedAt: now - DAY },
        replicate: { sealed: seal("r8_replicate_first_cd34", ctx("acct00000000000001", "replicate")), last4: "cd34", addedAt: now - DAY, rejectedAt: now - 1000 },
      },
      futureField: { kept: true },
    },
    {
      id: "acct00000000000002",
      email: "[email protected]",
      hash: hashPw("second password"),
      createdAt: now - 5 * DAY,
      passwordChangedAt: now - 2 * DAY,
      disabled: false,
      sessionsAfter: now - 3 * DAY,
      gpu: { url: "https://gpu-b.example.com" },
    },
    { id: "acct00000000000003", email: "[email protected]", hash: hashPw("third password"), createdAt: now - DAY, disabled: true, gpu: { mode: "computer" } },
    { id: "acct00000000000004", email: "[email protected]", hash: hashPw("fourth password"), createdAt: now - 1000, keys: {} },
  ];
  const tokenValue = () => `fuobe_${crypto.randomBytes(24).toString("base64url")}`;
  const tokens = [tokenValue(), tokenValue(), tokenValue()];
  const exp = now + 29 * DAY;
  const ownerCookie = `${exp}.${mac(`${secret}.${sha(owner)}`, String(exp))}`;
  const signedOutCookie = `${exp - 1}.${mac(`${secret}.${sha(owner)}`, String(exp - 1))}`;
  const accountCookie = (a: (typeof accounts)[number]) => `${exp}.${a.id}.${mac(`${secret}.${sha(a.hash)}`, `${exp}.${a.id}`)}`;
  const legacyCookie = (a: (typeof accounts)[number]) => `${exp}.${a.id}.${mac(secret, `${exp}.${a.id}`)}`;
  const data = {
    password: owner,
    secret,
    tokens: [
      { id: "tok1", name: "CI", hash: sha(tokens[0]), prefix: tokens[0].slice(0, 12), createdAt: now - DAY, lastUsedAt: now - 2 * 3_600_000 },
      { id: "tok2", name: "Laptop", hash: sha(tokens[1]), prefix: tokens[1].slice(0, 12), createdAt: now - DAY },
      { id: "tok3", name: "MCP", hash: sha(tokens[2]), prefix: tokens[2].slice(0, 12), createdAt: now - 1000 },
    ],
    accounts,
    publicSignup: true,
    signedOut: [
      { hash: sha(signedOutCookie), exp: exp - 1 },
      { hash: sha("an expired cookie"), exp: now - 1000 },
    ],
    laterSetting: { kept: "as it was" },
  };
  return {
    data,
    tokens,
    cookies: {
      owner: ownerCookie,
      signedOut: signedOutCookie,
      first: accountCookie(accounts[0]),
      second: accountCookie(accounts[1]),
      third: accountCookie(accounts[2]),
      firstLegacy: legacyCookie(accounts[0]),
      secondLegacy: legacyCookie(accounts[1]),
    },
  };
}

/** A data folder with an older install's auth.json (and the server key its account keys were sealed with). */
async function olderInstall(prefix: string) {
  const dir = tempDir(prefix);
  const srv = await server(dir);
  const fx = fixture(srv.box.seal);
  fs.writeFileSync(authFile(dir), JSON.stringify(fx.data, null, 2), { mode: 0o600 });
  return { dir, srv, fx, original: fs.readFileSync(authFile(dir), "utf8") };
}

describe("a new install", () => {
  it("starts open with nothing stored; the first change sets the store up and leaves a note no older build can read", async () => {
    const dir = tempDir("fuobe-auth-new-");
    let srv = await server(dir);
    expect(srv.auth.authEnabled()).toBe(false);
    expect(srv.store.authStoreMode()).toBe("fresh");
    expect(srv.auth.listAccounts()).toEqual([]);
    expect(srv.marker()).toBeUndefined();
    expect(fs.existsSync(authFile(dir))).toBe(false);
    srv.auth.setPassword("owner password");
    expect(srv.marker()).toMatchObject({ source: "new", state: "done" });
    expect(isNote(dir)).toBe(true);
    // An older build pointed at this folder fails closed instead of opening the studio.
    expect(legacyRead(dir).unreadable).toBe(true);
    srv = await server(dir);
    expect(srv.auth.authEnabled()).toBe(true);
    expect(srv.auth.checkPassword("owner password")).toBe(true);
    expect(srv.store.authStoreMode()).toBe("done");
  });

  it("moves in an auth.json put there before the first change; one put back after keeps serving, is set aside unread, and the owner is told", async () => {
    const dir = tempDir("fuobe-auth-drop-");
    let srv = await server(dir);
    expect(srv.auth.authEnabled()).toBe(false);
    legacyWrite(dir, { password: hashPw("dropped password"), secret: "a".repeat(64), tokens: [] });
    expect(srv.auth.authEnabled()).toBe(true);
    expect(srv.auth.checkPassword("dropped password")).toBe(true);
    expect(srv.marker()).toMatchObject({ source: "auth.json", state: "bridge" });
    // Put aside, then an auth.json without a password put back (read, it would open the studio).
    globalThis.__fuobeAuthClock = () => Date.now() + 11 * MIN;
    expect(srv.store.authStoreMode()).toBe("done");
    const putBack = JSON.stringify({ secret: "", tokens: [] }, null, 2);
    legacyWrite(dir, { secret: "", tokens: [] });
    const errors = vi.spyOn(console, "error").mockImplementation(() => {});
    const warns = vi.spyOn(console, "warn").mockImplementation(() => {});
    srv = await server(dir);
    // The site keeps serving, from the database: sign-in on, the password as it was.
    expect(srv.store.authStoreMode()).toBe("done");
    expect(srv.store.authStoreFailed()).toBe(false);
    expect(srv.auth.authEnabled()).toBe(true);
    expect(srv.auth.checkPassword("dropped password")).toBe(true);
    expect(srv.auth.readAccountIds().size).toBe(0);
    // Set aside, unread and as it was, readable by the server's user only; the note back in place of auth.json.
    const stray = strays(dir);
    expect(stray).toHaveLength(1);
    expect(fs.readFileSync(path.join(dir, stray[0]), "utf8")).toBe(putBack);
    if (process.platform !== "win32") expect(fs.statSync(path.join(dir, stray[0])).mode & 0o777).toBe(0o600);
    expect(isNote(dir)).toBe(true);
    // The owner is told: the file's name and its counts, nothing from inside it.
    expect(srv.store.signInNotices()).toEqual([{ file: stray[0], foundAt: expect.any(Number), reason: "stray", counts: { accounts: 0, tokens: 0, accountsNotInDatabase: 0 } }]);
    expect(warns.mock.calls.flat().join("\n")).toContain(stray[0]);
    expect(errors).not.toHaveBeenCalled();
    vi.restoreAllMocks();
    // Visitors see the site, not an error: /login answers and a page asks for sign-in.
    const { proxy } = await import("@/proxy");
    const { NextRequest } = await import("next/server");
    expect(proxy(new NextRequest("http://localhost:3000/login")).status).toBe(200);
    expect(proxy(new NextRequest("http://localhost:3000/")).status).toBe(307);
    // Another start: nothing new to set aside, the notice stays while the file is there.
    srv = await server(dir);
    expect(srv.store.authStoreMode()).toBe("done");
    expect(strays(dir)).toEqual(stray);
    expect(srv.store.signInNotices()).toHaveLength(1);
    // The owner deletes the file: the notice goes.
    fs.rmSync(path.join(dir, stray[0]));
    expect(srv.store.signInNotices()).toEqual([]);
    expect(srv.auth.checkPassword("dropped password")).toBe(true);
  });
});

describe("an older install's auth.json moves in", () => {
  it("whole: every account, token and setting reads back the same; cookies, tokens, passwords and sealed keys made before still work", async () => {
    const { dir, srv, fx, original } = await olderInstall("fuobe-auth-import-");
    const { auth, store, keys } = srv;
    expect(store.authStoreMode()).toBe("bridge");
    expect(srv.marker()).toMatchObject({ source: "auth.json", state: "bridge" });
    expect(JSON.parse(srv.marker()!.counts_json as string)).toEqual({ accounts: 4, tokens: 3, keys: 2, gpus: 3, signedOut: 1 });
    expect([srv.count("auth_accounts"), srv.count("auth_tokens"), srv.count("auth_account_keys"), srv.count("auth_account_gpu"), srv.count("auth_signed_out")]).toEqual([4, 3, 2, 3, 1]);
    // Moving in doesn't touch the file.
    expect(fs.readFileSync(authFile(dir), "utf8")).toBe(original);

    // Every account as it was (the "disabled: false" and empty keys of the file mean the same as leaving them out).
    const [first, second, third, fourth] = fx.data.accounts;
    expect(auth.accountById(first.id)).toEqual(first);
    const without = (o: object, key: string) => Object.fromEntries(Object.entries(o).filter(([k]) => k !== key));
    expect(auth.accountById(second.id)).toEqual(without(second, "disabled"));
    expect(auth.accountById(third.id)).toEqual(third);
    expect(auth.accountById(fourth.id)).toEqual(without(fourth, "keys"));
    const out = store.exportJson();
    expect(out.laterSetting).toEqual({ kept: "as it was" });
    expect(out.tokens).toEqual(fx.data.tokens);
    expect(out.signedOut).toEqual([fx.data.signedOut[0]]);
    expect(out.publicSignup).toBe(true);
    expect(auth.listAccounts().map((a) => a.email)).toEqual(["[email protected]", "[email protected]", "[email protected]", "[email protected]"]);
    expect(auth.listAccounts()[3]).toMatchObject({ gpu: true, keys: ["fal", "replicate"], disabled: false });

    // Sign-in made before the move.
    expect(auth.authEnabled()).toBe(true);
    expect(auth.publicSignupSetting()).toBe(true);
    expect(auth.checkPassword("owner password")).toBe(true);
    expect(auth.validSession(fx.cookies.owner)).toBe(true);
    expect(auth.sessionViewer(fx.cookies.signedOut)).toBeNull();
    expect(auth.sessionViewer(fx.cookies.first)).toMatchObject({ kind: "account", account: { email: "[email protected]" } });
    expect(auth.sessionViewer(fx.cookies.second)).toMatchObject({ kind: "account", account: { email: "[email protected]" } });
    expect(auth.sessionViewer(fx.cookies.third)).toBeNull(); // switched off
    expect(auth.sessionViewer(fx.cookies.firstLegacy)).toMatchObject({ kind: "account" });
    expect(auth.sessionViewer(fx.cookies.secondLegacy)).toBeNull(); // changed its password since
    for (const t of fx.tokens) expect(auth.validToken(`Bearer ${t}`)).toBe(true);
    expect(auth.checkAccount("[email protected]", "first password")?.id).toBe(first.id);
    await expect(auth.verifyAccount("[email protected]", "second password")).resolves.toMatchObject({ id: second.id });
    expect(auth.checkAccount("[email protected]", "second password")).toBeNull();
    // Sealed keys open with the same server key: nothing was sealed again.
    expect(keys.accountKey(first.id, "fal")).toBe("fal-key-first:ab12");
    expect(keys.accountKey(first.id, "replicate")).toBe("r8_replicate_first_cd34");
    expect(auth.accountKeyRecord(first.id, "fal")?.sealed).toBe(first.keys!.fal!.sealed);
    expect(auth.accountGpu(first.id)).toEqual(first.gpu);
    expect(auth.accountGpu(second.id)).toEqual({ url: "https://gpu-b.example.com" });
    expect(auth.accountGpu(third.id)).toBeNull(); // switched off

    // A restart moves nothing in again.
    const startedAt = srv.marker()!.started_at;
    const again = await server(dir);
    expect(again.auth.listAccounts()).toHaveLength(4);
    expect(again.marker()!.started_at).toBe(startedAt);
    expect(again.count("auth_accounts")).toBe(4);
    expect(again.auth.validSession(fx.cookies.owner)).toBe(true);
  });

  it("every sign-in path works on the moved data", async () => {
    const { srv, fx } = await olderInstall("fuobe-auth-paths-");
    const { auth } = srv;
    const [first, second] = fx.data.accounts;
    // A new password for an account ends its other sign-ins.
    const changed = await auth.setAccountPassword(first.id, "first password", "first new password");
    expect(auth.sessionViewer(fx.cookies.first)).toBeNull();
    expect(auth.sessionViewer(fx.cookies.firstLegacy)).toBeNull();
    expect(auth.sessionViewer(auth.createSession(first.id).value)).toMatchObject({ kind: "account" });
    expect(changed.passwordChangedAt).toBeGreaterThan(0);
    await expect(auth.setAccountPassword(first.id, "wrong", "whatever long")).rejects.toMatchObject({ status: 403 });
    await expect(auth.setAccountPassword("nobody", "x", "whatever long")).rejects.toMatchObject({ status: 404 });
    // Owner: switch off, sign out everywhere, delete.
    auth.setAccountDisabled(second.id, true);
    expect(auth.sessionViewer(fx.cookies.second)).toBeNull();
    auth.setAccountDisabled(second.id, false);
    expect(auth.sessionViewer(fx.cookies.second)).toMatchObject({ kind: "account" });
    auth.signOutAccount(second.id);
    expect(auth.sessionViewer(fx.cookies.second)).toBeNull();
    expect(() => auth.setAccountDisabled("nobody", true)).toThrow(/No such account/);
    expect(() => auth.signOutAccount("nobody")).toThrow(/No such account/);
    expect(() => auth.setAccountGpu("nobody", null)).toThrow(/No such account/);
    expect(() => auth.setAccountKey("nobody", "fal", null)).toThrow(/No such account/);
    auth.deleteAccount(first.id);
    expect(auth.accountById(first.id)).toBeUndefined();
    expect(auth.accountKeyRecord(first.id, "fal")).toBeNull();
    expect(srv.db().prepare("SELECT COUNT(*) AS n FROM auth_account_keys WHERE account_id = ?").get(first.id)).toEqual({ n: 0 });
    expect(srv.db().prepare("SELECT COUNT(*) AS n FROM auth_account_gpu WHERE account_id = ?").get(first.id)).toEqual({ n: 0 });
    expect(() => auth.deleteAccount(first.id)).toThrow(/No such account/);
    // Sign-up: one account per email, whatever its case.
    const made = auth.createAccount("[email protected]", "long enough");
    expect(() => auth.createAccount("[email protected]", "long enough")).toThrow(expect.objectContaining({ status: 409 }));
    expect(auth.checkAccount("[email protected]", "long enough")?.id).toBe(made.id);
    // Sign out (this cookie only), then everyone (with tokens).
    const owner = auth.createSession().value;
    await new Promise((r) => setTimeout(r, 5)); // another expiry: another cookie
    const other = auth.createSession().value;
    auth.endSession(owner);
    expect(auth.validSession(owner)).toBe(false);
    expect(auth.validSession(other)).toBe(true);
    const v = auth.authVersion();
    auth.signOutEveryone({ tokens: true });
    expect(auth.authVersion()).not.toBe(v);
    expect(auth.validSession(other)).toBe(false);
    expect(auth.validSession(fx.cookies.owner)).toBe(false);
    for (const t of fx.tokens) expect(auth.validToken(`Bearer ${t}`)).toBe(false);
    expect(auth.authStatus().tokens).toEqual([]);
    // Tokens: made, used, revoked.
    const { token, info } = auth.createToken("Script");
    expect(auth.validToken(`Bearer ${token}`)).toBe(true);
    expect(auth.authStatus().tokens).toEqual([expect.objectContaining({ id: info.id, name: "Script", lastUsedAt: expect.any(Number) })]);
    auth.revokeToken(info.id);
    expect(auth.validToken(`Bearer ${token}`)).toBe(false);
    // The public sign-up switch, and the Settings password.
    auth.setPublicSignup(false);
    expect(auth.publicSignupSetting()).toBe(false);
    auth.setPassword(null);
    expect(auth.authEnabled()).toBe(false);
    auth.setPassword("owner password two");
    expect(auth.checkPassword("owner password two")).toBe(true);
    // The sweep's account list.
    expect(auth.readAccountIds()).toEqual(new Set(auth.listAccounts().map((a) => a.id)));
    expect(auth.accountsCsv().split("\r\n")[0]).toBe("email,created,last_login,gpu_connected,fal_key,replicate_key,disabled");
  });
});

describe("the bridge: auth.json kept in step for a server of an older build", () => {
  it("writes each change to auth.json in its usual format, and reads back what an older server writes there", async () => {
    const { dir, srv, fx } = await olderInstall("fuobe-auth-bridge-");
    const { auth } = srv;
    expect(srv.store.authStoreMode()).toBe("bridge");
    // A change here: the older server sees it.
    const made = auth.createAccount("[email protected]", "long enough");
    const text = fs.readFileSync(authFile(dir), "utf8");
    expect(text).toBe(JSON.stringify(srv.store.exportJson(), null, 2));
    const seen = legacyRead(dir) as { accounts: { id: string; email: string }[]; password: string; secret: string };
    expect(seen.accounts.map((a) => a.email)).toContain("[email protected]");
    expect(seen.password).toBe(fx.data.password);
    expect(seen.secret).toBe(fx.data.secret);
    if (process.platform !== "win32") expect(fs.statSync(authFile(dir)).mode & 0o777).toBe(0o600);

    // The older server signs someone up, revokes a token and signs a cookie out: seen here on the next call.
    const older = legacyRead(dir) as typeof fx.data & { accounts: Record<string, unknown>[] };
    older.accounts.push({ id: "acctolder000000001", email: "[email protected]", hash: hashPw("older password"), createdAt: Date.now(), lastLoginAt: Date.now() });
    older.tokens = older.tokens.filter((t) => t.id !== "tok2");
    older.signedOut = [...(older.signedOut ?? []), { hash: sha(fx.cookies.first), exp: Date.now() + DAY }];
    legacyWrite(dir, older);
    expect(auth.checkAccount("[email protected]", "older password")?.id).toBe("acctolder000000001");
    expect(auth.validToken(`Bearer ${fx.tokens[1]}`)).toBe(false);
    expect(auth.validToken(`Bearer ${fx.tokens[0]}`)).toBe(true);
    expect(auth.sessionViewer(fx.cookies.first)).toBeNull();
    expect(auth.accountById(made.id)?.email).toBe("[email protected]");
    // Then a change here keeps both.
    auth.setAccountDisabled(made.id, true);
    const after = legacyRead(dir) as { accounts: { id: string; disabled?: boolean }[] };
    expect(after.accounts.find((a) => a.id === made.id)?.disabled).toBe(true);
    expect(after.accounts.some((a) => a.id === "acctolder000000001")).toBe(true);

    // auth.json removed: written again (an older server would otherwise see no password).
    fs.rmSync(authFile(dir));
    expect(auth.authEnabled()).toBe(true);
    expect((legacyRead(dir) as { password?: string }).password).toBe(fx.data.password);

    // An older server's file that can't be read back in (a broken write) leaves the database as it was.
    fs.writeFileSync(authFile(dir), "{ broken");
    expect(auth.checkAccount("[email protected]", "older password")?.id).toBe("acctolder000000001");
    auth.setPublicSignup(false);
    expect((legacyRead(dir) as { publicSignup?: boolean }).publicSignup).toBeUndefined();
    expect(legacyRead(dir).unreadable).toBeUndefined();
  });

  it("is put aside after 10 minutes once quiet for 3: kept as auth.json.migrated-<time>, with a note no older build can read", async () => {
    const { dir, srv, fx } = await olderInstall("fuobe-auth-retire-");
    const t0 = Date.now();
    expect(srv.store.authStoreMode()).toBe("bridge");
    // An older server still writing at minute 9: not put aside at minute 11, but at minute 12.5.
    globalThis.__fuobeAuthClock = () => t0 + 9 * MIN;
    legacyWrite(dir, { ...legacyRead(dir), publicSignup: false });
    expect(srv.store.authStoreMode()).toBe("bridge");
    globalThis.__fuobeAuthClock = () => t0 + 11 * MIN;
    expect(srv.store.authStoreMode()).toBe("bridge");
    expect(srv.auth.publicSignupSetting()).toBe(false);
    const last = fs.readFileSync(authFile(dir), "utf8");
    globalThis.__fuobeAuthClock = () => t0 + 12.5 * MIN;
    expect(srv.store.authStoreMode()).toBe("done");
    const m = srv.marker()!;
    expect(m).toMatchObject({ state: "done", backup_file: expect.stringMatching(/^auth\.json\.migrated-\d{8}T\d{6}Z$/) });
    const backup = path.join(dir, m.backup_file as string);
    expect(fs.readFileSync(backup, "utf8")).toBe(last);
    if (process.platform !== "win32") {
      expect(fs.statSync(backup).mode & 0o777).toBe(0o600);
      expect(fs.statSync(authFile(dir)).mode & 0o777).toBe(0o600);
    }
    expect(isNote(dir)).toBe(true);
    // Changes no longer touch auth.json; a restart moves nothing in.
    srv.auth.createAccount("[email protected]", "long enough");
    expect(legacyRead(dir).unreadable).toBe(true);
    const again = await server(dir);
    expect(again.store.authStoreMode()).toBe("done");
    expect(again.auth.validSession(fx.cookies.owner)).toBe(true);
    expect(again.auth.listAccounts()).toHaveLength(5);
    expect(fs.readdirSync(dir).filter((f) => f.startsWith("auth.json.migrated-"))).toHaveLength(1);
  });

  it("finishes putting auth.json aside after a crash between the database's change and the rename", async () => {
    const { dir, srv, original } = await olderInstall("fuobe-auth-retire-crash-");
    expect(srv.store.authStoreMode()).toBe("bridge");
    srv.db().prepare("UPDATE auth_migration SET state = 'done', backup_file = 'auth.json.migrated-20261104T000000Z', retired_at = ? WHERE id = 1").run(Date.now());
    const again = await server(dir);
    expect(again.store.authStoreMode()).toBe("done");
    expect(fs.readFileSync(path.join(dir, "auth.json.migrated-20261104T000000Z"), "utf8")).toBe(original);
    expect(isNote(dir)).toBe(true);
    // The note gone too: written again.
    fs.rmSync(authFile(dir));
    const third = await server(dir);
    expect(third.store.authStoreMode()).toBe("done");
    expect(legacyRead(dir).unreadable).toBe(true);
  });
});

describe("going back to an older build, and forward again", () => {
  /** An older install moved in and put aside, then an account made on this build. */
  async function moved(prefix: string) {
    const o = await olderInstall(prefix);
    expect(o.srv.store.authStoreMode()).toBe("bridge");
    globalThis.__fuobeAuthClock = () => Date.now() + 11 * MIN;
    expect(o.srv.store.authStoreMode()).toBe("done");
    globalThis.__fuobeAuthClock = undefined;
    const after = o.srv.auth.createAccount("[email protected]", "long enough");
    return { ...o, after };
  }

  it("scripts/auth-export.mjs writes auth.json for the older build; this build moves its changes back in", async () => {
    const { dir, fx, after } = await moved("fuobe-auth-export-");
    const backups = () => fs.readdirSync(dir).filter((f) => f.startsWith("auth.json.migrated-"));
    expect(backups()).toHaveLength(1);
    const r = exportScript(dir);
    expect(r.code, r.out).toBe(0);
    expect(r.out).not.toContain("@example.com");
    if (process.platform !== "win32") expect(fs.statSync(authFile(dir)).mode & 0o777).toBe(0o600);
    // The older build reads every account, the one made after the move too, and the password.
    const older = legacyRead(dir) as typeof fx.data & { accounts: Record<string, unknown>[] };
    expect(legacyRead(dir).unreadable).toBeUndefined();
    expect(older.accounts.map((a) => a.email)).toContain("[email protected]");
    expect(older.password).toBe(fx.data.password);
    // A second export would write over what the older build changed: refused.
    expect(exportScript(dir).code).not.toBe(0);
    // The older build signs someone up and changes the owner's password.
    older.accounts.push({ id: "acctrollback00001", email: "[email protected]", hash: hashPw("rollback password"), createdAt: Date.now() });
    older.password = hashPw("owner password two");
    legacyWrite(dir, older);
    // Forward again: moved back in, with the older build's changes, and bridged again.
    const srv = await server(dir);
    expect(srv.store.authStoreMode()).toBe("bridge");
    expect(srv.auth.checkAccount("[email protected]", "rollback password")?.id).toBe("acctrollback00001");
    expect(srv.auth.checkAccount("[email protected]", "long enough")?.id).toBe(after.id);
    expect(srv.auth.checkPassword("owner password two")).toBe(true);
    expect(srv.auth.checkPassword("owner password")).toBe(false);
    expect(srv.auth.readAccountIds().has("acctrollback00001")).toBe(true);
    expect(srv.marker()).toMatchObject({ state: "bridge", exported_version: null });
    globalThis.__fuobeAuthClock = () => Date.now() + 11 * MIN;
    expect(srv.store.authStoreMode()).toBe("done");
    expect(backups()).toHaveLength(2);
    expect(isNote(dir)).toBe(true);
    // The export and its way back are never taken for a stray file.
    globalThis.__fuobeAuthClock = () => Date.now() + 13 * MIN;
    expect(srv.store.authStoreMode()).toBe("done");
    expect(strays(dir)).toEqual([]);
    expect(srv.store.signInNotices()).toEqual([]);
  });

  it("an export the older build never used: back to the database as it was", async () => {
    const { dir } = await moved("fuobe-auth-export-unused-");
    expect(exportScript(dir).code).toBe(0);
    fs.rmSync(authFile(dir));
    const srv = await server(dir);
    expect(srv.store.authStoreMode()).toBe("done");
    expect(srv.marker()).toMatchObject({ state: "done" });
    expect(srv.auth.listAccounts()).toHaveLength(5);
    expect(isNote(dir)).toBe(true);
  });

  it("an export after which the database changed: fails closed until FUOBE_AUTH_REIMPORT=1", async () => {
    const { dir } = await moved("fuobe-auth-export-stale-");
    expect(exportScript(dir).code).toBe(0);
    let srv = await server(dir);
    // A change in the database after the export (the auth store not started: it would move the file in first).
    (await import("@/server/db")).db().prepare("UPDATE auth_install SET version = version + 1").run();
    srv = await server(dir);
    const errors = vi.spyOn(console, "error").mockImplementation(() => {});
    expect(srv.store.authStoreMode()).toBe("failed");
    errors.mockRestore();
    process.env.FUOBE_AUTH_REIMPORT = "1";
    try {
      srv = await server(dir);
      expect(srv.store.authStoreMode()).toBe("bridge");
      expect(srv.auth.listAccounts()).toHaveLength(5);
    } finally {
      delete process.env.FUOBE_AUTH_REIMPORT;
    }
  });

  it("the export script refuses an auth.json that isn't the note, and a store still in its bridge", async () => {
    const { dir, srv } = await olderInstall("fuobe-auth-export-refuse-");
    expect(srv.store.authStoreMode()).toBe("bridge");
    expect(exportScript(dir).out).toMatch(/kept up to date/);
    const { dir: dir2 } = await moved("fuobe-auth-export-refuse2-");
    legacyWrite(dir2, { secret: "", tokens: [] });
    const r = exportScript(dir2);
    expect(r.code).not.toBe(0);
    expect(r.out).toMatch(/move it away first/);
    expect(legacyRead(dir2)).toEqual({ secret: "", tokens: [] });
  });

  it("a backup copied back over auth.json: unchanged it is put aside again; changed by an older build it is set aside, the site keeps serving, and nothing is swept", async () => {
    const { dir, after } = await moved("fuobe-auth-putback-");
    let srv = await server(dir);
    expect(srv.store.authStoreMode()).toBe("done");
    const backup = path.join(dir, srv.marker()!.backup_file as string);
    // Copied back and not changed: holds nothing new, so the note goes back in its place.
    fs.copyFileSync(backup, authFile(dir));
    srv = await server(dir);
    expect(srv.store.authStoreMode()).toBe("done");
    expect(isNote(dir)).toBe(true);
    expect(srv.auth.accountById(after.id)).toBeDefined();
    expect(strays(dir)).toEqual([]);
    // Copied back, and an older build signed someone up there (and that account made a file here).
    const old = JSON.parse(fs.readFileSync(backup, "utf8"));
    old.accounts.push({ id: "acctputback000001", email: "[email protected]", hash: hashPw("putback password"), createdAt: Date.now() });
    legacyWrite(dir, old);
    vi.spyOn(console, "warn").mockImplementation(() => {});
    srv = await server(dir);
    expect(srv.store.authStoreMode()).toBe("done");
    expect(srv.store.authStoreFailed()).toBe(false);
    // The database's accounts go on; the file's new account isn't one.
    expect(srv.auth.accountById(after.id)).toBeDefined();
    expect(srv.auth.checkAccount("[email protected]", "long enough")?.id).toBe(after.id);
    expect(srv.auth.checkAccount("[email protected]", "putback password")).toBeNull();
    expect(strays(dir)).toHaveLength(1);
    expect(isNote(dir)).toBe(true);
    expect(srv.store.signInNotices()[0].counts).toEqual({ accounts: 5, tokens: 3, accountsNotInDatabase: 1 });
    // The sweep keeps the files of the account only that file lists, while it is there.
    const { saveFile, getAsset } = await import("@/server/storage");
    const { sweepAccountFiles } = await import("@/server/account-sweep");
    const png = Buffer.from("89504e470d0a1a0a0000000d4948445200000001000000010806000000", "hex");
    const theirs = saveFile({ data: png, contentType: "image/png", source: "upload", accountId: "acctputback000001" });
    expect((await sweepAccountFiles()).accounts).toBe(0);
    expect(getAsset(theirs.id)).toBeDefined();
    // A running server notices a file put back within a minute (and sets it aside under a name of its own).
    legacyWrite(dir, old);
    expect(srv.store.authStoreMode()).toBe("done");
    expect(strays(dir)).toHaveLength(1);
    globalThis.__fuobeAuthClock = () => Date.now() + 61_000;
    expect(srv.store.authStoreMode()).toBe("done");
    expect(strays(dir)).toHaveLength(2);
    expect(isNote(dir)).toBe(true);
    vi.restoreAllMocks();
    globalThis.__fuobeAuthClock = undefined;
    // Moved in when asked: the owner stops the server, renames the file back and starts once with FUOBE_AUTH_REIMPORT=1.
    fs.renameSync(path.join(dir, strays(dir)[0]), authFile(dir));
    process.env.FUOBE_AUTH_REIMPORT = "1";
    try {
      srv = await server(dir);
      expect(srv.store.authStoreMode()).toBe("bridge");
      expect(srv.auth.checkAccount("[email protected]", "putback password")?.id).toBe("acctputback000001");
      // The backup was taken before that account existed: only what the file holds comes back.
      expect(srv.auth.accountById(after.id)).toBeUndefined();
    } finally {
      delete process.env.FUOBE_AUTH_REIMPORT;
    }
    // Once the set-aside files are gone, the sweep may remove files of accounts the database doesn't have.
    for (const f of strays(dir)) fs.rmSync(path.join(dir, f));
    expect(srv.store.signInNotices()).toEqual([]);
  });
});

describe("an auth.json that turns up after the move", () => {
  /** An older install moved in and put aside: the database is the store. */
  async function settled(prefix: string) {
    const o = await olderInstall(prefix);
    expect(o.srv.store.authStoreMode()).toBe("bridge");
    globalThis.__fuobeAuthClock = () => Date.now() + 11 * MIN;
    expect(o.srv.store.authStoreMode()).toBe("done");
    globalThis.__fuobeAuthClock = undefined;
    return o;
  }

  it("with another password, secret, tokens and accounts: none of it takes effect, and nothing of it is logged", async () => {
    const { dir, fx } = await settled("fuobe-auth-stray-");
    const secret = crypto.randomBytes(32).toString("hex");
    const token = `fuobe_${crypto.randomBytes(24).toString("base64url")}`;
    const intruder = { id: "acctintruder00001", email: "[email protected]", hash: hashPw("intruder password"), createdAt: Date.now() };
    const stray = {
      password: hashPw("stray password"),
      secret,
      tokens: [{ id: "tokx", name: "x", hash: sha(token), prefix: token.slice(0, 12), createdAt: Date.now() }],
      accounts: [intruder],
      publicSignup: false,
    };
    legacyWrite(dir, stray);
    const logs = [vi.spyOn(console, "warn").mockImplementation(() => {}), vi.spyOn(console, "error").mockImplementation(() => {}), vi.spyOn(console, "info").mockImplementation(() => {})];
    const srv = await server(dir);
    expect(srv.store.authStoreMode()).toBe("done");
    // The database's password, cookies, tokens and accounts work as before.
    expect(srv.auth.checkPassword("owner password")).toBe(true);
    expect(srv.auth.validSession(fx.cookies.owner)).toBe(true);
    expect(srv.auth.validToken(`Bearer ${fx.tokens[0]}`)).toBe(true);
    expect(srv.auth.checkAccount("[email protected]", "first password")?.id).toBe("acct00000000000001");
    expect(srv.auth.publicSignupSetting()).toBe(true);
    // The file's never do: its password, its token, its account, a cookie signed with its secret.
    expect(srv.auth.checkPassword("stray password")).toBe(false);
    expect(srv.auth.validToken(`Bearer ${token}`)).toBe(false);
    expect(srv.auth.checkAccount("[email protected]", "intruder password")).toBeNull();
    const exp = Date.now() + DAY;
    expect(srv.auth.validSession(`${exp}.${mac(`${secret}.${sha(stray.password)}`, String(exp))}`)).toBe(false);
    expect(srv.auth.validSession(`${exp}.${intruder.id}.${mac(`${secret}.${sha(intruder.hash)}`, `${exp}.${intruder.id}`)}`)).toBe(false);
    expect((srv.db().prepare("SELECT secret, password_hash FROM auth_install").get() as { secret: string; password_hash: string })).toEqual({
      secret: fx.data.secret,
      password_hash: fx.data.password,
    });
    expect(srv.auth.readAccountIds().has(intruder.id)).toBe(false);
    // An older build pointed at this folder reads the note again, and stays locked.
    expect(isNote(dir)).toBe(true);
    expect(veryOldLocked(dir)).toBe(true);
    // Counts only, in the log and the notice.
    expect(srv.store.signInNotices()[0].counts).toEqual({ accounts: 1, tokens: 1, accountsNotInDatabase: 1 });
    const logged = logs.flatMap((l) => l.mock.calls.flat()).join("\n");
    for (const s of [intruder.email, intruder.hash, secret, stray.password, sha(token), ...fx.data.accounts.map((a) => a.email)]) expect(logged).not.toContain(s);
    expect(JSON.stringify(srv.store.signInNotices())).not.toContain("intruder");
    vi.restoreAllMocks();
  });

  it("a file that isn't sign-in data, and FUOBE_AUTH_REIMPORT=1 with data that can't go in: set aside, and the site keeps serving", async () => {
    const { dir, fx } = await settled("fuobe-auth-stray-bad-");
    vi.spyOn(console, "warn").mockImplementation(() => {});
    vi.spyOn(console, "error").mockImplementation(() => {});
    fs.writeFileSync(authFile(dir), "{ not json");
    let srv = await server(dir);
    expect(srv.store.authStoreMode()).toBe("done");
    expect(srv.auth.validSession(fx.cookies.owner)).toBe(true);
    expect(isNote(dir)).toBe(true);
    expect(srv.store.signInNotices()).toMatchObject([{ reason: "unreadable", counts: null }]);
    const bad = structuredClone(fx.data);
    bad.accounts[1].email = bad.accounts[0].email;
    bad.password = hashPw("a password that must not win");
    legacyWrite(dir, bad);
    process.env.FUOBE_AUTH_REIMPORT = "1";
    try {
      srv = await server(dir);
      expect(srv.store.authStoreMode()).toBe("done");
    } finally {
      delete process.env.FUOBE_AUTH_REIMPORT;
    }
    expect(srv.store.authStoreFailed()).toBe(false);
    expect(srv.auth.checkPassword("owner password")).toBe(true);
    expect(srv.auth.checkPassword("a password that must not win")).toBe(false);
    expect(srv.auth.listAccounts()).toHaveLength(4);
    expect(isNote(dir)).toBe(true);
    expect(srv.store.signInNotices().map((n) => n.reason)).toEqual(["import-failed", "unreadable"]);
    vi.restoreAllMocks();
  });

  it("a file that turns up during a change: the change is saved in the database, and the file is set aside at the next look", async () => {
    const { dir } = await settled("fuobe-auth-stray-write-");
    const srv = await server(dir);
    expect(srv.store.authStoreMode()).toBe("done");
    vi.spyOn(console, "warn").mockImplementation(() => {});
    const made = srv.store.authWrite(() => {
      legacyWrite(dir, { password: hashPw("mid-write password"), secret: "b".repeat(64), tokens: [] });
      return srv.auth.normalizeEmail("[email protected]");
    });
    expect(made).toBe("[email protected]");
    srv.auth.setPassword("changed password");
    expect(srv.auth.checkPassword("changed password")).toBe(true);
    // Not read while it waits for the next look.
    expect(srv.auth.checkPassword("mid-write password")).toBe(false);
    globalThis.__fuobeAuthClock = () => Date.now() + 61_000;
    expect(srv.store.authStoreMode()).toBe("done");
    expect(strays(dir)).toHaveLength(1);
    expect(isNote(dir)).toBe(true);
    expect(srv.auth.checkPassword("changed password")).toBe(true);
    expect(srv.auth.checkPassword("mid-write password")).toBe(false);
    vi.restoreAllMocks();
  });

  it.skipIf(process.platform === "win32")("a link in place of auth.json: kept as a link, what it points to never read or changed", async () => {
    const { dir } = await settled("fuobe-auth-stray-link-");
    const elsewhere = path.join(tempDir("fuobe-auth-elsewhere-"), "other.json");
    fs.writeFileSync(elsewhere, JSON.stringify({ password: hashPw("linked password"), secret: "c".repeat(64), tokens: [] }), { mode: 0o644 });
    fs.rmSync(authFile(dir));
    fs.symlinkSync(elsewhere, authFile(dir));
    vi.spyOn(console, "warn").mockImplementation(() => {});
    const srv = await server(dir);
    expect(srv.store.authStoreMode()).toBe("done");
    expect(srv.auth.checkPassword("linked password")).toBe(false);
    expect(fs.lstatSync(authFile(dir)).isSymbolicLink()).toBe(false);
    expect(isNote(dir)).toBe(true);
    const [kept] = strays(dir);
    expect(fs.readlinkSync(path.join(dir, kept))).toBe(elsewhere);
    expect(fs.statSync(elsewhere).mode & 0o777).toBe(0o644);
    expect(srv.store.signInNotices()).toMatchObject([{ reason: "link", counts: null }]);
    vi.restoreAllMocks();
  });

  it.skipIf(process.platform === "win32")("a named pipe in place of auth.json: never opened, set aside, and the site keeps serving", async () => {
    const { dir } = await settled("fuobe-auth-stray-fifo-");
    fs.rmSync(authFile(dir));
    expect(spawnSync("mkfifo", [authFile(dir)]).status).toBe(0);
    vi.spyOn(console, "warn").mockImplementation(() => {});
    const srv = await server(dir);
    expect(srv.store.authStoreMode()).toBe("done");
    expect(srv.auth.checkPassword("owner password")).toBe(true);
    expect(isNote(dir)).toBe(true);
    expect(strays(dir)).toHaveLength(1);
    expect(srv.store.signInNotices()).toMatchObject([{ reason: "unreadable", counts: null }]);
    vi.restoreAllMocks();
  });

  it("a changed copy of the note (another password and secret): set aside, the note put back, and every older build stays locked", async () => {
    const { dir, fx } = await settled("fuobe-auth-stray-like-");
    const note = JSON.parse(fs.readFileSync(authFile(dir), "utf8"));
    const secret = "d".repeat(64);
    const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
    for (const like of [
      { ...note, password: hashPw("like password"), secret },
      { ...note, password: hashPw("like password"), secret, unreadable: undefined },
      { [Object.keys(note)[0]]: "x", password: hashPw("like password"), secret, tokens: [] },
    ]) {
      legacyWrite(dir, like);
      const srv = await server(dir);
      expect(srv.store.authStoreMode()).toBe("done");
      expect(srv.auth.checkPassword("like password")).toBe(false);
      expect(srv.auth.checkPassword("owner password")).toBe(true);
      expect((srv.db().prepare("SELECT secret FROM auth_install").get() as { secret: string }).secret).toBe(fx.data.secret);
      expect(isNote(dir)).toBe(true);
      expect(veryOldLocked(dir)).toBe(true);
      expect(srv.store.signInNotices()[0]).toMatchObject({ reason: "stray", counts: { accounts: 0, tokens: 0, accountsNotInDatabase: 0 } });
    }
    expect(strays(dir)).toHaveLength(3);
    expect(warn.mock.calls.flat().join("\n")).not.toContain(secret);
    vi.restoreAllMocks();
  });

  it("FUOBE_AUTH_REIMPORT=1 left set: it counts for the first look after a start only", async () => {
    const { dir } = await settled("fuobe-auth-stray-flag-");
    vi.spyOn(console, "warn").mockImplementation(() => {});
    process.env.FUOBE_AUTH_REIMPORT = "1";
    try {
      const srv = await server(dir);
      expect(srv.store.authStoreMode()).toBe("done");
      legacyWrite(dir, { password: hashPw("later password"), secret: "e".repeat(64), tokens: [] });
      globalThis.__fuobeAuthClock = () => Date.now() + 61_000;
      expect(srv.store.authStoreMode()).toBe("done");
      expect(srv.auth.checkPassword("later password")).toBe(false);
      expect(srv.auth.checkPassword("owner password")).toBe(true);
      expect(strays(dir)).toHaveLength(1);
      expect(isNote(dir)).toBe(true);
    } finally {
      delete process.env.FUOBE_AUTH_REIMPORT;
    }
    vi.restoreAllMocks();
  });

  it("a note that can't be written in its place: nothing piles up, and the file is set aside at a later look", async () => {
    const { dir } = await settled("fuobe-auth-stray-busy-");
    const srv = await server(dir);
    expect(srv.store.authStoreMode()).toBe("done");
    legacyWrite(dir, { password: hashPw("busy password"), secret: "f".repeat(64), tokens: [] });
    vi.spyOn(console, "warn").mockImplementation(() => {});
    const rename = vi.spyOn(fs, "renameSync").mockImplementation(() => {
      throw Object.assign(new Error("busy"), { code: "EBUSY" });
    });
    for (const later of [61_000, 122_000]) {
      globalThis.__fuobeAuthClock = () => Date.now() + later;
      expect(srv.store.authStoreMode()).toBe("done");
    }
    rename.mockRestore();
    expect(strays(dir)).toEqual([]);
    expect(fs.readdirSync(dir).filter((f) => f.endsWith(".tmp"))).toEqual([]);
    expect(srv.store.signInNotices()).toEqual([]);
    expect(srv.auth.checkPassword("busy password")).toBe(false);
    expect(srv.auth.checkPassword("owner password")).toBe(true);
    globalThis.__fuobeAuthClock = () => Date.now() + 183_000;
    expect(srv.store.authStoreMode()).toBe("done");
    expect(strays(dir)).toHaveLength(1);
    expect(isNote(dir)).toBe(true);
    expect(srv.store.signInNotices()).toHaveLength(1);
    vi.restoreAllMocks();
  });
});

describe("a crash, or another server, while moving in", () => {
  it("a crash in the middle leaves no rows and no marker, and the next start moves it all in", async () => {
    const { dir, srv, original, fx } = await olderInstall("fuobe-auth-crash-");
    globalThis.__fuobeAuthImportFault = () => {
      throw new Error("the power went out");
    };
    expect(srv.store.authStoreMode()).toBe("failed");
    expect(srv.auth.authEnabled()).toBe(true);
    expect(srv.auth.validSession(fx.cookies.owner)).toBe(false);
    expect(srv.marker()).toBeUndefined();
    expect(srv.count("auth_accounts") + srv.count("auth_tokens") + srv.count("auth_install")).toBe(0);
    expect(fs.readFileSync(authFile(dir), "utf8")).toBe(original);
    globalThis.__fuobeAuthImportFault = undefined;
    const again = await server(dir);
    expect(again.store.authStoreMode()).toBe("bridge");
    expect(again.count("auth_accounts")).toBe(4);
    expect(again.auth.validSession(fx.cookies.owner)).toBe(true);
  });

  const child = (dir: string, go: string, ...args: string[]) =>
    new Promise<{ code: number | null; out: string }>((resolve) => {
      const root = path.join(import.meta.dirname, "..");
      const p = spawn(process.execPath, ["--no-warnings", "--import", pathToFileURL(path.join(root, "tests/helpers/ts-register.mjs")).href, path.join(root, "tests/helpers/auth-child.ts"), ...args, go], {
        cwd: root,
        env: { ...process.env, FUOBE_DATA_DIR: dir },
      });
      let out = "";
      p.stdout.on("data", (d) => (out += d));
      p.stderr.on("data", (d) => (out += d));
      p.on("close", (code) => resolve({ code, out }));
    });
  const result = (out: string) => JSON.parse(/RESULT (.*)/.exec(out)?.[1] ?? "null") as Record<string, unknown> | null;

  it("servers starting at the same moment move it in once", async () => {
    const { dir } = await olderInstall("fuobe-auth-race-");
    // The older install's database is there already (its own tables made); its auth.json waits to move in.
    (await import("@/server/db")).db();
    globalThis.__fuobeDb?.close();
    globalThis.__fuobeDb = undefined;
    const go = path.join(dir, "go");
    const runs = [1, 2, 3, 4].map(() => child(dir, go, "start", "-"));
    await new Promise((r) => setTimeout(r, 2500));
    fs.writeFileSync(go, "");
    const done = await Promise.all(runs);
    for (const d of done) expect(d.code, d.out).toBe(0);
    expect(done.map((d) => result(d.out))).toEqual(Array(4).fill({ mode: "bridge", accounts: 4 }));
    expect(done.filter((d) => d.out.includes("Sign-in data moved")).length).toBe(1);
    const after = await server(dir);
    expect(after.auth.listAccounts()).toHaveLength(4);
    expect(after.count("auth_accounts")).toBe(4);
    expect(JSON.parse(after.marker()!.counts_json as string)).toMatchObject({ accounts: 4 });
  }, 60_000);

  it("servers looking at a stray auth.json at the same moment set it aside once, and all keep serving", async () => {
    const { dir, srv } = await olderInstall("fuobe-auth-stray-race-");
    expect(srv.store.authStoreMode()).toBe("bridge");
    globalThis.__fuobeAuthClock = () => Date.now() + 11 * MIN;
    expect(srv.store.authStoreMode()).toBe("done");
    globalThis.__fuobeAuthClock = undefined;
    globalThis.__fuobeDb?.close();
    globalThis.__fuobeDb = undefined;
    legacyWrite(dir, { password: hashPw("raced password"), secret: "d".repeat(64), tokens: [] });
    const go = path.join(dir, "go");
    const runs = [1, 2, 3, 4].map(() => child(dir, go, "start", "-"));
    await new Promise((r) => setTimeout(r, 2500));
    fs.writeFileSync(go, "");
    const done = await Promise.all(runs);
    for (const d of done) expect(d.code, d.out).toBe(0);
    expect(done.map((d) => result(d.out))).toEqual(Array(4).fill({ mode: "done", accounts: 4 }));
    expect(fs.readdirSync(dir).filter((f) => f.startsWith("auth.json.stray-"))).toHaveLength(1);
    expect(isNote(dir)).toBe(true);
    const after = await server(dir);
    expect(after.store.signInNotices()).toHaveLength(1);
    expect(after.auth.checkPassword("owner password")).toBe(true);
    expect(after.auth.checkPassword("raced password")).toBe(false);
  }, 60_000);

  it("sign-ups with one email at the same moment on several servers: one account", async () => {
    const dir = tempDir("fuobe-auth-signup-race-");
    // A new install that has started once (its database made), with nothing stored yet.
    await server(dir);
    (await import("@/server/db")).db();
    globalThis.__fuobeDb?.close();
    globalThis.__fuobeDb = undefined;
    const go = path.join(dir, "go");
    const runs = [1, 2, 3, 4].map(() => child(dir, go, "signup", "[email protected]"));
    await new Promise((r) => setTimeout(r, 2500));
    fs.writeFileSync(go, "");
    const done = await Promise.all(runs);
    for (const d of done) expect(d.code, d.out).toBe(0);
    const results = done.map((d) => result(d.out)?.result);
    // One in, three told the email is taken (sorted: a failure shows what the odd one said).
    expect(results.map(String).sort()).toEqual(["409", "409", "409", "ok"]);
    const srv = await server(dir);
    expect(srv.auth.listAccounts().map((a) => a.email)).toEqual(["[email protected]"]);
  }, 60_000);
});

describe("an auth.json that can't be read, or can't go in", () => {
  it("can't be read: sign-in stays on, nobody is signed in, nothing is written; fixed, it moves in", async () => {
    const { dir, srv, fx, original } = await olderInstall("fuobe-auth-unreadable-");
    fs.writeFileSync(authFile(dir), "{ not json");
    expect(srv.store.authStoreMode()).toBe("unreadable");
    expect(srv.auth.authEnabled()).toBe(true);
    expect(srv.auth.authStatus().enabled).toBe(true);
    expect(srv.auth.validSession(fx.cookies.owner)).toBe(false);
    expect(srv.auth.validToken(`Bearer ${fx.tokens[0]}`)).toBe(false);
    expect(srv.auth.checkPassword("owner password")).toBe(false);
    expect(() => srv.auth.createAccount("[email protected]", "long enough")).toThrow(/can't be read/);
    expect(() => srv.auth.createSession()).toThrow(/can't be read/);
    expect(() => srv.auth.readAccountIds()).toThrow();
    expect(srv.auth.listAccounts()).toEqual([]);
    expect(fs.readFileSync(authFile(dir), "utf8")).toBe("{ not json");
    expect(srv.marker()).toBeUndefined();
    // Not a request-blocking failure: /login still answers (and asks for a password nobody can use).
    expect(srv.store.authStoreFailed()).toBe(false);
    fs.writeFileSync(authFile(dir), original);
    fs.utimesSync(authFile(dir), new Date(), new Date(Date.now() + 10_000));
    expect(srv.auth.validSession(fx.cookies.owner)).toBe(true);
    expect(srv.store.authStoreMode()).toBe("bridge");
  });

  it("the note an older build can't read, with no sign-in data in the database (fuobe.db lost): fails closed", async () => {
    const dir = tempDir("fuobe-auth-note-");
    let srv = await server(dir);
    srv.auth.setPassword("owner password");
    globalThis.__fuobeDb?.close();
    globalThis.__fuobeDb = undefined;
    for (const f of ["fuobe.db", "fuobe.db-wal", "fuobe.db-shm"]) fs.rmSync(path.join(dir, f), { force: true });
    srv = await server(dir);
    expect(srv.store.authStoreMode()).toBe("unreadable");
    expect(srv.auth.authEnabled()).toBe(true);
  });

  for (const [what, edit] of [
    ["two accounts with one email", (d: { accounts: { email: string; id: string }[] }) => (d.accounts[1].email = d.accounts[0].email)],
    ["an email that isn't lower-case", (d: { accounts: { email: string; id: string }[] }) => (d.accounts[1].email = "[email protected]")],
    ["two accounts with one id", (d: { accounts: { email: string; id: string }[] }) => (d.accounts[1].id = d.accounts[0].id)],
  ] as const) {
    it(`can't go in (${what}): nothing changes, sign-in fails closed and every request gets 503, /login too`, async () => {
      const { dir, srv, fx } = await olderInstall("fuobe-auth-bad-");
      const bad = structuredClone(fx.data);
      edit(bad);
      fs.writeFileSync(authFile(dir), JSON.stringify(bad, null, 2));
      const before = fs.readFileSync(authFile(dir), "utf8");
      const errors = vi.spyOn(console, "error").mockImplementation(() => {});
      expect(srv.store.authStoreMode()).toBe("failed");
      expect(srv.store.authStoreFailed()).toBe(true);
      expect(srv.auth.authEnabled()).toBe(true);
      expect(srv.auth.validSession(fx.cookies.owner)).toBe(false);
      expect(() => srv.auth.setPassword("another password")).toThrow(/can't be read/);
      expect(srv.marker()).toBeUndefined();
      expect(srv.count("auth_accounts") + srv.count("auth_install")).toBe(0);
      expect(fs.readFileSync(authFile(dir), "utf8")).toBe(before);
      // The log says what kind of problem, never an email, a hash or the secret.
      const logged = errors.mock.calls.flat().join("\n");
      expect(logged).toMatch(/couldn't be moved into the database/);
      for (const a of fx.data.accounts) {
        expect(logged).not.toContain(a.email);
        expect(logged).not.toContain(a.hash);
      }
      expect(logged).not.toContain(fx.data.secret);
      errors.mockRestore();
      const { proxy } = await import("@/proxy");
      const { NextRequest } = await import("next/server");
      for (const p of ["/login", "/", "/api/auth/public"]) expect(proxy(new NextRequest(`http://localhost:3000${p}`)).status, p).toBe(503);
      await expectBackSoon(proxy, NextRequest);
    });
  }

  it("a database whose sign-in tables can't be used: fails closed, and visitors get a calm page, never why", async () => {
    const dir = tempDir("fuobe-auth-db-broken-");
    // A table of the same name with other columns (made by something else) is never used.
    const Database = (await import("better-sqlite3")).default;
    const d = new Database(path.join(dir, "fuobe.db"));
    d.exec("CREATE TABLE auth_accounts (id TEXT PRIMARY KEY, name TEXT)");
    d.close();
    vi.spyOn(console, "error").mockImplementation(() => {});
    vi.spyOn(console, "warn").mockImplementation(() => {});
    const srv = await server(dir);
    expect(srv.store.authStoreMode()).toBe("failed");
    expect(srv.auth.authEnabled()).toBe(true);
    expect(srv.auth.checkPassword("")).toBe(false);
    expect(() => srv.auth.setPassword("a password")).toThrow();
    expect(srv.store.signInNotices()).toEqual([]);
    const { proxy } = await import("@/proxy");
    const { NextRequest } = await import("next/server");
    await expectBackSoon(proxy, NextRequest);
    vi.restoreAllMocks();
  });
});

/** Every request gets a calm "back in a moment" (503, so a deploy never switches to it), and no word about why. */
async function expectBackSoon(proxy: (r: import("next/server").NextRequest) => Response, Req: typeof import("next/server").NextRequest) {
  const page = proxy(new Req("http://localhost:3000/login"));
  expect(page.status).toBe(503);
  expect(page.headers.get("content-type")).toMatch(/text\/html/);
  expect(page.headers.get("retry-after")).toBe("30");
  const html = await page.text();
  expect(html).toContain("Back in a moment");
  const api = proxy(new Req("http://localhost:3000/api/auth/me"));
  expect(api.status).toBe(503);
  const body = await api.text();
  expect(JSON.parse(body).error).toMatch(/back in a moment/);
  for (const text of [html, body]) for (const word of [/sign-in/i, /database/i, /auth\.json/i, /\blogs?\b/i, /can't read/i]) expect(text).not.toMatch(word);
}

describe("100,000 accounts", () => {
  it("move in once, and every lookup stays quick", async () => {
    const dir = tempDir("fuobe-auth-100k-");
    const now = Date.now();
    const secret = crypto.randomBytes(32).toString("hex");
    const N = 100_000;
    const fakeHash = (i: number) => `${i.toString(16).padStart(32, "0")}:${sha(String(i))}`;
    const accounts = Array.from({ length: N }, (_, i) => ({
      id: `acct${i.toString(16).padStart(14, "0")}`,
      email: `member-${i}@example.com`,
      hash: fakeHash(i),
      createdAt: now - (N - i) * 1000,
      lastLoginAt: now - i,
      ...(i % 10 === 0 && { gpu: { url: `https://gpu-${i}.example.com` } }),
    }));
    let srv = await server(dir);
    fs.writeFileSync(authFile(dir), JSON.stringify({ password: hashPw("owner password"), secret, tokens: [], accounts }, null, 2));
    let t = performance.now();
    expect(srv.store.authStoreMode()).toBe("bridge");
    const importMs = performance.now() - t;
    expect(srv.count("auth_accounts")).toBe(N);
    expect(importMs).toBeLessThan(30_000);
    // Put aside (the bridge rewrites the whole file on each change, for 10 minutes only).
    globalThis.__fuobeAuthClock = () => now + 11 * MIN;
    expect(srv.store.authStoreMode()).toBe("done");
    globalThis.__fuobeAuthClock = undefined;
    srv = await server(dir);
    const pick = () => accounts[Math.floor(Math.random() * N)];
    const exp = Date.now() + DAY;
    const cookieOf = (a: (typeof accounts)[number]) => `${exp}.${a.id}.${mac(`${secret}.${sha(a.hash)}`, `${exp}.${a.id}`)}`;
    const avg = (n: number, fn: () => void) => {
      const start = performance.now();
      for (let i = 0; i < n; i++) fn();
      return (performance.now() - start) / n;
    };
    expect(avg(2000, () => expect(srv.auth.accountById(pick().id)).toBeDefined())).toBeLessThan(1);
    expect(avg(2000, () => expect(srv.auth.sessionViewer(cookieOf(pick()))?.kind).toBe("account"))).toBeLessThan(1);
    expect(avg(200, () => srv.auth.signOutAccount(pick().id))).toBeLessThan(5);
    t = performance.now();
    expect(srv.auth.listAccounts()).toHaveLength(N);
    expect(performance.now() - t).toBeLessThan(3000);
    t = performance.now();
    srv.auth.createAccount("[email protected]", "long enough");
    expect(performance.now() - t).toBeLessThan(500); // the scrypt hash, then one insert
    expect(() => srv.auth.createAccount(`member-${N - 1}@example.com`, "long enough")).toThrow(expect.objectContaining({ status: 409 }));
  }, 180_000);
});

describe("files", () => {
  it.skipIf(process.platform === "win32")("the database and its -wal and -shm files are readable by the server's user only", async () => {
    const dir = tempDir("fuobe-auth-modes-");
    const srv = await server(dir);
    srv.auth.setPassword("owner password");
    for (const f of ["fuobe.db", "fuobe.db-wal", "fuobe.db-shm"]) {
      const file = path.join(dir, f);
      if (fs.existsSync(file)) expect(fs.statSync(file).mode & 0o077, f).toBe(0);
    }
  });
});