// Multi-account integration coverage.
//
// Pins the cross-protocol invariants the unit tier can't reach:
//   - Two accounts (Pixelfed + Mastodon) can coexist in the registry.
//   - `AccountStore.setActive(id)` flips the active pointer atomically.
//   - `activeProtocol()` resolves to the right adapter after a switch.
//   - Query-key scoping (`AccountStore.activeIdOrThrow`) gives each account
//     its own keyspace — closes the regression class where pixelfed-shaped
//     keys leaked into a mastodon active session.
//   - Per-instance OAuth credentials stay distinct per host even though both
//     accounts live in the same keychain.
//
// We don't drive React Query here (the unit tier already pins the key
// shape); we just assert the registry- and adapter-level switching contract.

import { activeProtocol, parseHandle } from 'src/lib/protocol'
import { AccountStore } from 'src/state/accountStore'
import { secureStore } from 'src/state/storage'
import { describeWithBothRigs } from '../_helpers/loadTestRig'
import { __resetIntegrationMocks } from './_setup/jestMocks'
import { seedAccountFromRig } from './_setup/seedAccount'

describeWithBothRigs(
  'multi-account registry + switching (live)',
  ({ pixelfed, mastodon }) => {
    beforeAll(() => {
      __resetIntegrationMocks()
    })

    it('both rigs seed into the registry under distinct accountIds', () => {
      const pSummary = seedAccountFromRig(pixelfed)
      const mSummary = seedAccountFromRig(mastodon)
      expect(pSummary.accountId).not.toBe(mSummary.accountId)
      expect(AccountStore.list()).toHaveLength(2)
      expect(
        AccountStore.findByInstanceAndUserId(pixelfed.instance, pixelfed.userId)?.type
      ).toBe('pixelfed')
      expect(
        AccountStore.findByInstanceAndUserId(mastodon.instance, mastodon.userId)?.type
      ).toBe('mastodon')
    })

    it('per-instance OAuth credentials are keyed independently', () => {
      seedAccountFromRig(pixelfed)
      seedAccountFromRig(mastodon)
      expect(secureStore.getClientId(pixelfed.instance)).toBe(pixelfed.oauthClientId)
      expect(secureStore.getClientId(mastodon.instance)).toBe(mastodon.oauthClientId)
      // No cross-talk: pixelfed host doesn't see mastodon's secret.
      expect(secureStore.getClientSecret(pixelfed.instance)).toBe(
        pixelfed.oauthClientSecret
      )
      expect(secureStore.getClientSecret(mastodon.instance)).toBe(
        mastodon.oauthClientSecret
      )
    })

    it('setActive flips activeProtocol() across the protocol firewall', async () => {
      const pSummary = seedAccountFromRig(pixelfed)
      const mSummary = seedAccountFromRig(mastodon)

      AccountStore.setActive(pSummary.accountId)
      expect(activeProtocol().kind).toBe('pixelfed')
      // Round-trip a real call so we're sure the live wire path follows the
      // active account.
      const pHandle = parseHandle(pixelfed.acct, pixelfed.instance)
      const pLookup = await activeProtocol().lookupAccount(pHandle)
      expect(pLookup.kind).toBe('found')
      if (pLookup.kind === 'found') {
        expect(String(pLookup.account.id)).toBe(pixelfed.userId)
      }

      AccountStore.setActive(mSummary.accountId)
      expect(activeProtocol().kind).toBe('mastodon')
      const mHandle = parseHandle(mastodon.acct, mastodon.instance)
      const mLookup = await activeProtocol().lookupAccount(mHandle)
      expect(mLookup.kind).toBe('found')
      if (mLookup.kind === 'found') {
        expect(String(mLookup.account.id)).toBe(mastodon.userId)
      }
    })

    it('activeIdOrThrow always returns the most-recently-activated id', () => {
      const pSummary = seedAccountFromRig(pixelfed)
      const mSummary = seedAccountFromRig(mastodon)
      AccountStore.setActive(pSummary.accountId)
      expect(AccountStore.activeIdOrThrow()).toBe(pSummary.accountId)
      AccountStore.setActive(mSummary.accountId)
      expect(AccountStore.activeIdOrThrow()).toBe(mSummary.accountId)
    })
  }
)
