import Redis from 'ioredis'

let testClient

/**
 * Test-scoped Valkey client (separate db number from dev — see .env.test).
 * Tests can use this directly for keyspace assertions, or just rely on the services
 * which read VALKEY_URL via the production plugin (now pointing at the test db).
 */
export const getTestValkey = () => {
  if (!testClient) {
    testClient = new Redis(process.env.TEST_VALKEY_URL || process.env.VALKEY_URL)
  }
  return testClient
}

export const flushTestDb = async () => {
  const c = getTestValkey()
  await c.flushdb()
}

export const closeTestValkey = async () => {
  if (testClient) {
    testClient.disconnect()
    testClient = null
  }
}
