'use strict' ; import assert from 'assert' ; import { prepareTestApplication , testClear } from '../../../../Util' ; import { Application , Agent } from 'egg' ; import { LuaService } from '../../../../../app/basic/HostingPlatform/HostingClientService/LuaService' ; describe ( 'LuaServiceLabelSetup' , ( ) => { let app ; let agent ; let updateCounter = 0 ; let createCounter = 0 ; let luaService ; class MockHostingBase { getName ( ) { return 'name' ; } getConfig ( ) { return { } ; } } class MockClient { getHostingBase ( ) { return new MockHostingBase ( ) ; } getFullName ( ) { return 'owner/repo' ; } getCompConfig ( ) { return { labels : [ ] } ; } listLabels ( ) { return [ ] ; } updateLabels < T > ( updateTask ) { updateCounter += updateTask . length ; } createLabels < T > ( createTask ) { createCounter += createTask . length ; } } beforeEach ( async ( ) => { ( { app , agent } = await prepareTestApplication ( ) ) ; luaService = new LuaService ( app , new MockClient ( ) as any ) ; updateCounter = 0 ; createCounter = 0 ; } ) ; afterEach ( ( ) => { testClear ( app , agent ) ; } ) ; it ( 'no need to update' , async ( ) => { ( luaService as any ) . client . getCompConfig = ( ) => { return { labels : [ { name : 'test1' } ] } ; } ; ( luaService as any ) . client . listLabels = ( ) => { return [ { name : 'test1' } ] ; } ; await ( luaService as any ) . lua_labelSetup ( ) ; assert ( updateCounter === 0 && createCounter === 0 ) ; } ) ; it ( 'update old labels' , async ( ) => { ( luaService as any ) . client . getCompConfig = ( ) => { return { labels : [ { name : 'test1' , color : 'color1' } ] } ; } ; ( luaService as any ) . client . listLabels = ( ) => { return [ { name : 'test1' } ] ; } ; await ( luaService as any ) . lua_labelSetup ( ) ; assert ( updateCounter === 1 && createCounter === 0 ) ; } ) ; it ( 'create new labels' , async ( ) => { ( luaService as any ) . client . getCompConfig = ( ) => { return { labels : [ { name : 'test1' } ] } ; } ; await ( luaService as any ) . lua_labelSetup ( ) ; assert ( updateCounter === 0 && createCounter === 1 ) ; } ) ; it ( 'mixed tests' , async ( ) => { ( luaService as any ) . client . getCompConfig = ( ) => { return { labels : [ { name : 'test1' } , { name : 'test2' , description : 'description2' } , { name : 'test3' } , ] , } ; } ; ( luaService as any ) . client . listLabels = ( ) => { return [ { name : 'test1' } , { name : 'test2' } , ] ; } ; await ( luaService as any ) . lua_labelSetup ( ) ; assert ( updateCounter === 1 && createCounter === 1 ) ; } ) ; } ) ;