Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

31.1 KB
Raw Blame
/**
 * op-source — the lazy 1Password seam behind the credential authority.
 *
 * These tests cover op-source's UNIQUE contracts:
 *  1. the SYNC laziness gate (hasOpSources) and the short-circuits in
 *     resolveOpKeyForEnvVars that return BEFORE any 1Password module/SDK is
 *     touched;
 *  2. the GLOB SINGLE-FLIGHT: one configured glob resolves ONCE per process
 *     (one vaults.list + items.list + items.get; values come from that
 *     discovery, so NO secrets.resolveAll) no matter how many providers ask,
 *     failures are never memoized, and two different globs never cross-contaminate.
 *  3. DIALOG SCOPING: a run authorizes only the declared 1Password accounts
 *     whose sources it must actually inspect to satisfy the wanted key set.
 *
 * The deep resolution primitives (collectConfigImports / resolveGlobImportAll /
 * resolveSecrets) are exhaustively tested in providers/onepassword.test.ts; the
 * full resolve path against real 1Password is exercised by the manual
 * scratch-op-hydrate-check probe.
 *
 * Deliberately NO `mock.module` here: Bun's module mocks are process-global and
 * would bleed into sibling files (providers/onepassword.test.ts, the lazy test)
 * in a full `bun test` run. Everything is hermetic via the injectable
 * __setOpSourceSeamsForTests seams (in-memory config + fake SDK factory + stub
 * auth), mirroring onepassword.test.ts's SdkClientFactory idiom.
 */

import { afterEach, beforeEach, describe, expect, it, spyOn } from "bun:test";
import type { SdkAuth, SdkClientFactory, SdkClientLike } from "../../providers/onepassword.js";
import {
  getOpFailures,
  resetOpFailures,
  setAppLockProbe,
  setLockRetryTiming,
  setPeerLockProbe,
  setScreenLockProbe,
} from "../../providers/onepassword.js";
import {
  __configureStartupTraceForTests,
  __getStartupSpansForTests,
  __resetStartupTraceForTests,
} from "../../startup-trace.js";
import {
  OP_UNAVAILABLE_ENV,
  __resetOpUnavailableForTests,
  __resetResolveCacheForTests,
  __resetSdkAuthForTests,
  __resetSniffForTests,
  __resetWarnOnceForTests,
  __setOpSourceSeamsForTests,
  getOpUnavailableVars,
  hasOpSources,
  invalidateOpResolutionCache,
  recordOpUnavailableVars,
  resolveOpKeyForEnvVars,
} from "./op-source.js";

let savedArgv: string[];
let savedDisableOp: string | undefined;
let savedOpUnavailable: string | undefined;
let savedOpServiceAccountToken: string | undefined;

beforeEach(() => {
  savedArgv = process.argv;
  // The glob tests need the real sniff path (CLAUDISH_DISABLE_OP would force
  // hasOpSources()=false and short-circuit before the seams are consulted).
  savedDisableOp = process.env.CLAUDISH_DISABLE_OP;
  delete process.env.CLAUDISH_DISABLE_OP;
  savedOpUnavailable = process.env[OP_UNAVAILABLE_ENV];
  delete process.env[OP_UNAVAILABLE_ENV];
  savedOpServiceAccountToken = process.env.OP_SERVICE_ACCOUNT_TOKEN;
  delete process.env.OP_SERVICE_ACCOUNT_TOKEN;
  __resetSdkAuthForTests();
  __resetSniffForTests();
  __resetWarnOnceForTests();
  invalidateOpResolutionCache();
  __resetResolveCacheForTests();
  __resetOpUnavailableForTests();
  resetOpFailures();
  __setOpSourceSeamsForTests(undefined);
  setLockRetryTiming({ seconds: 0.01, tickMs: 1 });
  setScreenLockProbe(() => false);
  setAppLockProbe(() => false);
  setPeerLockProbe(() => false);
});

