Skip to content

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

21.8 KB
Raw Blame
const test = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const Module = require("node:module");

let userDataDir = fs.mkdtempSync(path.join(os.tmpdir(), "openwhispr-connector-actions-db-"));
const originalLoad = Module._load;

Module._load = function patchedLoad(request, parent, isMain) {
  if (request === "electron") {
    return {
      app: {
        getPath: () => userDataDir,
        getAppPath: () => process.cwd(),
        isReady: () => false,
      },
    };
  }
  return originalLoad.call(this, request, parent, isMain);
};

process.env.NODE_ENV = "test";

const DatabaseManager = require("../../src/helpers/database.js");
const { createActionLog } = require("../../src/helpers/connectors/actionLog.js");

function isNativeBindingUnavailable(error) {
  const message = String(error?.message || error);
  return (
    message.includes("NODE_MODULE_VERSION") ||
    message.includes("Could not locate the bindings file")
  );
}

function createDb(t) {
  userDataDir = fs.mkdtempSync(path.join(os.tmpdir(), "openwhispr-connector-actions-db-"));
  try {
    const db = new DatabaseManager();
    db.setActiveAccountId("account-a");
    return db;
  } catch (error) {
    if (isNativeBindingUnavailable(error)) {
      t.skip("better-sqlite3 native binding is not available for this Node runtime");
      return null;
    }
    throw error;
  }
}

test("an action row moves through its states and lists newest first", (t) => {
  const db = createDb(t);
  if (!db) return;
  const log = createActionLog(db);

  log.insert({
    id: "a1",
    accountId: "account-a",
    connector: "email",
    action: "draft",
    kind: "direct",
    destinationLabel: "[email protected]",
    state: "sent",
  });
  log.insert({
    id: "a2",
    accountId: "account-a",
    connector: "slack",
    action: "send_message",
    kind: "approval",
    destinationLabel: "#eng",
    state: "pending",
  });
  log.update("a2", { state: "committing" });
  log.update("a2", { state: "sent", resultUrl: "https://slack.test/p/1" });

  const [row] = log.listRecent("slack", 10, "account-a");
  assert.equal(row.id, "a2");
  assert.equal(row.state, "sent");
  assert.equal(row.resultUrl, "https://slack.test/p/1");
  assert.equal(row.destinationLabel, "#eng");
  assert.equal(log.listRecent("email", 10, "account-a")[0].kind, "direct");
  db.db.close();
});

test("rows interrupted by a quit are reconciled on the next launch", (t) => {
  const db = createDb(t);
  if (!db) return;
  const log = createActionLog(db);
  log.insert({
    id: "p1",
    accountId: "account-a",
    connector: "slack",
    action: "send_message",
    kind: "approval",
    state: "pending",
  });
  log.insert({
    id: "c1",
    accountId: "account-a",
    connector: "slack",
    action: "send_message",
    kind: "approval",
    state: "committing",
  });
  log.insert({
    id: "s1",
    accountId: "account-a",
    connector: "slack",
    action: "send_message",
    kind: "approval",
    state: "sent",
  });

  assert.deepEqual(log.reconcileInterrupted(), { unknown: 1, cancelled: 1, orphaned: 0 });

  const states = Object.fromEntries(
    log.listRecent("slack", 10, "account-a").map((row) => [row.id, row])
  );
  assert.equal(states.c1.state, "unknown");
  assert.equal(states.c1.errorCode, "app_quit");
  assert.equal(states.p1.state, "cancelled");
  assert.equal(states.s1.state, "sent");
  db.db.close();
});

test("a guarded update only moves a row out of the expected state", (t) => {
  const db = createDb(t);
  if (!db) return;
  const log = createActionLog(db);
  log.insert({
    id: "g1",
    accountId: "account-a",
    connector: "slack",
    action: "send_message",
    kind: "approval",
    state: "pending",
  });

  assert.equal(log.update("g1", { state: "committing" }, "pending"), 1);
  assert.equal(log.update("g1", { state: "committing" }, "pending"), 0);
  assert.equal(log.update("missing", { state: "committing" }, "pending"), 0);
  assert.equal(log.update("g1", { state: "sent" }), 1);
  db.db.close();
});

