Testing Algolia search: relevance checks and waiting for indexing tasks
Search breaks in ways that unit tests don't see: someone changes the ranking settings, a synonym list disappears, an indexing job fails and the index slowly goes stale. A few API tests against Algolia ("searching red shoes returns a red shoe first") catch these before users write in.
Step 1: Environment
| Variable | Value |
|---|---|
algoliaApp | your application id |
algoliaSearchKey | a search-only key, marked as secret |
algoliaAdminKey | a key with write rights, for the test index only, secret |
indexName | products_test |
Set the two headers in the collection's pre-request script, with the search key by default:
pm.request.headers.set('X-Algolia-Application-Id', pm.environment.get('algoliaApp'));
if (!pm.request.headers.toJSON()['X-Algolia-API-Key']) {
pm.request.headers.set('X-Algolia-API-Key', pm.environment.get('algoliaSearchKey'));
}Requests that write set X-Algolia-API-Key: {{algoliaAdminKey}} themselves.
Step 2: A relevance test
POST https://{{algoliaApp}}-dsn.algolia.net/1/indexes/{{indexName}}/query
Content-Type: application/json
{"query": "red running shoes", "hitsPerPage": 5, "attributesToRetrieve": ["name", "color", "category"]}const res = pm.response.json();
pm.test('results found', () => pm.expect(res.nbHits).to.be.above(0));
pm.test('top hit is a red shoe', () => {
const top = res.hits[0];
pm.expect(top.category).to.equal('shoes');
pm.expect(top.color).to.equal('red');
});
pm.test('search is fast', () => pm.expect(res.processingTimeMS).to.be.below(50));Pick five to ten queries that matter to the business ("the query in the homepage banner", "the brand name", "a typo of the brand name") and pin their top results. Keep the expectations loose enough to survive normal data changes: "top hit is a shoe", not "top hit is product 1234".
Step 3: Writes are asynchronous
POST https://{{algoliaApp}}.algolia.net/1/indexes/{{indexName}}
X-Algolia-API-Key: {{algoliaAdminKey}}
Content-Type: application/json
{"objectID": "api-test-1", "name": "API test shoe", "color": "red", "category": "shoes"}pm.environment.set('taskID', String(pm.response.json().taskID));The answer arrives before the record is searchable. Searching straight away is a classic source of flaky tests. Wait on the task:
GET https://{{algoliaApp}}.algolia.net/1/indexes/{{indexName}}/task/{{taskID}}
X-Algolia-API-Key: {{algoliaAdminKey}}pm.test('indexed', () => pm.expect(pm.response.json().status).to.equal('published'));notPublished means it isn't done yet. In a run, retry this request a few times with a delay; polling async jobs shows the pattern. Writes go to {APP_ID}.algolia.net, and searches to the -dsn host, which is the one tuned for reads.
Step 4: Key permissions
A test that's easy to forget: the search key must not be able to write.
POST https://{{algoliaApp}}.algolia.net/1/indexes/{{indexName}}
X-Algolia-API-Key: {{algoliaSearchKey}}
Content-Type: application/json
{"objectID": "should-fail"}pm.test('search key cannot write', () => pm.expect(pm.response.code).to.equal(403));That key ships in your frontend. If it can write, anyone can change your index.
Step 5: Clean up
DELETE https://{{algoliaApp}}.algolia.net/1/indexes/{{indexName}}/api-test-1 with the admin key, as a cleanup step. Or run write tests against a separate index and clear it with POST /1/indexes/{index}/clear.