Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

26.5 KB
Raw Blame
const test = require("node:test");
const assert = require("node:assert/strict");

const load = () => import("../../../src/helpers/connectors/connectorIpc.js");

const SCOPE = { accountId: "account-a", authGeneration: 3 };
const ALLOWED = { policyState: "allowed", accountId: "account-a" };

function fakeIpcMain() {
  const handlers = new Map();
  return { handlers, handle: (channel, handler) => handlers.set(channel, handler) };
}

function fakeManager() {
  const calls = [];
  const record =
    (name) =>
    (...args) => {
      calls.push({ name, args });
      return { ok: name };
    };
  return {
    calls,
    status: record("status"),
    prepare: record("prepare"),
    commit: record("commit"),
    cancel: (...args) => {
      calls.push({ name: "cancel", args });
      return { cancelled: false };
    },
    runDirect: record("runDirect"),
    recentActions: record("recentActions"),
  };
}

test("connect needs policy; disconnect never does", async () => {
  const { registerConnectorIpc } = await load();
  const ipcMain = fakeIpcMain();
  const calls = [];
  const manager = {
    ...fakeManager(),
    connect: async (...args) => {
      calls.push(["connect", ...args]);
      return { status: "connected", accountLabel: "chad", workspaceLabel: "Acme" };
    },
    disconnect: async (...args) => {
      calls.push(["disconnect", ...args]);
      return { status: "disconnected" };
    },
  };
  let policyCalls = 0;
  registerConnectorIpc({
    ipcMain,
    manager,
    getPolicyState: async () => {
      policyCalls += 1;
      return "allowed";
    },
  });

  await ipcMain.handlers.get("connector-connect")({}, "slack");
  await ipcMain.handlers.get("connector-disconnect")({}, "slack");

  assert.deepEqual(calls, [
    ["connect", "slack", "allowed"],
    ["disconnect", "slack"],
  ]);
  assert.equal(policyCalls, 1);
  assert.deepEqual(await ipcMain.handlers.get("connector-connect")({}, 7), {
    status: "unavailable",
    reason: "invalid_request",
  });
});

test("each channel reaches the manager, with policy only where something can leave", async () => {
  const { registerConnectorIpc } = await load();
  const ipcMain = fakeIpcMain();
  const manager = fakeManager();
  const policyCalls = [];
  registerConnectorIpc({
    ipcMain,
    manager,
    getPolicyState: async (event) => {
      policyCalls.push(event);
      return "allowed";
    },
    getAccountScope: () => SCOPE,
  });
  const event = { sender: "renderer-1" };
  const h = (channel) => ipcMain.handlers.get(channel);

  await h("connector-status")(event);
  await h("connector-prepare")(event, "slack", "send_message", { text: "hi" });
  await h("connector-commit")(event, "action-1", { body: "hi!" });
  await h("connector-cancel")(event, "action-1", "cancelled_by_user");
  await h("connector-run-direct")(event, "email", "draft", { to: ["[email protected]"] });
  await h("connector-recent-actions")(event, "email", 5);

  assert.deepEqual(
    manager.calls.map((call) => call.name),
    ["status", "prepare", "commit", "cancel", "runDirect", "recentActions"]
  );
  assert.deepEqual(manager.calls[1].args, ["slack", "send_message", { text: "hi" }, ALLOWED]);
  assert.deepEqual(manager.calls[2].args, ["action-1", { body: "hi!" }, ALLOWED]);
  const [connectorId, action, args, auth, runtime] = manager.calls[4].args;
  assert.deepEqual(
    [connectorId, action, args, auth],
    ["email", "draft", { to: ["[email protected]"] }, ALLOWED]
  );
  assert.equal(runtime.webContents, "renderer-1");
  assert.equal(runtime.signal.aborted, false);
  assert.deepEqual(manager.calls[5].args, ["email", 5, "account-a"]);
  assert.equal(policyCalls.length, 3);
});

