mattermost-mobile/app/database/operator/utils/general.test.ts
Daniel Espino García bb7ff622af
Add Playbooks read-only support for mobile devices (#8978)
* Add the channel options to get into playbooks (#8750)

* Add the channel options to get into playbooks

* Use playbook run id instead of playbook id

* i18n

* Fix issues and move playbooks to the products folder

* Address some tests

* Fix test

* Address design issues

* Add requested comment

---------

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>

* Playbooks database (#8802)

* Add lastPlaybookFetchAt to channel table (#8916)

* Add lastPlaybookFetchAt to channel table

* Add missing commit

* Use my_channel table instead

* Fix test

* Address feedback

* First implementation of playbooks API (#8897)

* First implementation of playbooks API

* Add version check

* Address feedback

* Fix test

* Add last fetch at usage and other improvements

* Simplify test

* Add sort_order, update_at and previousReminder columns (#8927)

* Add sort_order, update_at and previousReminder columns

* Remove order from the schema

* Fix tests

* Add tests

* Add websockets for playbooks (#8947)

* Add websocket events for playbooks

* Fix typo

* Add playbook run list (#8761)

* Add the channel options to get into playbooks

* Use playbook run id instead of playbook id

* i18n

* Fix issues and move playbooks to the products folder

* Address some tests

* Fix test

* Add playbook run list

* Add missing texts

* Add back button support and item size to flash list

* Address design issues

* Add requested comment

* Standardize tag and use it in the card

* Fix merge

* Add API related functionality

---------

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>

* Add playbooks run details (#8872)

* Add the channel options to get into playbooks

* Use playbook run id instead of playbook id

* i18n

* Fix issues and move playbooks to the products folder

* Address some tests

* Fix test

* Add playbook run list

* Add missing texts

* Add back button support and item size to flash list

* Address design issues

* Add requested comment

* Standardize tag and use it in the card

* Add playbooks run details

* Fix merge

* Add API related functionality

* Add API related changes

* Order fixes

* Several fixes

* Add error state on playbook run

* i18n-extract

* Fix tests

* Fix test

* Several fixes

* Fixes and add missing UI elements

* i18n-extract

* Fix tests

* Remove files from bad merge

---------

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>

* Add missing tests for playbooks (#8976)

* Add the channel options to get into playbooks

* Use playbook run id instead of playbook id

* i18n

* Fix issues and move playbooks to the products folder

* Address some tests

* Fix test

* Add playbook run list

* Add missing texts

* Add back button support and item size to flash list

* Address design issues

* Add requested comment

* Standardize tag and use it in the card

* Add playbooks run details

* Fix merge

* Add API related functionality

* Add API related changes

* Order fixes

* Several fixes

* Add error state on playbook run

* i18n-extract

* Fix tests

* Fix test

* Several fixes

* Fixes and add missing UI elements

* i18n-extract

* Fix tests

* Remove files from bad merge

* Add tests

* Fix typo

* Add missing strings

* Fix tests and skip some

* Fix test

* Fix typo

---------

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>

* Address feedback

* Address feedback and fix tests

* Address comments and fix tests

* Address feedback

* Address plugin changes and fix bugs

---------

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
2025-07-14 09:21:37 +02:00

125 lines
4.6 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import DatabaseManager from '@database/manager';
import {PLAYBOOK_TABLES} from '@playbooks/constants/database';
import TestHelper from '@test/test_helper';
import {isHasManyAssociation, prepareDestroyPermanentlyChildrenAssociatedRecords} from './general';
import type ServerDataOperator from '../server_data_operator';
import type {AssociationInfo} from '@nozbe/watermelondb/Model';
const {PLAYBOOK_RUN, PLAYBOOK_CHECKLIST, PLAYBOOK_CHECKLIST_ITEM} = PLAYBOOK_TABLES;
describe('isHasManyAssociation', () => {
it('should return true for a valid has_many association', () => {
const association: AssociationInfo = {
type: 'has_many',
foreignKey: 'user_id',
};
expect(isHasManyAssociation(association)).toBe(true);
});
it('should return false for an association without type "has_many"', () => {
const association: AssociationInfo = {
type: 'belongs_to',
key: 'user_id',
};
expect(isHasManyAssociation(association)).toBe(false);
});
it('should return false for an association without a foreignKey', () => {
// @ts-expect-error foreginKey is missing
const association: AssociationInfo = {
type: 'has_many',
};
expect(isHasManyAssociation(association)).toBe(false);
});
it('should return false for an invalid association object', () => {
const association = {
type: 'has_many',
};
expect(isHasManyAssociation(association as AssociationInfo)).toBe(false);
});
});
describe('prepareDestroyPermanentlyChildrenAssociatedRecords', () => {
let operator: ServerDataOperator;
beforeEach(async () => {
await DatabaseManager.init(['test.server.com']);
operator = DatabaseManager.serverDatabases['test.server.com']!.operator;
});
afterEach(async () => {
await DatabaseManager.destroyServerDatabase('test.server.com');
});
it('should prepare associated records for permanent deletion', async () => {
// Create playbook runs with associated checklists and items
const mockRuns = TestHelper.createPlaybookRuns(1, 1, 2); // 1 run, 1 checklist, 2 items
await operator.handlePlaybookRun({
runs: mockRuns,
prepareRecordsOnly: false,
processChildren: true,
});
// Fetch the playbook run record
const playbookRunRecord = await operator.database.get(PLAYBOOK_RUN).find(mockRuns[0].id);
// Call the function under test
const result = await prepareDestroyPermanentlyChildrenAssociatedRecords([playbookRunRecord]);
// Fetch the checklist records
const checklistRecords = await operator.database.collections.get(PLAYBOOK_CHECKLIST).query().fetch();
// Fetch the checklist item records
const checklistItemRecords = await operator.database.get(PLAYBOOK_CHECKLIST_ITEM).query().fetch();
// Verify that all checklists and items were prepared for permanent deletion
const expectedPreparedRecords = [
...checklistRecords.map((checklist) => checklist.prepareDestroyPermanently()),
...checklistItemRecords.map((item) => item.prepareDestroyPermanently()),
];
expect(result).toHaveLength(expectedPreparedRecords.length);
expectedPreparedRecords.forEach((expectedRecord) => {
const matchingRecord = result.find((record) => record.id === expectedRecord.id);
expect(matchingRecord).toBeDefined();
expect(matchingRecord).toEqual(expectedRecord);
});
});
it('should handle records with no associations', async () => {
// Create a playbook run without any checklists or items
const mockItem: PartialChecklistItem = {
...TestHelper.createPlaybookItem('playbook_run_1-checklist_1', 1),
checklist_id: 'checklist_1',
};
await operator.handlePlaybookChecklistItem({
items: [mockItem],
prepareRecordsOnly: false,
});
// Fetch the playbook run record
const playbookItemRecord = await operator.database.get(PLAYBOOK_CHECKLIST_ITEM).find(mockItem.id);
// Call the function under test
const result = await prepareDestroyPermanentlyChildrenAssociatedRecords([playbookItemRecord]);
// Verify the results
expect(result).toEqual([]);
});
it('should handle empty records array', async () => {
const result = await prepareDestroyPermanentlyChildrenAssociatedRecords([]);
expect(result).toEqual([]);
});
});