test("a receipt belongs to the account it names, whatever the database scope says", (t) => {
  const db = createDb(t);
  if (!db) return;
  const log = createActionLog(db);
  const draft = (id, accountId, destinationLabel) =>
    log.insert({
      id,
      accountId,
      connector: "email",
      action: "draft",
      kind: "direct",
      destinationLabel,
      state: "sent",
    });

  draft("a1", "account-a", "[email protected]");
  // The A -> B gap: B's credential is in use, the database scope still says A.
  draft("b1", "account-b", "[email protected]");
  // The null-scope gap: signed in as B, database scope not synced yet.
  db.setActiveAccountId(null);
  draft("b2", "account-b", "[email protected]");

  db.setActiveAccountId("account-a");
  assert.deepEqual(
    log.listRecent("email", 10, "account-a").map((row) => row.id),
    ["a1"]
  );
  assert.deepEqual(
    log.listRecent("email", 10, "account-b").map((row) => row.id),
    ["b2", "b1"]
  );
  assert.deepEqual(log.listRecent("email", 10, null), []);

  db.deleteAccountData("account-a");
  assert.deepEqual(log.listRecent("email", 10, "account-a"), []);
  assert.equal(log.listRecent("email", 10, "account-b").length, 2);
  db.db.close();
});

test("receipts with no account are removed on launch", (t) => {
  const db = createDb(t);
  if (!db) return;
  const log = createActionLog(db);
  log.insert({
    id: "legacy",
    accountId: null,
    connector: "email",
    action: "draft",
    kind: "direct",
    state: "sent",
  });
  log.insert({
    id: "kept",
    accountId: "account-a",
    connector: "email",
    action: "draft",
    kind: "direct",
    state: "sent",
  });

  assert.deepEqual(log.reconcileInterrupted(), { unknown: 0, cancelled: 0, orphaned: 1 });
  assert.deepEqual(
    db.db
      .prepare("SELECT id FROM connector_actions")
      .all()
      .map((row) => row.id),
    ["kept"]
  );
  db.db.close();
});

test("listRecent respects the limit", (t) => {
  const db = createDb(t);
  if (!db) return;
  const log = createActionLog(db);
  for (let i = 0; i < 5; i += 1) {
    log.insert({
      id: `e${i}`,
      accountId: "account-a",
      connector: "email",
      action: "draft",
      kind: "direct",
      state: "sent",
    });
  }
  assert.equal(log.listRecent("email", 3, "account-a").length, 3);
  db.db.close();
});

test("receipts list only for their account and go with it on account deletion", (t) => {
  const db = createDb(t);
  if (!db) return;
  const log = createActionLog(db);
  db.setActiveAccountId("acct-a");

  log.insert({
    id: "a1",
    accountId: "acct-a",
    connector: "slack",
    action: "send_message",
    kind: "approval",
    destinationLabel: "#eng",
    state: "sent",
  });
  log.insert({
    id: "b1",
    accountId: "acct-b",
    connector: "slack",
    action: "send_message",
    kind: "approval",
    destinationLabel: "#ops",
    state: "sent",
  });

  assert.deepEqual(
    log.listRecent("slack", 10, "acct-a").map((row) => row.id),
    ["a1"]
  );
  assert.deepEqual(log.listRecent("slack", 10, null), []);

  db.deleteAccountData("acct-a");
  assert.deepEqual(log.listRecent("slack", 10, "acct-a"), []);
  assert.deepEqual(
    log.listRecent("slack", 10, "acct-b").map((row) => row.id),
    ["b1"]
  );
  db.db.close();
});