test("a cancel that lands while a direct run waits on policy stops it", async () => {
  const { registerConnectorIpc } = await load();
  const ipcMain = fakeIpcMain();
  const manager = fakeManager();
  const policyWaits = [];
  registerConnectorIpc({
    ipcMain,
    manager,
    getPolicyState: () => new Promise((resolve) => policyWaits.push(resolve)),
    getAccountScope: () => SCOPE,
  });
  const h = (channel) => ipcMain.handlers.get(channel);

  const cancelledRun = h("connector-run-direct")({}, "email", "draft", { to: ["[email protected]"] }, "run-1");
  assert.deepEqual(await h("connector-cancel")({}, "run-1", "cancelled_by_user"), {
    cancelled: true,
  });
  policyWaits.shift()("allowed");
  assert.deepEqual(await cancelledRun, { state: "not_sent", reason: "cancelled" });

  // Once the run finishes it is no longer tracked, so a later cancel with the
  // same id is only an ordinary (pending approval) cancel.
  const run = h("connector-run-direct")({}, "email", "draft", { to: ["[email protected]"] }, "run-2");
  policyWaits.shift()("allowed");
  await run;
  assert.deepEqual(await h("connector-cancel")({}, "run-2", "cancelled_by_user"), {
    cancelled: false,
  });

  assert.deepEqual(
    manager.calls.map((call) => call.name),
    ["cancel", "runDirect", "cancel"]
  );
});

test("malformed arguments never reach the manager", async () => {
  const { registerConnectorIpc } = await load();
  const ipcMain = fakeIpcMain();
  const manager = fakeManager();
  registerConnectorIpc({
    ipcMain,
    manager,
    getPolicyState: async () => "allowed",
    getAccountScope: () => SCOPE,
  });
  const h = (channel) => ipcMain.handlers.get(channel);

  const prepared = await h("connector-prepare")({}, 42, "send_message", null);
  const committed = await h("connector-commit")({}, null, {});

  assert.deepEqual(prepared, { status: "unavailable", reason: "invalid_request" });
  assert.deepEqual(committed, { state: "not_sent", reason: "invalid_request" });
  for (const [connectorId, action, args] of [
    ["email", "draft", ["[email protected]"]],
    ["email", "draft", null],
    ["email", "", {}],
    [7, "draft", {}],
  ]) {
    assert.deepEqual(await h("connector-run-direct")({}, connectorId, action, args, "run-1"), {
      state: "unavailable",
      reason: "invalid_request",
    });
  }
  assert.equal(manager.calls.length, 0);
});

test("signed out refuses, a lookup that can't tell fails closed, snapshots still map", async () => {
  const { createConnectorPolicyResolver } = await load();
  const neverCalled = async () => {
    throw new Error("must not be called");
  };

  const signedOut = createConnectorPolicyResolver({
    getAuthHeader: async () => ({}),
    getPolicy: neverCalled,
    getAuthGeneration: () => 1,
  });
  assert.equal(await signedOut({}), "signed_out");

  const cannotTell = createConnectorPolicyResolver({
    getAuthHeader: async () => null,
    getPolicy: neverCalled,
    getAuthGeneration: () => 1,
  });
  assert.equal(await cannotTell({}), "unavailable");

  const blocked = createConnectorPolicyResolver({
    getAuthHeader: async () => ({ Authorization: "Bearer t" }),
    getPolicy: async () => ({
      success: true,
      managed: true,
      policy: { features: { agentEnabled: true, connectorsEnabled: false } },
    }),
    getAuthGeneration: () => 1,
  });
  assert.equal(await blocked({}), "blocked");
});

test("the auth generation is read before the header lookup", async () => {
  const { createConnectorPolicyResolver } = await load();
  let generation = 1;
  const expected = [];
  const resolver = createConnectorPolicyResolver({
    // A sign-in lands while the header lookup is running.
    getAuthHeader: async () => {
      generation = 2;
      return { Authorization: "Bearer new-session" };
    },
    getPolicy: async ({ expectedAuthGeneration }) => {
      expected.push(expectedAuthGeneration);
      return { success: false, status: "error", code: "AUTH_CONTEXT_CHANGED" };
    },
    getAuthGeneration: () => generation,
  });

  assert.equal(await resolver({}), "unavailable");
  assert.deepEqual(expected, [1]);
});

