44import { SYSTEM_IDENTIFIERS } from '@constants/database' ;
55import DatabaseManager from '@database/manager' ;
66import { PLAYBOOK_TABLES } from '@playbooks/constants/database' ;
7+ import { getMyChannel } from '@queries/servers/channel' ;
78import { querySystemValue } from '@queries/servers/system' ;
9+ import TestHelper from '@test/test_helper' ;
810
11+ import { updateLastPlaybookRunsFetchAt } from './channel' ;
912import { setPlaybooksVersion } from './version' ;
1013
1114import type ServerDataOperator from '@database/operator/server_data_operator' ;
1215
1316const serverUrl = 'baseHandler.test.com' ;
17+ const channelId = 'channelid' ;
1418let operator : ServerDataOperator ;
1519
20+ const channel : Channel = TestHelper . fakeChannel ( {
21+ id : channelId ,
22+ team_id : 'teamid1' ,
23+ } ) ;
24+
25+ const channelMember : ChannelMembership = TestHelper . fakeChannelMember ( {
26+ id : 'id' ,
27+ channel_id : channelId ,
28+ } ) ;
29+
1630beforeEach ( async ( ) => {
1731 await DatabaseManager . init ( [ serverUrl ] ) ;
1832 operator = DatabaseManager . serverDatabases [ serverUrl ] ! . operator ;
@@ -50,6 +64,11 @@ describe('setPlaybooksVersion', () => {
5064
5165 it ( 'should purge playbooks when version is empty' , async ( ) => {
5266 const database = operator . database ;
67+
68+ await operator . handleChannel ( { channels : [ channel ] , prepareRecordsOnly : false } ) ;
69+ await operator . handleMyChannel ( { channels : [ channel ] , myChannels : [ channelMember ] , prepareRecordsOnly : false } ) ;
70+ await updateLastPlaybookRunsFetchAt ( serverUrl , channelId , 1234 ) ;
71+
5372 jest . spyOn ( database . adapter , 'unsafeExecute' ) . mockImplementation ( ( ) => {
5473 return Promise . resolve ( ) ;
5574 } ) ;
@@ -65,10 +84,18 @@ describe('setPlaybooksVersion', () => {
6584 [ `DELETE FROM ${ PLAYBOOK_TABLES . PLAYBOOK_CHECKLIST_ITEM } ` , [ ] ] ,
6685 ] ,
6786 } ) ;
87+
88+ const myChannel = await getMyChannel ( operator . database , channelId ) ;
89+ expect ( myChannel ?. lastPlaybookRunsFetchAt ) . toBe ( 0 ) ;
6890 } ) ;
6991
7092 it ( 'should not purge playbooks when version is not empty' , async ( ) => {
7193 const database = operator . database ;
94+
95+ await operator . handleChannel ( { channels : [ channel ] , prepareRecordsOnly : false } ) ;
96+ await operator . handleMyChannel ( { channels : [ channel ] , myChannels : [ channelMember ] , prepareRecordsOnly : false } ) ;
97+ await updateLastPlaybookRunsFetchAt ( serverUrl , channelId , 1234 ) ;
98+
7299 jest . spyOn ( database . adapter , 'unsafeExecute' ) . mockImplementation ( ( ) => {
73100 return Promise . resolve ( ) ;
74101 } ) ;
@@ -78,6 +105,8 @@ describe('setPlaybooksVersion', () => {
78105 expect ( data ) . toBe ( true ) ;
79106
80107 expect ( database . adapter . unsafeExecute ) . not . toHaveBeenCalled ( ) ;
108+ const myChannel = await getMyChannel ( operator . database , channelId ) ;
109+ expect ( myChannel ?. lastPlaybookRunsFetchAt ) . toBe ( 1234 ) ;
81110 } ) ;
82111
83112 it ( 'should handle purge playbooks errors' , async ( ) => {
0 commit comments