-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
55 changed files
with
1,479 additions
and
567 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
apps/appcontainer-node/packages/generic/src/__mocks__/@sofie-package-manager/api.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ | ||
import EventEmitter from 'events' | ||
|
||
const packageManagerAPI: any = jest.createMockFromModule('@sofie-package-manager/api') | ||
const realPackageManagerAPI = jest.requireActual('@sofie-package-manager/api') | ||
|
||
type ClientType = 'N/A' | 'workerAgent' | 'expectationManager' | 'appContainer' | ||
|
||
class MockClientConnection extends EventEmitter { | ||
constructor() { | ||
super() | ||
} | ||
|
||
public clientType: ClientType = 'N/A' | ||
public clientId = '' | ||
} | ||
|
||
export class WebsocketServer extends EventEmitter { | ||
constructor(public _port: number, public _logger: any, connectionClb: (client: MockClientConnection) => void) { | ||
super() | ||
WebsocketServer.connectionClb = connectionClb | ||
} | ||
static connectionClb: (connection: MockClientConnection) => void | ||
|
||
static openConnections: MockClientConnection[] = [] | ||
|
||
static mockNewConnection(clientId: string, clientType: ClientType): MockClientConnection { | ||
const newConnection = new MockClientConnection() | ||
newConnection.clientId = clientId | ||
newConnection.clientType = clientType | ||
WebsocketServer.openConnections.push(newConnection) | ||
WebsocketServer.connectionClb(newConnection) | ||
return newConnection | ||
} | ||
|
||
terminate() { | ||
WebsocketServer.openConnections.forEach((connection) => { | ||
connection.emit('close') | ||
}) | ||
this.emit('close') | ||
} | ||
} | ||
packageManagerAPI.WebsocketServer = WebsocketServer | ||
|
||
// these are various utilities, not really a part of the API | ||
packageManagerAPI.initializeLogger = realPackageManagerAPI.initializeLogger | ||
packageManagerAPI.setupLogger = realPackageManagerAPI.setupLogger | ||
packageManagerAPI.protectString = realPackageManagerAPI.protectString | ||
packageManagerAPI.unprotectString = realPackageManagerAPI.unprotectString | ||
packageManagerAPI.literal = realPackageManagerAPI.literal | ||
packageManagerAPI.mapEntries = realPackageManagerAPI.mapEntries | ||
packageManagerAPI.findValue = realPackageManagerAPI.findValue | ||
packageManagerAPI.DataStore = realPackageManagerAPI.DataStore | ||
packageManagerAPI.stringifyError = realPackageManagerAPI.stringifyError | ||
packageManagerAPI.waitTime = realPackageManagerAPI.waitTime | ||
|
||
module.exports = packageManagerAPI |
74 changes: 74 additions & 0 deletions
74
apps/appcontainer-node/packages/generic/src/__mocks__/child_process.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import EventEmitter from 'events' | ||
|
||
/* eslint-disable no-console */ | ||
|
||
const child_process: any = jest.createMockFromModule('child_process') | ||
|
||
async function pExec(_commandString: string, _options: any): Promise<{ stdout: string; stderr: string }> { | ||
const NOOP = { stdout: '', stderr: '' } | ||
return NOOP | ||
} | ||
function exec( | ||
commandString: string, | ||
options?: any, | ||
cb?: (error: any | null, result: { stdout: string; stderr: string } | null) => void | ||
): void { | ||
if (typeof options === 'function' && cb === undefined) { | ||
cb = options | ||
options = {} | ||
} | ||
pExec(commandString, options) | ||
.then((result) => cb?.(null, result)) | ||
.catch((err) => cb?.(err, null)) | ||
} | ||
child_process.exec = exec | ||
|
||
const allProcesses: SpawnedProcess[] = [] | ||
let mockOnNewProcessClb: null | ((process: SpawnedProcess) => void) = null | ||
function spawn(command: string, args: string[] = []) { | ||
const spawned = new SpawnedProcess(command, args) | ||
mockOnNewProcessClb?.(spawned) | ||
allProcesses.push(spawned) | ||
|
||
spawned.on('exit', () => { | ||
const index = allProcesses.indexOf(spawned) | ||
allProcesses.splice(index, 1) | ||
}) | ||
return spawned | ||
} | ||
child_process.spawn = spawn | ||
|
||
function mockOnNewProcess(clb: (process: SpawnedProcess) => void) { | ||
mockOnNewProcessClb = clb | ||
} | ||
child_process.mockOnNewProcess = mockOnNewProcess | ||
|
||
function mockListAllProcesses(): SpawnedProcess[] { | ||
return allProcesses | ||
} | ||
child_process.mockListAllProcesses = mockListAllProcesses | ||
|
||
function mockClearAllProcesses(): void { | ||
allProcesses.length = 0 | ||
} | ||
child_process.mockClearAllProcesses = mockClearAllProcesses | ||
|
||
class SpawnedProcess extends EventEmitter { | ||
public stdout = new EventEmitter() | ||
public stderr = new EventEmitter() | ||
public pid: number | ||
|
||
constructor(public command: string, public args: string[]) { | ||
super() | ||
this.pid = Date.now() | ||
} | ||
|
||
kill() { | ||
this.emit('exit') | ||
this.stdout.emit('end') | ||
this.stderr.emit('end') | ||
return true | ||
} | ||
} | ||
|
||
module.exports = child_process |
50 changes: 50 additions & 0 deletions
50
apps/appcontainer-node/packages/generic/src/__mocks__/workerAgentApi.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { | ||
AppContainerWorkerAgent, | ||
AdapterServerOptions, | ||
LogLevel, | ||
Expectation, | ||
ReturnTypeDoYouSupportExpectation, | ||
PackageContainerExpectation, | ||
AppContainerId, | ||
WorkerAgentId, | ||
} from '@sofie-package-manager/api' | ||
|
||
export class WorkerAgentAPI implements AppContainerWorkerAgent.WorkerAgent { | ||
constructor( | ||
public id: AppContainerId, | ||
methods: AppContainerWorkerAgent.AppContainer, | ||
_options: AdapterServerOptions<AppContainerWorkerAgent.WorkerAgent> | ||
) { | ||
WorkerAgentAPI.mockAppContainer[methods.id] = methods | ||
} | ||
|
||
public static mockAppContainer: Record<WorkerAgentId, AppContainerWorkerAgent.AppContainer> = {} | ||
|
||
public static mockReset(): void { | ||
WorkerAgentAPI.mockAppContainer = {} | ||
} | ||
|
||
type = '' | ||
|
||
async setLogLevel(_logLevel: LogLevel): Promise<void> { | ||
return | ||
} | ||
async _debugKill(): Promise<void> { | ||
return | ||
} | ||
async doYouSupportExpectation(_exp: Expectation.Any): Promise<ReturnTypeDoYouSupportExpectation> { | ||
return { | ||
support: true, | ||
} | ||
} | ||
async doYouSupportPackageContainer( | ||
_packageContainer: PackageContainerExpectation | ||
): Promise<ReturnTypeDoYouSupportExpectation> { | ||
return { | ||
support: true, | ||
} | ||
} | ||
async setSpinDownTime(_spinDownTime: number): Promise<void> { | ||
return | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
apps/appcontainer-node/packages/generic/src/__mocks__/workforceApi.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ | ||
import { AppContainerId, LoggerInstance, WorkForceAppContainer } from '@sofie-package-manager/api' | ||
import { EventEmitter } from 'events' | ||
|
||
export class WorkforceAPI extends EventEmitter implements WorkForceAppContainer.WorkForce { | ||
constructor(public id: AppContainerId, _loger: LoggerInstance) { | ||
super() | ||
} | ||
|
||
connected = false | ||
public static mockAvailableApps: AppDesc[] = [] | ||
public static mockMethods: Record<string, (...args: any[]) => Promise<void>> = {} | ||
|
||
registerAvailableApps = async (availableApps: AppDesc[]): Promise<void> => { | ||
WorkforceAPI.mockAvailableApps = availableApps | ||
return | ||
} | ||
init = async (_connectionOptions: any, methods: any) => { | ||
WorkforceAPI.mockMethods = methods | ||
this.connected = true | ||
setImmediate(() => { | ||
this.emit('connected') | ||
}) | ||
} | ||
|
||
terminate = () => { | ||
this.emit('disconnected') | ||
} | ||
} | ||
|
||
type AppDesc = { appType: `@@protectedString/AppType/${string}` } |
Oops, something went wrong.