test("contact lookup sources cover meetings, synced contacts and the user's accounts", (t) => {
  const db = createDb(t);
  if (!db) return;
  const event = (id, startTime, organizer) => ({
    id,
    calendar_id: "primary",
    provider: "google",
    summary: "Lunch",
    start_time: startTime,
    end_time: startTime,
    is_all_day: false,
    status: "confirmed",
    organizer_email: organizer,
    attendees_count: 1,
    attendees: JSON.stringify([{ email: organizer, displayName: "Someone", self: false }]),
  });
  const soon = new Date(Date.now() + 60 * 60 * 1000).toISOString();
  const later = new Date(Date.now() + 20 * 24 * 60 * 60 * 1000).toISOString();
  db.upsertCalendarEvents([
    event("evt-later", later, "[email protected]"),
    event("evt-soon", soon, "[email protected]"),
    // A meeting the user never had can't count as meeting anyone.
    { ...event("evt-cancelled", soon, "[email protected]"), status: "cancelled" },
    { ...event("evt-declined", soon, "[email protected]"), self_response_status: "declined" },
  ]);
  db.upsertContacts([{ email: "[email protected]", displayName: "Priya Shah" }], "manual");
  db.saveGoogleCalendars(
    [{ id: "primary", summary: "Chad", is_primary: true }],
    "[email protected]"
  );
  db.saveMicrosoftCalendars([{ id: "work", summary: "Calendar" }], "[email protected]");

  const sources = db.getContactLookupSources();

  // The meetings nearest to now come first, so a limit keeps the relevant ones.
  assert.deepEqual(
    sources.meetings.map((row) => row.organizer_email),
    ["[email protected]", "[email protected]"]
  );
  assert.match(sources.meetings[0].attendees, /Someone/);
  assert.deepEqual(sources.contacts, [{ email: "[email protected]", display_name: "Priya Shah" }]);
  assert.deepEqual([...sources.excludedEmails].sort(), ["[email protected]", "[email protected]"]);
  db.db.close();
});

test("contacts come most recently synced first, and a sync purges the addresses it keeps out", (t) => {
  const db = createDb(t);
  if (!db) return;
  db.saveAppleCalendars([{ id: "a-cal", title: "Home" }]);
  db.syncCalendarContacts(
    "apple",
    null,
    [
      { email: "[email protected]", displayName: "Josh Old" },
      { email: "[email protected]", displayName: "Josh New" },
    ],
    []
  );
  db.db
    .prepare("UPDATE contacts SET updated_at = '2020-01-01 00:00:00' WHERE email = ?")
    .run("[email protected]");

  assert.deepEqual(
    db.getContactLookupSources().contacts.map((row) => row.email),
    ["[email protected]", "[email protected]"]
  );
  db.syncCalendarContacts("apple", null, [], ["[email protected]"]);
  assert.deepEqual(
    db.getContactLookupSources().contacts.map((row) => row.email),
    ["[email protected]"]
  );
  db.db.close();
});

test("only meetings on the user's own selected calendars count", (t) => {
  const db = createDb(t);
  if (!db) return;
  db.saveGoogleCalendars(
    [
      { id: "[email protected]", summary: "Me" },
      { id: "[email protected]", summary: "Dana" },
      { id: "[email protected]", summary: "Team" },
    ],
    "[email protected]"
  );
  db.updateCalendarSelection("[email protected]", false);
  db.saveMicrosoftCalendars([{ id: "work", summary: "Calendar" }], "[email protected]");
  db.saveAppleCalendars([{ id: "home", title: "Home" }]);
  const soon = new Date(Date.now() + 60 * 60 * 1000).toISOString();
  const event = (id, provider, calendarId) => ({
    id,
    calendar_id: calendarId,
    provider,
    start_time: soon,
    end_time: soon,
    is_all_day: false,
    status: "confirmed",
    organizer_email: `${id}@example.com`,
    attendees_count: 0,
    attendees: null,
  });
  db.upsertCalendarEvents([
    event("own", "google", "[email protected]"),
    // A colleague's shared calendar: its people come through contacts.
    event("shared", "google", "[email protected]"),
    event("deselected", "google", "[email protected]"),
    // Left behind by a sync that finished after its account was disconnected.
    event("orphan", "google", "[email protected]"),
    event("work", "microsoft", "work"),
    event("home", "apple", "home"),
    event("removed", "apple", "removed-calendar"),
  ]);

  assert.deepEqual(
    db
      .getContactLookupSources()
      .meetings.map((row) => row.organizer_email)
      .sort(),
    ["[email protected]", "[email protected]", "[email protected]"]
  );
  db.db.close();
});

