Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

4.6 KB
Raw Blame
import assert from 'node:assert/strict';
import type { Browser } from 'webdriverio';
import { fixtureId, USER_ID } from './fixtures/mail';

/** Read-only native-cache diagnostics; never seed the cache or replace IPC. */
export async function invokeNative(
  browser: Browser,
  command: string,
  args: Record<string, unknown>
) {
  return browser.execute(
    async (command, args) => {
      const tauri = (
        window as unknown as {
          __TAURI_INTERNALS__?: {
            invoke: (
              command: string,
              args: Record<string, unknown>
            ) => Promise<unknown>;
          };
        }
      ).__TAURI_INTERNALS__;
      if (!tauri) throw new Error('Not a native Tauri webview');
      return await tauri.invoke(command, args);
    },
    command,
    args
  );
}

export async function checkpoints(browser: Browser) {
  return browser.execute(
    (userId) =>
      Object.keys(localStorage)
        .filter(
          (key) =>
            key.startsWith('graphql-soup-backfill:') && key.includes(userId)
        )
        .map((key) => ({
          key,
          ...(JSON.parse(localStorage.getItem(key) ?? '{}') as {
            completed: boolean;
            pagesFetched: number;
          }),
        })),
    USER_ID
  );
}

const METADATA_LANES = [
  ':email-signal-filter-metadata',
  ':email-noise-filter-metadata',
];

export async function waitForBackfill(browser: Browser, pages: number) {
  await browser.waitUntil(
    async () => {
      const lanes = (await checkpoints(browser)).filter((checkpoint) =>
        METADATA_LANES.some((suffix) => checkpoint.key.endsWith(suffix))
      );
      return (
        lanes.length === METADATA_LANES.length &&
        lanes.every((lane) => lane.completed) &&
        lanes.reduce((total, lane) => total + lane.pagesFetched, 0) === pages
      );
    },
    {
      timeout: 120_000,
      timeoutMsg: `Real metadata backfill did not checkpoint all ${pages} pages`,
    }
  );
}

export async function assertNativeRecords(browser: Browser) {
  const result = (await invokeNative(
    browser,
    'graphql_cache_read_records_by_keys',
    {
      document:
        'fragment NativeE2ERecord on GraphqlSoupEmailThread { id name isRead inboxVisible }',
      fragmentName: 'NativeE2ERecord',
      keys: [4, 6, 8, 9, 10, 12].map(
        (n) => `GraphqlSoupEmailThread:${fixtureId(n)}`
      ),
    }
  )) as { records: { recordKey: string; record: { id: string } | null }[] };
  assert.equal(result.records.length, 6);
  assert.deepEqual(
    result.records.map((record) => record.record?.id).sort(),
    [4, 6, 8, 9, 10, 12].map(fixtureId).sort()
  );
}

/** Assert exact row identities rather than counts or cached labels alone. */
export async function expectRows(browser: Browser, numbers: number[]) {
  const expected = numbers.map(fixtureId).sort();
  await browser.waitUntil(
    async () => {
      const actual = await browser.execute(() =>
        [...document.querySelectorAll('[data-entity-id]')]
          .map((element) => element.getAttribute('data-entity-id'))
          .filter(
            (id): id is string =>
              id?.startsWith('00000000-0000-0000-0000-') ?? false
          )
      );
      return (
        JSON.stringify([...new Set(actual)].sort()) === JSON.stringify(expected)
      );
    },
    {
      timeout: 15_000,
      timeoutMsg: `Expected visible mail rows ${numbers.join(', ')} from the native cache.`,
    }
  );
}

/** Exercise the same native HTTP transport as the app, not the runner's fetch. */
export async function nativeHttpReachable(browser: Browser, origin: string) {
  return browser.execute(async (origin) => {
    const modulePath = '/src/lib/core/util/platformFetch.ts';
    const { platformFetch }: { platformFetch: typeof fetch } = await import(
      modulePath
    );
    try {
      return (
        await platformFetch(`${origin}/health`, {
          signal: AbortSignal.timeout(2000),
        })
      ).ok;
    } catch {
      return false;
    }
  }, origin);
}

export async function selectTab(browser: Browser, label: 'Noise' | 'All') {
  const tab = browser.$(
    `//nav[@aria-label="Email tabs"]//button[.//span[normalize-space(.)="${label}"]]`
  );
  // WebKitWebDriver's pointer clicks can hit the adjacent row in this Tauri
  // window. Activate the labeled button's real UI handler, not query/cache state.
  await tab.waitForDisplayed({ timeout: 5000 });
  await tab.execute((element) => element.click());
  await browser.waitUntil(
    async () => (await tab.getAttribute('aria-current')) === 'page',
    {
      timeout: 5000,
      timeoutMsg: `${label} tab did not become selected`,
    }
  );
}