afterEach(() => {
  process.argv = savedArgv;
  if (savedDisableOp === undefined) delete process.env.CLAUDISH_DISABLE_OP;
  else process.env.CLAUDISH_DISABLE_OP = savedDisableOp;
  if (savedOpUnavailable === undefined) delete process.env[OP_UNAVAILABLE_ENV];
  else process.env[OP_UNAVAILABLE_ENV] = savedOpUnavailable;
  if (savedOpServiceAccountToken === undefined) delete process.env.OP_SERVICE_ACCOUNT_TOKEN;
  else process.env.OP_SERVICE_ACCOUNT_TOKEN = savedOpServiceAccountToken;
  __resetSdkAuthForTests();
  __resetSniffForTests();
  __resetWarnOnceForTests();
  invalidateOpResolutionCache();
  __resetResolveCacheForTests();
  __resetOpUnavailableForTests();
  resetOpFailures();
  __setOpSourceSeamsForTests(undefined);
  setLockRetryTiming();
  setScreenLockProbe(undefined);
  setAppLockProbe(undefined);
  setPeerLockProbe(undefined);
});

describe("hasOpSources() — the sync laziness gate", () => {
  // The no-flag case reads the real ~/.claudish/config.json (the sniff reads the
  // file directly), so we don't assert "false" — it depends on the host's config.
  // The argv-flag cases ARE deterministic regardless of host config.

  it("is true with --op <glob>", () => {
    process.argv = ["bun", "index.ts", "--op", "op://V/Item/**"];
    __resetSniffForTests();
    expect(hasOpSources()).toBe(true);
  });

  it("is true with --op=<glob> inline form", () => {
    process.argv = ["bun", "index.ts", "--op=op://V/Item/**"];
    __resetSniffForTests();
    expect(hasOpSources()).toBe(true);
  });

  it("is true with --op-env <id>", () => {
    process.argv = ["bun", "index.ts", "--op-env", "env-1"];
    __resetSniffForTests();
    expect(hasOpSources()).toBe(true);
  });

  it("is true with --op-env=<id> inline form", () => {
    process.argv = ["bun", "index.ts", "--op-env=env-1"];
    __resetSniffForTests();
    expect(hasOpSources()).toBe(true);
  });

  it("honors CLAUDISH_DISABLE_OP after the source sniff is memoized", () => {
    process.argv = ["bun", "index.ts", "--op", "op://V/Item/**"];
    __resetSniffForTests();
    expect(hasOpSources()).toBe(true);

    // A test that only sets the env var up-front does not cover this — it would
    // pass against the old buggy code too.
    process.env.CLAUDISH_DISABLE_OP = "1";
    expect(hasOpSources()).toBe(false);
  });
});

describe("resolveOpKeyForEnvVars() — short-circuits (no SDK touched)", () => {
  it("returns {} for an empty wanted set, even WITH an op source present", async () => {
    // A keyless/satisfied model produces an empty wanted set. This is the
    // "ollama@ / already-satisfied key" laziness case: no SDK, no resolution.
    process.argv = ["bun", "index.ts", "--op", "op://V/Item/**"];
    __resetSniffForTests();
    expect(hasOpSources()).toBe(true);
    const out = await resolveOpKeyForEnvVars(new Set(), { onAuthFailure: "skip" });
    expect(out).toEqual({});
  });
});

