fix: add regression tests for delete and message update paths

Address follow-up review findings:

- Add test for deleteObjectFromMeili verifying it uses messageId (not
  MongoDB _id) when calling index.deleteDocument, guarding against
  regression of the silent orphaned-document bug.

- Add test for message model update path asserting { primaryKey:
  'messageId' } is passed to updateDocuments (previously only the
  conversation model update path was tested).

- Add @param config.primaryKey to createMeiliMongooseModel JSDoc.
This commit is contained in:
Danny Avila 2026-04-03 14:06:24 -04:00
parent b203ff5868
commit 3e6f90efe4
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956
2 changed files with 41 additions and 0 deletions

View file

@ -156,6 +156,46 @@ describe('Meilisearch Mongoose plugin', () => {
);
});
test('updating an indexed message calls updateDocuments with primaryKey: messageId', async () => {
const messageModel = createMessageModel(mongoose);
const msg = await messageModel.create({
messageId: new mongoose.Types.ObjectId().toString(),
conversationId: new mongoose.Types.ObjectId(),
user: new mongoose.Types.ObjectId(),
isCreatedByUser: true,
});
mockUpdateDocuments.mockClear();
msg._meiliIndex = true;
msg.text = 'Updated text';
await msg.save();
expect(mockUpdateDocuments).toHaveBeenCalledWith(
[expect.objectContaining({ messageId: expect.anything() })],
{ primaryKey: 'messageId' },
);
});
test('deleteObjectFromMeili calls deleteDocument with messageId, not _id', async () => {
const messageModel = createMessageModel(mongoose);
const msgId = new mongoose.Types.ObjectId().toString();
const msg = await messageModel.create({
messageId: msgId,
conversationId: new mongoose.Types.ObjectId(),
user: new mongoose.Types.ObjectId(),
isCreatedByUser: true,
});
mockDeleteDocument.mockClear();
const typedMsg = msg as unknown as import('./mongoMeili').DocumentWithMeiliIndex;
await new Promise<void>((resolve, reject) => {
typedMsg.deleteObjectFromMeili!((err) => (err ? reject(err) : resolve()));
});
expect(mockDeleteDocument).toHaveBeenCalledWith(msgId);
expect(mockDeleteDocument).not.toHaveBeenCalledWith(String(msg._id));
});
test('updateDocuments receives preprocessed data with primaryKey', async () => {
const conversationModel = createConversationModel(mongoose);
const conversationId = 'abc|def|ghi';

View file

@ -130,6 +130,7 @@ const processBatch = async <T>(
* @param config - Configuration object.
* @param config.index - The MeiliSearch index object.
* @param config.attributesToIndex - List of attributes to index.
* @param config.primaryKey - The primary key field for MeiliSearch document operations.
* @param config.syncOptions - Sync configuration options.
* @returns A class definition that will be loaded into the Mongoose schema.
*/