test("an auth change during lookup cannot reuse the previous account cached verdict", async () => {
  const { createConnectorPolicyResolver } = await load();
  let generation = 4;
  const peeked = [];
  const resolver = createConnectorPolicyResolver({
    getAuthHeader: async () => {
      generation = 5;
      return { Authorization: "Bearer t" };
    },
    getPolicy: () => new Promise(() => {}),
    peekPolicy: (request) => {
      peeked.push(request.expectedAuthGeneration);
      return { success: true, managed: false, policy: null };
    },
    getAuthGeneration: () => generation,
    timeoutMs: 20,
  });

  assert.equal(await resolver({}), "unavailable");
  assert.deepEqual(peeked, []);
});

test("the auth lookup never reads a destroyed window as signed out", async () => {
  const { createConnectorAuthLookup } = await load();
  const live = { isDestroyed: () => false };
  const gone = { isDestroyed: () => true };
  const lookup = (hasToken, win) =>
    createConnectorAuthLookup({
      hasBearerToken: () => hasToken,
      windowFor: () => win,
      authHeaderFor: async (w) => {
        if (hasToken) return { Authorization: "Bearer t" };
        return w === live ? {} : { Cookie: "session=1" };
      },
    });

  assert.deepEqual(await lookup(true, null)({}), { Authorization: "Bearer t" });
  assert.equal(await lookup(false, null)({}), null);
  assert.equal(await lookup(false, gone)({}), null);
  assert.deepEqual(await lookup(false, live)({}), {});
});

test("a policy lookup that hangs or throws fails closed", async () => {
  const { createConnectorPolicyResolver } = await load();
  const hanging = createConnectorPolicyResolver({
    getAuthHeader: async () => ({ Authorization: "Bearer t" }),
    getPolicy: () => new Promise(() => {}),
    getAuthGeneration: () => 1,
    timeoutMs: 20,
  });
  const started = Date.now();
  assert.equal(await hanging({}), "unavailable");
  assert.ok(Date.now() - started < 1000);

  const throwing = createConnectorPolicyResolver({
    getAuthHeader: async () => ({ Cookie: "session=1" }),
    getPolicy: async () => {
      throw new Error("offline");
    },
    getAuthGeneration: () => 1,
  });
  assert.equal(await throwing({}), "unavailable");

  const throttled = createConnectorPolicyResolver({
    getAuthHeader: async () => ({ Authorization: "Bearer t" }),
    getPolicy: async () => ({ success: false, status: "error", code: "POLICY_RETRY_THROTTLED" }),
    getAuthGeneration: () => 1,
  });
  assert.equal(await throttled({}), "unavailable");
});

test("a slow refresh falls back to the verdict already held for the account", async () => {
  const { createConnectorPolicyResolver } = await load();
  const peeked = [];
  const slowRefresh = (held) =>
    createConnectorPolicyResolver({
      getAuthHeader: async () => ({ Authorization: "Bearer t" }),
      getPolicy: () => new Promise(() => {}),
      peekPolicy: (request) => {
        peeked.push(request);
        return held;
      },
      getAuthGeneration: () => 7,
      timeoutMs: 20,
    });

  assert.equal(await slowRefresh({ success: true, managed: false, policy: null })({}), "allowed");
  assert.equal(
    await slowRefresh({
      success: true,
      managed: true,
      policy: { features: { connectorsEnabled: false } },
    })({}),
    "blocked"
  );
  // Nothing held yet (the session's first lookup): still fails closed.
  assert.equal(await slowRefresh(null)({}), "unavailable");
  assert.deepEqual(peeked[0], {
    expectedAuthGeneration: 7,
    authHeaders: { Authorization: "Bearer t" },
  });
});