describe("CLAUDISH_OP_UNAVAILABLE skip-list", () => {
  it("records sorted, deduplicated non-empty names and resets them", () => {
    recordOpUnavailableVars(["ZHIPU_API_KEY", "", "OPENAI_API_KEY", "ZHIPU_API_KEY"]);

    expect(getOpUnavailableVars()).toEqual(["OPENAI_API_KEY", "ZHIPU_API_KEY"]);

    __resetOpUnavailableForTests();
    expect(getOpUnavailableVars()).toEqual([]);
  });

  it("skips the SDK when every wanted name was inherited as unavailable", async () => {
    let sdkFactoryCalls = 0;
    const factory: SdkClientFactory = async () => {
      sdkFactoryCalls++;
      throw new Error("SDK factory must not run for inherited unavailable vars");
    };
    seamWithEnvironment(factory);
    process.env[OP_UNAVAILABLE_ENV] = " SKIPPED_A_KEY,SKIPPED_B_KEY, ";

    const out = await resolveOpKeyForEnvVars(new Set(["SKIPPED_A_KEY", "SKIPPED_B_KEY"]), {
      onAuthFailure: "skip",
    });

    expect(out).toEqual({});
    expect(sdkFactoryCalls).toBe(0);
  });

  it("still resolves a wanted name that is not in the inherited skip-list", async () => {
    const { factory, counts } = makeCountingEnvironmentFactory();
    seamWithEnvironment(factory);
    process.env[OP_UNAVAILABLE_ENV] = "OPENROUTER_API_KEY";

    const out = await resolveOne("GEMINI_API_KEY");

    expect(out).toEqual({ GEMINI_API_KEY: "sk-test-gemini" });
    expect(counts).toEqual({ getVariables: 1, environmentIds: [ENVIRONMENT_ID] });
  });

  it("records a clean missing name as unavailable", async () => {
    const { factory } = makeCountingEnvironmentFactory();
    seamWithEnvironment(factory);

    expect(await resolveOne("CLEAN_MISSING_KEY")).toEqual({});
    expect(getOpUnavailableVars()).toEqual(["CLEAN_MISSING_KEY"]);
    expect(getOpFailures()).toEqual([]);
  });

  it("records a source failure instead of publishing an auth-failure miss", async () => {
    const denial = "Denied authorization for SDK client (test)";
    let sdkFactoryCalls = 0;
    seamWithEnvironment(async () => {
      sdkFactoryCalls++;
      throw new Error(denial);
    });

    const errorSpy = spyOn(console, "error").mockImplementation(() => {});
    let messages: string[] = [];
    try {
      expect(await resolveOne("AUTH_FAILURE_KEY")).toEqual({});
      messages = errorSpy.mock.calls.map(([message]) => String(message));
    } finally {
      errorSpy.mockRestore();
    }

    expect(getOpUnavailableVars()).toEqual(["AUTH_FAILURE_KEY"]);
    expect(sdkFactoryCalls).toBe(1);
    expect(getOpFailures()).toHaveLength(1);
    expect(getOpFailures()[0]?.message).toBe(denial);
    expect(messages).toContain(
      "[claudish] 1Password environment skipped: " +
        "the 1Password approval prompt was dismissed (or never answered)"
    );
    expect(process.env[OP_UNAVAILABLE_ENV]).toBeUndefined();
  });
});

// ===========================================================================
// Glob single-flight + full-result memoization (the 36s-startup fix)
//
// Fixture DERIVED from providers/onepassword.test.ts's real-captured item (same
// vault/item/section/field titles — a compact subset; no invented secret-like
// data). The fake SdkClientFactory counts every SDK namespace call so the tests
// can assert "exactly ONE discovery, and NO resolveAll" for N providers (the
// glob path reads values from discovery, not a second title-based resolve).
// ===========================================================================

const VAULT = "Jack";
const ITEM = "AI LLM models API keys 10xlabs";
const GLOB_ALL = `op://${VAULT}/${ITEM}/**`;
const GLOB_OPENAI = `op://${VAULT}/${ITEM}/OpenAI/*`;
const GLOB_MOON = `op://${VAULT}/${ITEM}/Moonshot Kimi/*`;

/** SDK-shaped item (subset of the captured fixture — same titles/sections). */
const SDK_ITEM: Awaited<ReturnType<SdkClientLike["items"]["get"]>> = {
  id: "abc123",
  title: ITEM,
  sections: [
    { id: "s-oai", title: "OpenAI" },
    { id: "s-moon", title: "Moonshot Kimi" },
    { id: "s-glm", title: "GLM Z models" },
  ],
  fields: [
    // sectionless, lowercase → invalid env name → skipped by the glob filter.
    { id: "f-0", title: "username", fieldType: "STRING", value: "" },
    { id: "f-1", title: "OPENAI_API_KEY", sectionId: "s-oai", fieldType: "CONCEALED", value: "x" },
    {
      id: "f-2",
      title: "MOONSHOT_API_KEY",
      sectionId: "s-moon",
      fieldType: "CONCEALED",
      value: "x",
    },
    {
      id: "f-3",
      title: "KIMI_CODING_API_KEY",
      sectionId: "s-moon",
      fieldType: "CONCEALED",
      value: "x",
    },
    { id: "f-4", title: "ZHIPU_API_KEY", sectionId: "s-glm", fieldType: "CONCEALED", value: "x" },
  ],
};

interface SdkCallCounts {
  vaultsList: number;
  itemsList: number;
  itemsGet: number;
  resolveAll: number;
}

/**
 * A counting fake SDK client factory (mirrors onepassword.test.ts's
 * makeFakeSdkFactory, plus per-namespace call counters). `failVaultsList` is a
 * one-shot latch: when active, the NEXT vaults.list throws (a transient
 * desktop-app failure), then the latch clears.
 */
