File size: 2,923 Bytes
488aedf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { afterAll, describe, expect, mock, test } from "bun:test";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import {
  __resetCaptchaWorkerDispatchForTest,
  __setInProcessSolverForTest,
  solveViaWorkerOrInProcess,
} from "./captcha-worker-dispatch.js";

// The dispatch layer must degrade, not fail, when the worker path is
// unusable. Worker mode is exercised with a REAL worker_threads round trip
// against a canned fixture entry (no happy-dom, no network); the fallback
// cases substitute the in-process solver through the test seam. Neither
// captcha-solver.js nor captcha-happy.js is module-mocked here: those mocks
// are process-wide in Bun and leak partial export surfaces into later test
// files (order-dependent across platforms).
describe("captcha worker dispatch (worker / in-process)", () => {
  const fixtureDir = fs.mkdtempSync(path.join(os.tmpdir(), "cap-worker-"));
  const fixturePath = path.join(fixtureDir, "fixture-worker.mjs");
  fs.writeFileSync(
    fixturePath,
    [
      'import { parentPort } from "node:worker_threads";',
      "if (!parentPort) throw new Error('fixture requires parent port');",
      "parentPort.on('message', (m) => {",
      "  parentPort.postMessage({ id: m.id, ok: true, param: `fixture-param:${m.scene}:${m.region}` });",
      "});",
    ].join("\n"),
    "utf8",
  );
  afterAll(() => {
    __setInProcessSolverForTest(null);
    try { fs.rmSync(fixtureDir, { recursive: true, force: true }); } catch {}
  });

  test("solves via a real worker when the entry asset resolves", async () => {
    mock.module("./captcha-worker-asset.js", () => ({ default: fixturePath }));
    __resetCaptchaWorkerDispatchForTest();
    const param = await solveViaWorkerOrInProcess({ scene: "sc1", region: "rg1", prefix: "pf1" });
    expect(param).toBe("fixture-param:sc1:rg1");
  });

  test("falls back to in-process solving when the entry is unavailable", async () => {
    mock.module("./captcha-worker-asset.js", () => ({ default: null }));
    __setInProcessSolverForTest(
      async (opts) => `inproc-param:${opts.scene}`,
    );
    __resetCaptchaWorkerDispatchForTest();
    const param = await solveViaWorkerOrInProcess({ scene: "sc2", region: "rg2", prefix: "pf2" });
    expect(param).toBe("inproc-param:sc2");
  });

  test("an unloadable worker entry degrades to in-process solving", async () => {
    // A path that cannot be loaded as a worker module (a directory) surfaces
    // as an async module-not-found 'error' — it must fall back, not reject.
    mock.module("./captcha-worker-asset.js", () => ({ default: fixtureDir }));
    __setInProcessSolverForTest(
      async (opts) => `inproc-param:${opts.scene}`,
    );
    __resetCaptchaWorkerDispatchForTest();
    const param = await solveViaWorkerOrInProcess({ scene: "sc3", region: "rg3", prefix: "pf3" });
    expect(param).toBe("inproc-param:sc3");
  });
});