function storedContactEmails(db) {
  return db.db
    .prepare("SELECT email FROM contacts ORDER BY email")
    .all()
    .map((row) => row.email);
}

test("contact lookup only sees hand-added contacts and connected accounts' contacts", (t) => {
  const db = createDb(t);
  if (!db) return;
  db.saveGoogleCalendars([{ id: "g-cal", summary: "Me" }], "[email protected]");
  db.saveMicrosoftCalendars([{ id: "m-cal", summary: "Calendar" }], "[email protected]");
  db.saveAppleCalendars([{ id: "a-cal", title: "Home" }]);
  db.syncCalendarContacts("google", "[email protected]", [{ email: "[email protected]" }], []);
  db.syncCalendarContacts("microsoft", "[email protected]", [{ email: "[email protected]" }], []);
  db.syncCalendarContacts("apple", null, [{ email: "[email protected]" }], []);
  db.upsertContacts([{ email: "[email protected]" }], "manual");
  // Synced by both accounts.
  db.syncCalendarContacts("google", "[email protected]", [{ email: "[email protected]" }], []);
  db.syncCalendarContacts("microsoft", "[email protected]", [{ email: "[email protected]" }], []);
  // Stored by an older build, before contacts had a source.
  db.db
    .prepare("INSERT INTO contacts (email, display_name) VALUES (?, ?)")
    .run("[email protected]", "Lincoln Room");

  const lookup = () =>
    db
      .getContactLookupSources()
      .contacts.map((row) => row.email)
      .sort();
  assert.deepEqual(lookup(), [
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]",
  ]);
  // Note-participant autocomplete still offers the legacy row.
  assert.equal(db.searchContacts("lincoln").length, 1);

  const stored = () => storedContactEmails(db);
  db.removeGoogleAccount("[email protected]");
  assert.deepEqual(stored(), [
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]",
  ]);
  db.removeMicrosoftAccount("[email protected]");
  db.clearAppleCalendarData();
  assert.deepEqual(stored(), ["[email protected]", "[email protected]"]);
  assert.deepEqual(lookup(), ["[email protected]"]);
  // A snapshot that lands after the disconnect still isn't a connected account.
  db.syncCalendarContacts("apple", null, [{ email: "[email protected]" }], []);
  assert.deepEqual(lookup(), ["[email protected]"]);
  db.db.close();
});

test("clearing a provider removes every one of its accounts' contacts", (t) => {
  const db = createDb(t);
  if (!db) return;
  db.syncCalendarContacts("google", "[email protected]", [{ email: "[email protected]" }], []);
  db.syncCalendarContacts("google", "[email protected]", [{ email: "[email protected]" }], []);
  db.syncCalendarContacts("microsoft", "[email protected]", [{ email: "[email protected]" }], []);
  db.syncCalendarContacts("microsoft", "[email protected]", [{ email: "[email protected]" }], []);
  db.upsertContacts([{ email: "[email protected]" }], "manual");

  db.clearGoogleCalendarData();
  assert.deepEqual(storedContactEmails(db), [
    "[email protected]",
    "[email protected]",
    "[email protected]",
  ]);
  db.clearMicrosoftCalendarData();
  assert.deepEqual(storedContactEmails(db), ["[email protected]"]);
  db.db.close();
});