function makeCountingFactory(opts: { failVaultsList?: { active: boolean } } = {}): {
  factory: SdkClientFactory;
  counts: SdkCallCounts;
} {
  const counts: SdkCallCounts = { vaultsList: 0, itemsList: 0, itemsGet: 0, resolveAll: 0 };
  const client: SdkClientLike = {
    secrets: {
      async resolve(ref: string): Promise<string> {
        return `sdk:${ref}`;
      },
      async resolveAll(refs: string[]) {
        counts.resolveAll++;
        const individualResponses: Record<string, { content?: { secret: string } }> = {};
        for (const r of refs) individualResponses[r] = { content: { secret: `sdk:${r}` } };
        return { individualResponses };
      },
    },
    vaults: {
      async list() {
        counts.vaultsList++;
        if (opts.failVaultsList?.active) {
          opts.failVaultsList.active = false;
          throw new Error("1Password desktop bridge failed (test)");
        }
        return [{ id: "vid", title: VAULT }];
      },
    },
    items: {
      async list() {
        counts.itemsList++;
        return [{ id: "abc123", title: ITEM }];
      },
      async get() {
        counts.itemsGet++;
        return SDK_ITEM;
      },
    },
    environments: {
      async getVariables() {
        return { variables: [] };
      },
    },
  };
  return { factory: async () => client, counts };
}

const stubAuth: SdkAuth = { kind: "token", token: "ops_test" };

/** Install the hermetic seams: in-memory config + counting SDK + stub auth. */
function seamWith(globs: string[], factory: SdkClientFactory): void {
  __setOpSourceSeamsForTests({
    config: { onepassword: globs },
    sdkFactory: factory,
    auth: stubAuth,
  });
  __resetSniffForTests(); // re-sniff against the seamed config
}

const resolveOne = (name: string): Promise<Record<string, string>> =>
  resolveOpKeyForEnvVars(new Set([name]), { onAuthFailure: "skip" });

describe("glob single-flight — one resolution shared by every provider", () => {
  it("N concurrent per-key resolves over ONE glob → exactly one vaults.list/items.list/items.get/resolveAll", async () => {
    const { factory, counts } = makeCountingFactory();
    seamWith([GLOB_ALL], factory);

    // Six concurrent "providers" (incl. a duplicate and a key the item lacks) —
    // the startup shape: everyone enqueues before the first resolution lands.
    const [openai, moonshot, kimi, zhipu, missing, openaiAgain] = await Promise.all([
      resolveOne("OPENAI_API_KEY"),
      resolveOne("MOONSHOT_API_KEY"),
      resolveOne("KIMI_CODING_API_KEY"),
      resolveOne("ZHIPU_API_KEY"),
      resolveOne("NOT_IN_THIS_ITEM_KEY"),
      resolveOne("OPENAI_API_KEY"),
    ]);

    // ONE full discovery, and NO resolveAll — values come from discovery now.
    expect(counts).toEqual({ vaultsList: 1, itemsList: 1, itemsGet: 1, resolveAll: 0 });

    // Every caller got ITS value out of the shared result (the discovered value).
    expect(openai).toEqual({ OPENAI_API_KEY: "x" });
    expect(moonshot).toEqual({ MOONSHOT_API_KEY: "x" });
    expect(kimi).toEqual({ KIMI_CODING_API_KEY: "x" });
    expect(zhipu).toEqual({ ZHIPU_API_KEY: "x" });
    expect(missing).toEqual({}); // the glob simply doesn't hold this key
    expect(openaiAgain).toEqual(openai);
  });

  it("sequential resolves after completion → pure cache hits, zero additional SDK calls", async () => {
    const { factory, counts } = makeCountingFactory();
    seamWith([GLOB_ALL], factory);

    await resolveOne("OPENAI_API_KEY");
    expect(counts.itemsGet).toBe(1);

    // A var NEVER wanted before — but the full-glob result already holds it.
    const moonshot = await resolveOne("MOONSHOT_API_KEY");
    expect(moonshot).toEqual({ MOONSHOT_API_KEY: "x" });
    // A var the glob does NOT hold → memoized empty pick, still no SDK.
    expect(await resolveOne("NOT_IN_THIS_ITEM_KEY")).toEqual({});

    expect(counts).toEqual({ vaultsList: 1, itemsList: 1, itemsGet: 1, resolveAll: 0 });
  });

  it("a FAILED glob resolution is not cached — the next resolve retries", async () => {
    const failLatch = { active: true };
    const { factory, counts } = makeCountingFactory({ failVaultsList: failLatch });
    seamWith([GLOB_ALL], factory);

    // First resolve: discovery throws → warn+skip (startup contract), {} back.
    expect(await resolveOne("OPENAI_API_KEY")).toEqual({});
    expect(counts.vaultsList).toBe(1);
    expect(counts.itemsGet).toBe(0);

    // Second resolve: the rejected promise was EVICTED → full retry, succeeds.
    const out = await resolveOne("OPENAI_API_KEY");
    expect(out).toEqual({ OPENAI_API_KEY: "x" });
    expect(counts).toEqual({ vaultsList: 2, itemsList: 1, itemsGet: 1, resolveAll: 0 });
  });

  it("two DIFFERENT globs → two resolutions, no cross-contamination", async () => {
    const { factory, counts } = makeCountingFactory();
    seamWith([GLOB_OPENAI, GLOB_MOON], factory);

    // Wanted key lives in glob #1 → glob #2 is never touched (loop breaks).
    const openai = await resolveOne("OPENAI_API_KEY");
    expect(openai).toEqual({ OPENAI_API_KEY: "x" });
    expect(counts.itemsGet).toBe(1);

    // Wanted key lives in glob #2 → glob #1 is a memoized miss, #2 resolves.
    const moonshot = await resolveOne("MOONSHOT_API_KEY");
    expect(moonshot).toEqual({ MOONSHOT_API_KEY: "x" });
    expect(counts.itemsGet).toBe(2);

    // A key NEITHER scoped glob matches → both memoized, no more SDK calls.
    expect(await resolveOne("ZHIPU_API_KEY")).toEqual({});
    expect(counts.itemsGet).toBe(2);
    expect(counts.resolveAll).toBe(0);
  });

  it("invalidateOpResolutionCache() forces a fresh discovery on the next resolve", async () => {
    const { factory, counts } = makeCountingFactory();
    seamWith([GLOB_ALL], factory);

    await resolveOne("OPENAI_API_KEY");
    expect(counts.itemsGet).toBe(1);

    // TUI hydrate-on-add / item edited in 1Password → drop every memo.
    invalidateOpResolutionCache();
    __resetSniffForTests(); // invalidate also resets the sniff; re-sniff the seams

    await resolveOne("OPENAI_API_KEY");
    expect(counts.itemsGet).toBe(2);
  });
});

