import * as vscode from 'vscode' ; import { JestExtExplorerContext , TestItemData } from './types' ; /**
 * provide context information from JestExt and test provider state:
 * 1. TestData <-> TestItem
 *
 * as well as factory functions to create TestItem and TestRun that could impact the state
 */ export type OUTPUT_COLOR = 'red' | 'green' | 'yellow' ; const COLORS = { [ 'red' ] : '\x1b[0;31m' , [ 'green' ] : '\x1b[0;32m' , [ 'yellow' ] : '\x1b[0;33m' , [ 'end' ] : '\x1b[0m' , } ; export type TagIdType = 'run' | 'debug' ; export class JestTestProviderContext { private testItemData : WeakMap < vscode . TestItem , TestItemData > ; constructor ( public readonly ext , private readonly controller , private readonly profiles ) { this . testItemData = new WeakMap ( ) ; } createTestItem = ( id , label , uri , data , parent ? , tagIds = [ 'run' , 'debug' ] ) : vscode . TestItem => { const testItem = this . controller . createTestItem ( id , label , uri ) ; this . testItemData . set ( testItem , data ) ; const collection = parent ? parent . children : this . controller . items ; collection . add ( testItem ) ; tagIds ?. forEach ( ( tId ) => { const tag = this . getTag ( tId ) ; if ( tag ) { testItem . tags = [ ... testItem . tags , tag ] ; } } ) ; return testItem ; } ; param item param childId returns getChildData = < T extends TestItemData = TestItemData > ( item , childId ) : T | undefined => { const cItem = item . children . get ( childId ) ; return cItem && ( this . testItemData . get ( cItem ) as T ) ; } ; returns getData = < T extends TestItemData > ( item ) : T | undefined => { return this . testItemData . get ( item ) as T | undefined ; } ; createTestRun = ( request , name ) : vscode . TestRun => { return this . controller . createTestRun ( request , name ) ; } ; appendOutput = ( msg , run , newLine = true , color ? ) : void => { const converted = msg . replace ( /\n/g , '\r\n' ) ; let text = newLine ? `[${ this . ext . workspace . name }]: ${ converted }` : converted ; if ( color ) { text = `${ COLORS [ color ] }${ text }${ COLORS [ 'end' ] }` ; } run . appendOutput ( `${ text }${ newLine ? '\r\n' : '' }` ) ; showTestExplorerTerminal ( ) ; } ; getTag = ( tagId ) : vscode . TestTag | undefined => this . profiles . find ( ( p ) => p . tag ?. id === tagId ) ?. tag ; } /** show TestExplorer Terminal on first invocation only */ let showTerminal = true ; const showTestExplorerTerminal = ( ) => { if ( showTerminal ) { showTerminal = false ; vscode . commands . executeCommand ( 'testing.showMostRecentOutput' ) ; } } ;