← back to Dw Unbuyable Recovery Pilot

tk11040-momentum-reconcile/meilisearch-client.test.mjs

39 lines

import assert from 'node:assert/strict';
import test from 'node:test';
import { searchHits } from './meilisearch-client.mjs';

test('returns hits from a successful response', async () => {
  const hits = [{ number: '123', price: 42 }];
  const result = await searchHits({
    host: 'https://example.invalid',
    key: 'test-only',
    query: 'sample',
    fetchImpl: async () => ({ ok: true, status: 200, json: async () => ({ hits }) }),
  });
  assert.deepEqual(result, hits);
});

test('fails closed on invalid authentication', async () => {
  await assert.rejects(
    searchHits({
      host: 'https://example.invalid',
      key: 'invalid-test-only',
      query: 'sample',
      fetchImpl: async () => ({ ok: false, status: 401 }),
    }),
    /HTTP 401/,
  );
});

test('fails closed when the response schema is invalid', async () => {
  await assert.rejects(
    searchHits({
      host: 'https://example.invalid',
      key: 'test-only',
      query: 'sample',
      fetchImpl: async () => ({ ok: true, status: 200, json: async () => ({}) }),
    }),
    /missing a hits array/,
  );
});