// ===========================================================================
// Environment point-of-need + full-result memoization
//
// A 1Password Environment is fetched only after a concrete provider key reaches
// this resolver. getVariables() returns the whole environment, so one SDK call
// must satisfy later requests for other keys from the process-wide cache.
// ===========================================================================

const ENVIRONMENT_ID = "env-abc";
const ENVIRONMENT_VARIABLES = [
  { name: "OPENROUTER_API_KEY", value: "sk-test-router", masked: true },
  { name: "GEMINI_API_KEY", value: "sk-test-gemini", masked: true },
];

interface EnvironmentCallCounts {
  getVariables: number;
  environmentIds: string[];
}

function makeCountingEnvironmentFactory(): {
  factory: SdkClientFactory;
  counts: EnvironmentCallCounts;
} {
  const counts: EnvironmentCallCounts = { getVariables: 0, environmentIds: [] };
  const client: SdkClientLike = {
    secrets: {
      async resolve(ref: string): Promise<string> {
        return `sdk:${ref}`;
      },
      async resolveAll() {
        return { individualResponses: {} };
      },
    },
    vaults: {
      async list() {
        return [];
      },
    },
    items: {
      async list() {
        return [];
      },
      async get() {
        throw new Error("environment tests must not inspect items");
      },
    },
    environments: {
      async getVariables(id: string) {
        counts.getVariables++;
        counts.environmentIds.push(id);
        return { variables: ENVIRONMENT_VARIABLES };
      },
    },
  };
  return { factory: async () => client, counts };
}

function seamWithEnvironment(factory: SdkClientFactory): void {
  const config = {
    onepassword: [],
    onepasswordEnvironments: [ENVIRONMENT_ID],
  };
  __setOpSourceSeamsForTests({
    config,
    sdkFactory: factory,
    auth: stubAuth,
  });
  __resetSniffForTests();
}