test("the auth generation is read before the header lookup", async () => {
  const { createConnectorPolicyResolver } = await load();
  let generation = 4;
  const requests = [];
  const resolver = createConnectorPolicyResolver({
    getAuthHeader: async () => {
      // A sign-in lands while the cookie lookup is in flight.
      generation = 5;
      return { Cookie: "session=old" };
    },
    getPolicy: async (request) => {
      requests.push(request);
      return { success: false, status: "error", code: "AUTH_CONTEXT_CHANGED" };
    },
    getAuthGeneration: () => generation,
  });

  assert.equal(await resolver({}), "unavailable");
  assert.equal(requests[0].expectedAuthGeneration, 4);
});

test("the deadline covers the auth-header lookup too", async () => {
  const { createConnectorPolicyResolver } = await load();
  const hangingAuth = createConnectorPolicyResolver({
    getAuthHeader: () => new Promise(() => {}),
    getPolicy: async () => {
      throw new Error("must not be called");
    },
    getAuthGeneration: () => 1,
    timeoutMs: 20,
  });
  const started = Date.now();
  assert.equal(await hangingAuth({}), "unavailable");
  assert.ok(Date.now() - started < 1000);

  const throwingAuth = createConnectorPolicyResolver({
    getAuthHeader: async () => {
      throw new Error("window destroyed");
    },
    getPolicy: async () => ({ success: true, managed: false, policy: null }),
    getAuthGeneration: () => {
      throw new Error("token store unavailable");
    },
  });
  assert.equal(await throwingAuth({}), "unavailable");
});

test("contact lookup trims the query and returns nothing the org policy doesn't allow", async () => {
  const { registerConnectorIpc } = await load();
  const ipcMain = fakeIpcMain();
  const queries = [];
  const policies = ["allowed", "blocked", "unavailable"];
  registerConnectorIpc({
    ipcMain,
    manager: fakeManager(),
    getPolicyState: async () => policies.shift(),
    getAccountScope: () => SCOPE,
    findContacts: (query) => {
      queries.push(query);
      return {
        contacts: [{ name: "Gabe", email: "[email protected]", lastMet: null }],
        hasMore: false,
      };
    },
  });
  const handler = ipcMain.handlers.get("connector-find-contacts");
  assert.deepEqual(await handler({}, "  Gabe "), {
    contacts: [{ name: "Gabe", email: "[email protected]", lastMet: null }],
    hasMore: false,
  });
  assert.deepEqual(await handler({}, "Gabe"), {
    contacts: [],
    unavailableReason: "policy_blocked",
  });
  assert.deepEqual(await handler({}, "Gabe"), {
    contacts: [],
    unavailableReason: "policy_unavailable",
  });
  // Malformed or oversized queries never reach the policy lookup or the search.
  assert.deepEqual(await handler({}, 42), { contacts: [] });
  assert.deepEqual(await handler({}, "a".repeat(201)), { contacts: [] });
  assert.deepEqual(queries, ["Gabe"]);
  assert.equal(policies.length, 0);
});

test("an action runs under the account bound to its credential, or none if it changed", async () => {
  const { registerConnectorIpc } = await load();
  const cases = [
    // A stable scope.
    [SCOPE, SCOPE, "account-a"],
    // Signed out, or signed in but not yet scoped (the null-scope gap).
    [null, null, null],
    // A sign-in lands during the policy wait.
    [null, SCOPE, null],
    // A → B: another account's credential arrives during the wait.
    [SCOPE, { accountId: "account-b", authGeneration: 4 }, null],
    // Same account, rotated credential.
    [SCOPE, { ...SCOPE, authGeneration: 4 }, null],
  ];
  for (const [before, after, accountId] of cases) {
    const ipcMain = fakeIpcMain();
    const manager = fakeManager();
    let scope = before;
    registerConnectorIpc({
      ipcMain,
      manager,
      getPolicyState: async () => {
        scope = after;
        return "allowed";
      },
      getAccountScope: () => scope,
    });
    await ipcMain.handlers.get("connector-run-direct")({}, "email", "draft", {});
    assert.deepEqual(manager.calls[0].args[3], { policyState: "allowed", accountId });
  }
});