test("a hand-added contact stays the user's when a calendar later syncs it", (t) => {
  const db = createDb(t);
  if (!db) return;
  db.saveGoogleCalendars([{ id: "g-cal", summary: "Me" }], "[email protected]");
  db.upsertContacts([{ email: "[email protected]" }], "manual");
  db.syncCalendarContacts("google", "[email protected]", [{ email: "[email protected]" }], []);

  db.removeGoogleAccount("[email protected]");
  assert.deepEqual(storedContactEmails(db), ["[email protected]"]);
  assert.deepEqual(
    db.getContactLookupSources().contacts.map((row) => row.email),
    ["[email protected]"]
  );
  db.db.close();
});

test("a contact another source still has survives a disconnect", (t) => {
  const db = createDb(t);
  if (!db) return;
  db.saveGoogleCalendars([{ id: "g-cal", summary: "Me" }], "[email protected]");
  db.saveMicrosoftCalendars([{ id: "m-cal", summary: "Calendar" }], "[email protected]");
  db.saveAppleCalendars([{ id: "a-cal", title: "Me (Google)" }]);
  const josh = [{ email: "[email protected]", displayName: "Josh" }];
  // Calendar.app mirrors the Google account, and a Microsoft account has him too.
  db.syncCalendarContacts("google", "[email protected]", josh, []);
  db.syncCalendarContacts("apple", null, josh, []);
  db.syncCalendarContacts("microsoft", "[email protected]", josh, []);
  const lookup = () => db.getContactLookupSources().contacts.map((row) => row.email);

  db.clearAppleCalendarData();
  assert.deepEqual(lookup(), ["[email protected]"]);
  db.removeMicrosoftAccount("[email protected]");
  assert.deepEqual(lookup(), ["[email protected]"]);
  db.removeGoogleAccount("[email protected]");
  assert.deepEqual(storedContactEmails(db), []);
  db.db.close();
});

test("picking a stored contact for a note doesn't make it a hand-added one", (t) => {
  const db = createDb(t);
  if (!db) return;
  db.saveGoogleCalendars([{ id: "g-cal", summary: "Me" }], "[email protected]");
  db.syncCalendarContacts("google", "[email protected]", [{ email: "[email protected]" }], []);
  db.db
    .prepare("INSERT INTO contacts (email, display_name) VALUES (?, ?)")
    .run("[email protected]", "Legacy");

  db.addManualContact({ email: "[email protected]", displayName: "Synced" });
  db.addManualContact({ email: "[email protected]", displayName: null });
  db.addManualContact({ email: "[email protected]", displayName: null });

  const lookup = () =>
    db
      .getContactLookupSources()
      .contacts.map((row) => row.email)
      .sort();
  assert.deepEqual(lookup(), ["[email protected]", "[email protected]"]);
  db.removeGoogleAccount("[email protected]");
  assert.deepEqual(storedContactEmails(db), ["[email protected]", "[email protected]"]);
  assert.deepEqual(lookup(), ["[email protected]"]);
  db.db.close();
});

test("a sync never purges a hand-added address", (t) => {
  const db = createDb(t);
  if (!db) return;
  db.addManualContact({ email: "[email protected]", displayName: "Board" });
  db.syncCalendarContacts("google", "[email protected]", [{ email: "[email protected]" }], []);

  db.syncCalendarContacts("google", "[email protected]", [], ["[email protected]", "[email protected]"]);

  assert.deepEqual(storedContactEmails(db), ["[email protected]"]);
  db.db.close();
});