describe("environment point-of-need", () => {
  it("resolves a wanted key with exactly one environment fetch", async () => {
    const { factory, counts } = makeCountingEnvironmentFactory();
    seamWithEnvironment(factory);

    const out = await resolveOne("OPENROUTER_API_KEY");

    expect(out).toEqual({ OPENROUTER_API_KEY: "sk-test-router" });
    expect(counts).toEqual({ getVariables: 1, environmentIds: [ENVIRONMENT_ID] });
  });

  it("caches the whole environment for a later provider key", async () => {
    const { factory, counts } = makeCountingEnvironmentFactory();
    seamWithEnvironment(factory);

    expect(await resolveOne("OPENROUTER_API_KEY")).toEqual({
      OPENROUTER_API_KEY: "sk-test-router",
    });
    expect(await resolveOne("GEMINI_API_KEY")).toEqual({
      GEMINI_API_KEY: "sk-test-gemini",
    });
    expect(counts).toEqual({ getVariables: 1, environmentIds: [ENVIRONMENT_ID] });
  });

  it("does not fetch a configured environment for a no-key operation", async () => {
    const { factory, counts } = makeCountingEnvironmentFactory();
    seamWithEnvironment(factory);

    const out = await resolveOpKeyForEnvVars(new Set(), { onAuthFailure: "skip" });

    expect(out).toEqual({});
    expect(counts).toEqual({ getVariables: 0, environmentIds: [] });
  });

  it("fetches once and returns {} when the environment lacks the wanted key", async () => {
    const { factory, counts } = makeCountingEnvironmentFactory();
    seamWithEnvironment(factory);

    const out = await resolveOne("MISSING_KEY");

    expect(out).toEqual({});
    expect(counts).toEqual({ getVariables: 1, environmentIds: [ENVIRONMENT_ID] });
  });
});

// ===========================================================================
// Desktop approval dialog scoping
//
// The fake factory records the DesktopAuth account used for every client build.
// Distinct accounts are therefore the approval dialogs a real run would raise.
// The elapsed-time guard makes these tests fail if the hermetic seam is bypassed
// and a real `op`/SDK path turns a microtask-only case into a seconds-long probe.
// ===========================================================================

const DIALOG_ACCOUNT_A = "acctA.1password.com";
const DIALOG_ACCOUNT_B = "acctB.1password.com";
const DIALOG_ENV_A = "envA";
const DIALOG_ENV_B = "envB";
const DIALOG_REF_A = "op://V/I/KEY_A";
const DIALOG_REF_B = "op://V/I/KEY_B";
const DIALOG_BUDGET_MS = 100;

interface DialogVariable {
  name: string;
  value: string;
  masked: boolean;
}

function makeDialogScopedFactory(opts: {
  environments?: Record<string, DialogVariable[]>;
  refs?: Record<string, string>;
}): {
  factory: SdkClientFactory;
  authorizedAccounts: string[];
} {
  const authorizedAccounts: string[] = [];
  const factory: SdkClientFactory = async (auth) => {
    if (auth.kind !== "desktop") {
      throw new Error("dialog-scoping tests require declared DesktopAuth accounts");
    }
    authorizedAccounts.push(auth.accountName);
    return {
      secrets: {
        async resolve(ref: string): Promise<string> {
          const value = opts.refs?.[ref];
          if (value === undefined) throw new Error(`unexpected ref: ${ref}`);
          return value;
        },
        async resolveAll(refs: string[]) {
          const individualResponses: Record<string, { content?: { secret: string } }> = {};
          for (const ref of refs) {
            const value = opts.refs?.[ref];
            if (value !== undefined) individualResponses[ref] = { content: { secret: value } };
          }
          return { individualResponses };
        },
      },
      vaults: {
        async list() {
          return [];
        },
      },
      items: {
        async list() {
          return [];
        },
        async get() {
          throw new Error("dialog-scoping tests must not inspect items");
        },
      },
      environments: {
        async getVariables(id: string) {
          return { variables: opts.environments?.[id] ?? [] };
        },
      },
    };
  };
  return { factory, authorizedAccounts };
}

function seamWithDialogSources(
  config: {
    onepassword?: { ref: string; account: string }[];
    onepasswordEnvironments?: { id: string; account: string }[];
  },
  factory: SdkClientFactory
): void {
  __setOpSourceSeamsForTests({ config, sdkFactory: factory, auth: stubAuth });
  __resetSniffForTests();
}