test("recent actions are read for the account bound to the credential", async () => {
  const { registerConnectorIpc } = await load();
  const ipcMain = fakeIpcMain();
  const manager = fakeManager();
  let scope = null;
  registerConnectorIpc({
    ipcMain,
    manager,
    getPolicyState: async () => "allowed",
    getAccountScope: () => scope,
  });
  const recent = ipcMain.handlers.get("connector-recent-actions");
  await recent({}, "email", 5);
  scope = SCOPE;
  await recent({}, "email", 5);
  assert.deepEqual(
    manager.calls.map((call) => call.args),
    [
      ["email", 5, null],
      ["email", 5, "account-a"],
    ]
  );
});

test("a cancel after policy resolves still reaches the running action's signal", async () => {
  const { registerConnectorIpc } = await load();
  const ipcMain = fakeIpcMain();
  const gate = [];
  let runtimeSeen = null;
  const manager = {
    ...fakeManager(),
    runDirect: (connectorId, action, args, auth, runtime) => {
      runtimeSeen = runtime;
      return new Promise((resolve) => gate.push(resolve));
    },
  };
  registerConnectorIpc({
    ipcMain,
    manager,
    getPolicyState: async () => "allowed",
    getAccountScope: () => SCOPE,
  });
  const h = (channel) => ipcMain.handlers.get(channel);

  const run = h("connector-run-direct")({}, "email", "draft", {}, "run-1");
  await new Promise((resolve) => setImmediate(resolve));
  assert.equal(runtimeSeen.signal.aborted, false);
  assert.deepEqual(await h("connector-cancel")({}, "run-1", "cancelled_by_user"), {
    cancelled: true,
  });
  assert.equal(runtimeSeen.signal.aborted, true);
  gate.shift()({ state: "not_sent", reason: "cancelled" });
  assert.deepEqual(await run, { state: "not_sent", reason: "cancelled" });
});

test("a run id can't hijack a live run or swallow an approval's cancel", async () => {
  const { registerConnectorIpc } = await load();
  const ipcMain = fakeIpcMain();
  const manager = fakeManager();
  const policyWaits = [];
  registerConnectorIpc({
    ipcMain,
    manager,
    getPolicyState: () => new Promise((resolve) => policyWaits.push(resolve)),
    getAccountScope: () => SCOPE,
  });
  const h = (channel) => ipcMain.handlers.get(channel);

  const first = h("connector-run-direct")({}, "email", "draft", {}, "action-1");
  assert.deepEqual(await h("connector-run-direct")({}, "email", "draft", {}, "action-1"), {
    state: "unavailable",
    reason: "invalid_request",
  });

  // "action-1" is also a pending approval's id: its cancel still reaches main.
  await h("connector-cancel")({}, "action-1", "cancelled_by_user");
  assert.deepEqual(manager.calls[0], { name: "cancel", args: ["action-1", "cancelled_by_user"] });

  policyWaits.shift()("allowed");
  assert.deepEqual(await first, { state: "not_sent", reason: "cancelled" });
  assert.equal(policyWaits.length, 0);
});

test("a held verdict that throws on the deadline fallback fails closed", async () => {
  const { createConnectorPolicyResolver } = await load();
  const resolver = createConnectorPolicyResolver({
    getAuthHeader: async () => ({ Authorization: "Bearer t" }),
    getPolicy: () => new Promise(() => {}),
    peekPolicy: () => {
      throw new Error("cache corrupted");
    },
    getAuthGeneration: () => 1,
    timeoutMs: 20,
  });
  assert.equal(await resolver({}), "unavailable");
});

function registerNoteAttendees(policies) {
  const ipcMain = fakeIpcMain();
  const requests = [];
  return load().then(({ registerConnectorIpc }) => {
    registerConnectorIpc({
      ipcMain,
      manager: fakeManager(),
      getPolicyState: async () => policies.shift(),
      getAccountScope: () => SCOPE,
      noteAttendees: (request) => {
        requests.push(request);
        return request.participants
          .filter((attendee) => !attendee.self)
          .map(({ email, displayName }) => ({ name: displayName, email }));
      },
    });
    const handler = ipcMain.handlers.get("connector-note-attendees");
    return { handler, requests };
  });
}