test("a room any stored event flags is excluded, even where it isn't flagged", (t) => {
  const db = createDb(t);
  if (!db) return;
  db.saveGoogleCalendars(
    [{ id: "[email protected]", summary: "Me", is_primary: true }],
    "[email protected]"
  );
  const event = (id, attendees, extra = {}) => ({
    id,
    calendar_id: "[email protected]",
    provider: "google",
    start_time: new Date().toISOString(),
    end_time: new Date().toISOString(),
    is_all_day: false,
    status: "confirmed",
    attendees_count: attendees.length,
    attendees: JSON.stringify(attendees),
    ...extra,
  });
  db.upsertCalendarEvents([
    // Stored by an older build, before rooms were flagged.
    event("legacy", [{ email: "[email protected]", displayName: "Lincoln Room" }]),
    event(
      "declined",
      [{ email: "[email protected]", displayName: "Lincoln Room", resource: true }],
      { self_response_status: "declined" }
    ),
  ]);

  assert.deepEqual(db.getContactLookupSources().excludedEmails, [
    "[email protected]",
    "[email protected]",
  ]);

  // One unreadable attendee list must not blank the whole lookup.
  db.db.prepare("UPDATE calendar_events SET attendees = 'not json' WHERE id = 'legacy'").run();
  assert.deepEqual(db.getContactLookupSources().excludedEmails, [
    "[email protected]",
    "[email protected]",
  ]);
  db.db.close();
});

test("a Microsoft account keeps its tenant and aliases until it's disconnected", (t) => {
  const db = createDb(t);
  if (!db) return;
  const tokens = {
    microsoft_email: "[email protected]",
    access_token: "access",
    refresh_token: "refresh",
    expires_at: Date.now() + 60_000,
    scope: "scope",
  };
  db.saveMicrosoftTokens({ ...tokens, tenant_id: "tenant-1" });
  // A refresh without an id_token carries no tenant.
  db.saveMicrosoftTokens(tokens);
  db.saveMicrosoftOwnAddresses("[email protected]", ["[email protected]"]);
  db.saveMicrosoftCalendars([{ id: "m-cal", summary: "Calendar" }], "[email protected]");

  assert.deepEqual(db.getMicrosoftAccounts(), [{ email: "[email protected]", tenantId: "tenant-1" }]);
  assert.deepEqual(db.getMicrosoftOwnAddresses("[email protected]"), ["[email protected]"]);
  assert.deepEqual(db.getContactLookupSources().excludedEmails, [
    "[email protected]",
    "[email protected]",
  ]);
  db.removeMicrosoftAccount("[email protected]");
  assert.deepEqual(db.getContactLookupSources().excludedEmails, []);
  db.db.close();
});

test("contact sources from pre-release builds move to contact_sources", (t) => {
  const db = createDb(t);
  if (!db) return;
  db.db.exec("ALTER TABLE contacts ADD COLUMN source TEXT");
  db.db
    .prepare("INSERT INTO contacts (email, display_name, source) VALUES (?, ?, ?)")
    .run("[email protected]", "Josh", "google:[email protected]");
  db.db.close();

  const reopened = new DatabaseManager();
  assert.deepEqual(reopened.db.prepare("SELECT email, source FROM contact_sources").all(), [
    { email: "[email protected]", source: "google:[email protected]" },
  ]);
  assert.equal(reopened.db.prepare("SELECT source FROM contacts").get().source, null);
  reopened.db.close();
});

test("upgrading to user_version 3 forces a full calendar re-sync", (t) => {
  const db = createDb(t);
  if (!db) return;
  db.saveGoogleCalendars([{ id: "primary", summary: "Chad" }], "[email protected]");
  db.saveMicrosoftCalendars([{ id: "work", summary: "Calendar" }], "[email protected]");
  db.updateCalendarSyncToken("primary", "google-token", Date.now() + 60_000);
  db.updateMicrosoftCalendarSyncToken("work", "microsoft-token", Date.now() + 60_000);
  db.db.pragma("user_version = 2");
  db.db.close();

  // Same user data directory: the next launch runs the migration.
  const reopened = new DatabaseManager();
  const tokens = reopened.db
    .prepare(
      "SELECT sync_token FROM google_calendars UNION ALL SELECT sync_token FROM microsoft_calendars"
    )
    .all();
  assert.deepEqual(tokens, [{ sync_token: null }, { sync_token: null }]);
  // Reopening also applies subsequent migrations.
  assert.equal(reopened.db.pragma("user_version", { simple: true }), 5);
  reopened.db.close();
});