async function resolveWithinDialogBudget(names: string[]): Promise<Record<string, string>> {
  const startedAt = performance.now();
  const out = await resolveOpKeyForEnvVars(new Set(names), { onAuthFailure: "skip" });
  expect(performance.now() - startedAt).toBeLessThan(DIALOG_BUDGET_MS);
  return out;
}

function distinctAccounts(accounts: string[]): string[] {
  return [...new Set(accounts)];
}

describe("Desktop approval dialog scoping", () => {
  // HEADLINE REGRESSION: configuring account B must not cost a second dialog
  // when account A already satisfies the run. This holds because the environment
  // loop breaks on SATISFACTION (`stillWantedEnv.size === 0`), not exhaustion.
  it("a one-account run authorizes exactly that one configured account", async () => {
    const { factory, authorizedAccounts } = makeDialogScopedFactory({
      environments: {
        [DIALOG_ENV_A]: [{ name: "KEY_A", value: "value-a", masked: true }],
        [DIALOG_ENV_B]: [{ name: "KEY_B", value: "value-b", masked: true }],
      },
    });
    seamWithDialogSources(
      {
        onepasswordEnvironments: [
          { id: DIALOG_ENV_A, account: DIALOG_ACCOUNT_A },
          { id: DIALOG_ENV_B, account: DIALOG_ACCOUNT_B },
        ],
      },
      factory
    );

    expect(await resolveWithinDialogBudget(["KEY_A"])).toEqual({ KEY_A: "value-a" });
    expect(distinctAccounts(authorizedAccounts)).toEqual([DIALOG_ACCOUNT_A]);
  });

  // REGRESSION: dialog minimization must not skip an account that genuinely owns
  // another wanted key; two required accounts correctly cost two dialogs.
  it("a two-account run authorizes both accounts and resolves both keys", async () => {
    const { factory, authorizedAccounts } = makeDialogScopedFactory({
      environments: {
        [DIALOG_ENV_A]: [{ name: "KEY_A", value: "value-a", masked: true }],
        [DIALOG_ENV_B]: [{ name: "KEY_B", value: "value-b", masked: true }],
      },
    });
    seamWithDialogSources(
      {
        onepasswordEnvironments: [
          { id: DIALOG_ENV_A, account: DIALOG_ACCOUNT_A },
          { id: DIALOG_ENV_B, account: DIALOG_ACCOUNT_B },
        ],
      },
      factory
    );

    expect(await resolveWithinDialogBudget(["KEY_A", "KEY_B"])).toEqual({
      KEY_A: "value-a",
      KEY_B: "value-b",
    });
    expect(distinctAccounts(authorizedAccounts)).toEqual([DIALOG_ACCOUNT_A, DIALOG_ACCOUNT_B]);
  });

  // REGRESSION: preserve the IRREDUCIBLE ordering cost for opaque environments.
  // This is not a bug: fetching is the only way to learn their contents, and it
  // authorizes the account, so user-controlled source ordering is the only lever.
  it("authorizes an opaque earlier environment before the one holding the key", async () => {
    const { factory, authorizedAccounts } = makeDialogScopedFactory({
      environments: {
        [DIALOG_ENV_A]: [{ name: "KEY_A", value: "value-a", masked: true }],
        [DIALOG_ENV_B]: [{ name: "KEY_B", value: "value-b", masked: true }],
      },
    });
    seamWithDialogSources(
      {
        onepasswordEnvironments: [
          { id: DIALOG_ENV_B, account: DIALOG_ACCOUNT_B },
          { id: DIALOG_ENV_A, account: DIALOG_ACCOUNT_A },
        ],
      },
      factory
    );

    expect(await resolveWithinDialogBudget(["KEY_A"])).toEqual({ KEY_A: "value-a" });
    expect(authorizedAccounts).toEqual([DIALOG_ACCOUNT_B, DIALOG_ACCOUNT_A]);
    expect(distinctAccounts(authorizedAccounts)).toHaveLength(2);
  });

  // REGRESSION: a keyless or already-satisfied run (represented here by an empty
  // wanted set) must keep non-1Password users free of desktop approval dialogs.
  it("authorizes no account when the run needs no op key", async () => {
    const { factory, authorizedAccounts } = makeDialogScopedFactory({
      environments: {
        [DIALOG_ENV_A]: [{ name: "KEY_A", value: "value-a", masked: true }],
        [DIALOG_ENV_B]: [{ name: "KEY_B", value: "value-b", masked: true }],
      },
    });
    seamWithDialogSources(
      {
        onepasswordEnvironments: [
          { id: DIALOG_ENV_A, account: DIALOG_ACCOUNT_A },
          { id: DIALOG_ENV_B, account: DIALOG_ACCOUNT_B },
        ],
      },
      factory
    );

    expect(await resolveWithinDialogBudget([])).toEqual({});
    expect(distinctAccounts(authorizedAccounts)).toEqual([]);
  });

  // REGRESSION: single refs must never inherit the opacity cost of environments.
  // A ref names its key, so an account owning an unwanted ref is never touched,
  // even when that unwanted ref appears first in configuration order.
  it("authorizes only the account owning a wanted single ref", async () => {
    for (const onepassword of [
      [
        { ref: DIALOG_REF_A, account: DIALOG_ACCOUNT_A },
        { ref: DIALOG_REF_B, account: DIALOG_ACCOUNT_B },
      ],
      [
        { ref: DIALOG_REF_B, account: DIALOG_ACCOUNT_B },
        { ref: DIALOG_REF_A, account: DIALOG_ACCOUNT_A },
      ],
    ]) {
      __resetSdkAuthForTests();
      __resetSniffForTests();
      __resetWarnOnceForTests();
      invalidateOpResolutionCache();
      __resetResolveCacheForTests();

      const { factory, authorizedAccounts } = makeDialogScopedFactory({
        environments: {
          [DIALOG_ENV_B]: [{ name: "KEY_B", value: "environment-value-b", masked: true }],
        },
        refs: { [DIALOG_REF_A]: "value-a", [DIALOG_REF_B]: "value-b" },
      });
      seamWithDialogSources(
        {
          onepassword,
          onepasswordEnvironments: [{ id: DIALOG_ENV_B, account: DIALOG_ACCOUNT_B }],
        },
        factory
      );

      expect(await resolveWithinDialogBudget(["KEY_A"])).toEqual({ KEY_A: "value-a" });
      expect(distinctAccounts(authorizedAccounts)).toEqual([DIALOG_ACCOUNT_A]);
    }
  });
});