const meeting = (overrides) => ({
  noteId: null,
  participants: [],
  calendarEventId: null,
  selfEmail: null,
  ...overrides,
});

test("note attendees pass only well-formed fields to the filter, and follow the org policy", async () => {
  const policies = ["allowed", "blocked", "unavailable"];
  const { handler, requests } = await registerNoteAttendees(policies);
  const participants = [
    {
      email: "[email protected]",
      displayName: "Dana",
      responseStatus: "accepted",
      self: false,
      extra: "dropped",
    },
    { email: "[email protected]", displayName: null, self: true },
    { email: "[email protected]", displayName: "Room", resource: true, self: "yes" },
    { email: "" },
    { displayName: "No address" },
    "[email protected]",
    null,
    { email: `${"a".repeat(320)}@example.com` },
    { email: "[email protected]", displayName: "x".repeat(201) },
  ];
  const request = meeting({ noteId: 7, participants, selfEmail: " [email protected] " });

  assert.deepEqual(await handler({}, request), {
    attendees: [
      { name: "Dana", email: "[email protected]" },
      { name: "Room", email: "[email protected]" },
      { name: null, email: "[email protected]" },
    ],
  });
  assert.deepEqual(requests, [
    {
      noteId: 7,
      participants: [
        { email: "[email protected]", displayName: "Dana", self: false, resource: false },
        { email: "[email protected]", displayName: null, self: true, resource: false },
        { email: "[email protected]", displayName: "Room", self: false, resource: true },
        { email: "[email protected]", displayName: null, self: false, resource: false },
      ],
      calendarEventId: null,
      selfEmail: "[email protected]",
    },
  ]);
  assert.deepEqual(await handler({}, request), {
    attendees: [],
    unavailableReason: "policy_blocked",
  });
  assert.deepEqual(await handler({}, request), {
    attendees: [],
    unavailableReason: "policy_unavailable",
  });
  assert.equal(requests.length, 1);
});

test("a note's attendee list is cut at 200, and a request with nothing to look up skips the policy lookup", async () => {
  const policies = ["allowed"];
  const { handler, requests } = await registerNoteAttendees(policies);
  const many = Array.from({ length: 300 }, (_, index) => ({
    email: `person${index}@example.com`,
    displayName: null,
    self: false,
  }));

  const { attendees } = await handler({}, meeting({ participants: many }));

  assert.equal(attendees.length, 200);
  assert.equal(attendees[199].email, "[email protected]");
  assert.equal(requests[0].participants.length, 200);
  // Nothing to filter: no policy lookup, no filter call.
  for (const request of [
    "not a request",
    null,
    [],
    meeting({ participants: "not a list" }),
    meeting({ participants: [{ displayName: "No address" }] }),
    meeting({ noteId: 0 }),
    meeting({ noteId: "7" }),
    meeting({ calendarEventId: "x".repeat(1025) }),
    meeting({ calendarEventId: "" }),
  ]) {
    assert.deepEqual(await handler({}, request), { attendees: [] }, JSON.stringify(request));
  }
  assert.equal(requests.length, 1);
  assert.equal(policies.length, 0);
});

test("a note id or calendar event alone reaches the filter; malformed values don't", async () => {
  const policies = ["allowed", "allowed"];
  const { handler, requests } = await registerNoteAttendees(policies);

  await handler({}, meeting({ calendarEventId: "event-1" }));
  await handler(
    {},
    meeting({
      noteId: 9,
      calendarEventId: { id: "event-1" },
      selfEmail: `${"a".repeat(321)}@x.test`,
    })
  );

  assert.deepEqual(requests, [meeting({ calendarEventId: "event-1" }), meeting({ noteId: 9 })]);
  assert.equal(policies.length, 0);
});

