import db from "debug" ; import { get , has , set , unset } from "lodash" ; import { CustomError } from "ts-custom-error" ; import { createDeployment , Deployment } from "../../src/server/deployment" ; import { HistoryEntry , HistoryName , HistoryStatus , HistoryStore } from "../../src/server/history" ; import { AdaptServer , DeleteOptions , GetOptions , SetOptions , } from "../../src/server/server" ; import { ServerBase } from "../../src/server/server_base" ; export class DataError extends CustomError { public constructor ( message ? ) { super ( message ) ; } } const debugServer = db ( "adapt:server" ) ; const mockDeploymentDefaults = { projectName : "mock" , stackName : "mock" , } ; export interface MockDeploymentOptions { deployID ? : string ; projectName ? : string ; stackName ? : string ; } export async function createMockDeployment ( options = { } ) { const opts = { ... mockDeploymentDefaults , ... options } ; const { projectName , stackName , deployID } = opts ; const server = new MockServer ( ) ; return createDeployment ( server , projectName , stackName , { deployID } ) ; } const toLpath = ( p ) => { if ( p [ 0 ] !== "/" ) throw new Error ( `MockServer: Only absolute paths supported (${ p })` ) ; return p . slice ( 1 ) . replace ( RegExp ( "/" , "g" ) , "." ) ; } ; export class MockServer extends ServerBase implements AdaptServer { data = { } ; async init ( ) { } async destroy ( ) { } async set ( dataPath , val , options = { } ) { return this . withLock ( options , async ( ) => { const lpath = toLpath ( dataPath ) ; if ( options && options . mustCreate === true && has ( this . data , lpath ) ) { throw new Error ( `Path '${ dataPath }' already exists` ) ; } debugServer ( `SET ${ options . lock ? "(L) " : "" }${ dataPath } = ${ val }` ) ; set ( this . data , lpath , val ) ; } ) ; } async get ( dataPath , options = { } ) { return this . withLock ( options , async ( ) => { const lpath = toLpath ( dataPath ) ; if ( ! has ( this . data , lpath ) ) throw new DataError ( `Path '${ dataPath }' not found` ) ; const val = get ( this . data , lpath ) ; debugServer ( `GET ${ options . lock ? "(L) " : "" }${ dataPath } = ${ val }` ) ; return val ; } ) ; } async delete ( dataPath , options = { } ) { return this . withLock ( options , async ( ) => { const lpath = toLpath ( dataPath ) ; if ( ! has ( this . data , lpath ) ) throw new DataError ( `Path '${ dataPath }' not found` ) ; debugServer ( `DEL ${ options . lock ? "(L) " : "" }${ dataPath }` ) ; unset ( this . data , lpath ) ; } ) ; } async historyStore ( dataPath , init ) { return new MockHistoryStore ( ) ; } } export class MockHistoryStore implements HistoryStore { async commitEntry ( toStore ) { throw new Error ( `Not implemented` ) ; } async getDataDir ( withStatus ) { throw new Error ( `Not implemented` ) ; } async releaseDataDir ( ) { throw new Error ( `Not implemented` ) ; } async historyEntry ( historyName ) { throw new Error ( `Not implemented` ) ; } async last ( withStatus ) { throw new Error ( `Not implemented` ) ; } async destroy ( ) { } }