describe("1Password failure warnings", () => {
  it("prints each distinct environment failure message only once across a routing chain", async () => {
    const repeatedFailure = "Denied authorization for SDK client (test)";
    const distinctFailure = "1Password desktop bridge unavailable (test)";
    const client: SdkClientLike = {
      secrets: {
        async resolve(ref: string): Promise<string> {
          return `sdk:${ref}`;
        },
        async resolveAll() {
          return { individualResponses: {} };
        },
      },
      vaults: {
        async list() {
          return [];
        },
      },
      items: {
        async list() {
          return [];
        },
        async get() {
          throw new Error("warning test must not inspect items");
        },
      },
      environments: {
        async getVariables(id: string) {
          throw new Error(id === "env-denied" ? repeatedFailure : distinctFailure);
        },
      },
    };
    __setOpSourceSeamsForTests({
      config: {
        onepassword: [],
        onepasswordEnvironments: ["env-denied", "env-unavailable"],
      },
      sdkFactory: async () => client,
      auth: stubAuth,
    });
    __resetSniffForTests();

    const errorSpy = spyOn(console, "error").mockImplementation(() => {});
    try {
      // A bare model's routing chain asks for several different provider vars.
      // Both environment resolutions fail on every ask; only the first copy of
      // each distinct message should reach stderr.
      for (const envVar of [
        "OPENROUTER_API_KEY",
        "GEMINI_API_KEY",
        "XAI_API_KEY",
        "MISTRAL_API_KEY",
      ]) {
        expect(await resolveOne(envVar)).toEqual({});
      }

      const messages = errorSpy.mock.calls.map(([message]) => String(message));
      const repeatedWarning =
        "[claudish] 1Password environment skipped: " +
        "the 1Password approval prompt was dismissed (or never answered)";
      const distinctWarning = `[claudish] 1Password environment skipped: ${distinctFailure}`;
      expect(messages.filter((message) => message === repeatedWarning)).toHaveLength(1);
      expect(messages.filter((message) => message === distinctWarning)).toHaveLength(1);
      expect(messages).toHaveLength(2);
    } finally {
      errorSpy.mockRestore();
    }
  });
});