test("without an attendee filter, no note-attendees channel is registered", async () => {
  const { registerConnectorIpc } = await load();
  const ipcMain = fakeIpcMain();
  registerConnectorIpc({ ipcMain, manager: fakeManager(), getPolicyState: async () => "allowed" });
  assert.equal(ipcMain.handlers.has("connector-note-attendees"), false);
});

test("a query reaches the manager with the call's auth; malformed requests don't", async () => {
  const { registerConnectorIpc } = await load();
  const ipcMain = fakeIpcMain();
  const calls = [];
  const manager = {
    ...fakeManager(),
    query: async (...args) => {
      calls.push(args);
      return { status: "ok", items: [], truncated: false };
    },
  };
  registerConnectorIpc({
    ipcMain,
    manager,
    getPolicyState: async () => "allowed",
    getAccountScope: () => SCOPE,
  });
  const query = ipcMain.handlers.get("connector-query");

  assert.deepEqual(await query({}, "linear", "search_issues", { query: "login" }), {
    status: "ok",
    items: [],
    truncated: false,
  });
  assert.deepEqual(calls, [["linear", "search_issues", { query: "login" }, ALLOWED]]);

  for (const bad of [
    [7, "search_issues", {}],
    ["linear", "", {}],
    ["linear", "search_issues", null],
    ["linear", "search_issues", ["login"]],
  ]) {
    assert.deepEqual(await query({}, ...bad), { status: "unavailable", reason: "invalid_request" });
  }
  assert.equal(calls.length, 1);
});

test("cancelling a connect needs no policy, and a malformed id never reaches the manager", async () => {
  const { registerConnectorIpc } = await load();
  const ipcMain = fakeIpcMain();
  const cancels = [];
  const manager = {
    ...fakeManager(),
    cancelConnect: (connectorId) => {
      cancels.push(connectorId);
      return { status: "cancelled" };
    },
  };
  let policyCalls = 0;
  registerConnectorIpc({
    ipcMain,
    manager,
    getPolicyState: async () => {
      policyCalls += 1;
      return "blocked";
    },
  });
  const cancel = ipcMain.handlers.get("connector-cancel-connect");

  assert.deepEqual(await cancel({}, "github"), { status: "cancelled" });
  for (const bad of [undefined, "", 7, { id: "github" }]) {
    assert.deepEqual(await cancel({}, bad), { status: "unavailable", reason: "invalid_request" });
  }

  assert.deepEqual(cancels, ["github"]);
  assert.equal(policyCalls, 0);
});

// Leaving Settings mid policy wait must not leave a device flow polling for
// 15 minutes with nothing on screen.
test("a cancel during the policy wait stops the connect before the manager starts it", async () => {
  const { registerConnectorIpc } = await load();
  const ipcMain = fakeIpcMain();
  const calls = [];
  const manager = {
    ...fakeManager(),
    connect: async (...args) => {
      calls.push(["connect", ...args]);
      return { status: "connected" };
    },
    cancelConnect: (connectorId) => {
      calls.push(["cancelConnect", connectorId]);
      return { status: "idle" };
    },
  };
  const policyWaits = [];
  registerConnectorIpc({
    ipcMain,
    manager,
    getPolicyState: () => new Promise((resolve) => policyWaits.push(resolve)),
  });
  const h = (channel) => ipcMain.handlers.get(channel);

  const githubConnect = h("connector-connect")({}, "github");
  const slackConnect = h("connector-connect")({}, "slack");
  assert.deepEqual(await h("connector-cancel-connect")({}, "github"), { status: "cancelled" });

  for (const resolve of policyWaits) resolve("allowed");
  assert.deepEqual(await githubConnect, { status: "failed", errorCode: "oauth_cancelled" });
  // Only that connector's connect is stopped, and one that got through is
  // left to the manager's own cancel.
  assert.deepEqual(await slackConnect, { status: "connected" });
  assert.deepEqual(await h("connector-cancel-connect")({}, "github"), { status: "idle" });
  assert.deepEqual(calls, [
    ["cancelConnect", "github"],
    ["connect", "slack", "allowed"],
    ["cancelConnect", "github"],
  ]);
});