tstest/ts/tstest.classes.tstest.ts

199 lines
7.0 KiB
TypeScript
Raw Normal View History

2022-03-12 23:30:32 +00:00
import * as plugins from './tstest.plugins.js';
import * as paths from './tstest.paths.js';
import * as logPrefixes from './tstest.logprefixes.js';
2018-08-05 21:20:32 +00:00
2023-07-13 01:19:11 +00:00
import { coloredString as cs } from '@push.rocks/consolecolor';
2018-08-04 12:23:07 +00:00
2022-03-12 23:30:32 +00:00
import { TestDirectory } from './tstest.classes.testdirectory.js';
import { TapCombinator } from './tstest.classes.tap.combinator.js';
import { TapParser } from './tstest.classes.tap.parser.js';
2018-08-03 17:18:42 +00:00
export class TsTest {
2020-07-07 20:50:29 +00:00
public testDir: TestDirectory;
public smartshellInstance = new plugins.smartshell.Smartshell({
executor: 'bash',
pathDirectories: [paths.binDirectory],
sourceFilePaths: [],
});
public smartbrowserInstance = new plugins.smartbrowser.SmartBrowser();
public tsbundleInstance = new plugins.tsbundle.TsBundle();
2018-08-03 17:18:42 +00:00
constructor(cwdArg: string, relativePathToTestDirectory: string) {
this.testDir = new TestDirectory(cwdArg, relativePathToTestDirectory);
}
async run() {
const fileNamesToRun: string[] = await this.testDir.getTestFilePathArray();
console.log(cs(plugins.figures.hamburger.repeat(80), 'cyan'));
console.log('');
console.log(`${logPrefixes.TsTestPrefix} FOUND ${fileNamesToRun.length} TESTFILE(S):`);
2018-08-03 17:18:42 +00:00
for (const fileName of fileNamesToRun) {
2018-08-05 21:20:32 +00:00
console.log(`${logPrefixes.TsTestPrefix} ${cs(fileName, 'orange')}`);
2018-08-03 17:18:42 +00:00
}
console.log('-'.repeat(48));
2018-08-04 17:23:17 +00:00
console.log(''); // force new line
2020-07-07 21:59:07 +00:00
2018-08-03 17:18:42 +00:00
const tapCombinator = new TapCombinator(); // lets create the TapCombinator
2020-07-07 20:50:29 +00:00
for (const fileNameArg of fileNamesToRun) {
2020-07-07 21:59:07 +00:00
switch (true) {
2020-10-01 13:30:29 +00:00
case process.env.CI && fileNameArg.includes('.nonci.'):
console.log('!!!!!!!!!!!');
2020-10-01 14:12:13 +00:00
console.log(
2022-07-24 12:11:17 +00:00
`not running testfile ${fileNameArg}, since we are CI and file name includes '.nonci.' tag`
2020-10-01 14:12:13 +00:00
);
2020-10-01 13:30:29 +00:00
console.log('!!!!!!!!!!!');
break;
2022-11-08 07:04:46 +00:00
case fileNameArg.endsWith('.browser.ts') || fileNameArg.endsWith('.browser.nonci.ts'):
2020-09-29 15:14:32 +00:00
const tapParserBrowser = await this.runInChrome(fileNameArg);
tapCombinator.addTapParser(tapParserBrowser);
2020-07-07 20:50:29 +00:00
break;
2022-11-08 07:04:46 +00:00
case fileNameArg.endsWith('.both.ts') || fileNameArg.endsWith('.both.nonci.ts'):
2020-10-01 14:12:13 +00:00
console.log('>>>>>>> TEST PART 1: chrome');
const tapParserBothBrowser = await this.runInChrome(fileNameArg);
tapCombinator.addTapParser(tapParserBothBrowser);
console.log(cs(`|`.repeat(16), 'cyan'));
console.log(''); // force new line
console.log('>>>>>>> TEST PART 2: node');
const tapParserBothNode = await this.runInNode(fileNameArg);
tapCombinator.addTapParser(tapParserBothNode);
break;
2020-07-07 20:50:29 +00:00
default:
2020-09-29 15:14:32 +00:00
const tapParserNode = await this.runInNode(fileNameArg);
tapCombinator.addTapParser(tapParserNode);
2020-07-07 20:50:29 +00:00
break;
2018-12-05 23:38:03 +00:00
}
2020-10-01 14:12:13 +00:00
2020-07-07 23:09:17 +00:00
console.log(cs(`^`.repeat(16), 'cyan'));
console.log(''); // force new line
2018-08-03 17:18:42 +00:00
}
2018-08-04 12:23:07 +00:00
tapCombinator.evaluate();
2018-08-03 17:18:42 +00:00
}
2020-07-07 20:50:29 +00:00
public async runInNode(fileNameArg: string): Promise<TapParser> {
console.log(`${cs('=> ', 'blue')} Running ${cs(fileNameArg, 'orange')} in node.js runtime.`);
2020-07-09 22:00:45 +00:00
console.log(`${cs(`= `.repeat(32), 'cyan')}`);
2020-09-29 15:58:26 +00:00
const tapParser = new TapParser(fileNameArg + ':node');
2020-07-07 20:50:29 +00:00
// tsrun options
let tsrunOptions = '';
if (process.argv.includes('--web')) {
tsrunOptions += ' --web';
}
const execResultStreaming = await this.smartshellInstance.execStreamingSilent(
`tsrun ${fileNameArg}${tsrunOptions}`
);
await tapParser.handleTapProcess(execResultStreaming.childProcess);
return tapParser;
}
public async runInChrome(fileNameArg: string): Promise<TapParser> {
console.log(`${cs('=> ', 'blue')} Running ${cs(fileNameArg, 'orange')} in chromium runtime.`);
2020-07-09 22:00:45 +00:00
console.log(`${cs(`= `.repeat(32), 'cyan')}`);
2020-07-07 21:59:07 +00:00
2020-07-07 20:50:29 +00:00
// lets get all our paths sorted
const tsbundleCacheDirPath = plugins.path.join(paths.cwd, './.nogit/tstest_cache');
const bundleFileName = fileNameArg.replace('/', '__') + '.js';
const bundleFilePath = plugins.path.join(tsbundleCacheDirPath, bundleFileName);
2020-07-07 21:59:07 +00:00
2020-07-07 20:50:29 +00:00
// lets bundle the test
2022-03-14 10:49:38 +00:00
await plugins.smartfile.fs.ensureEmptyDir(tsbundleCacheDirPath);
2022-03-16 11:28:50 +00:00
await this.tsbundleInstance.build(process.cwd(), fileNameArg, bundleFilePath, {
2022-11-08 07:04:46 +00:00
bundler: 'esbuild',
2022-03-16 11:28:50 +00:00
});
2020-07-07 21:59:07 +00:00
2020-07-07 20:50:29 +00:00
// lets create a server
2023-07-13 01:19:11 +00:00
const server = new plugins.typedserver.servertools.Server({
2020-07-07 20:50:29 +00:00
cors: true,
2020-07-07 21:59:07 +00:00
port: 3007,
2020-07-07 20:50:29 +00:00
});
2020-07-07 21:59:07 +00:00
server.addRoute(
'/test',
2023-07-13 01:19:11 +00:00
new plugins.typedserver.servertools.Handler('GET', async (req, res) => {
2020-07-07 21:59:07 +00:00
res.type('.html');
res.write(`
2020-07-07 20:50:29 +00:00
<html>
2020-07-07 21:59:07 +00:00
<head>
<script>
globalThis.testdom = true;
</script>
</head>
2020-07-07 20:50:29 +00:00
<body></body>
</html>
`);
2020-07-07 21:59:07 +00:00
res.end();
})
);
2023-07-13 01:19:11 +00:00
server.addRoute('*', new plugins.typedserver.servertools.HandlerStatic(tsbundleCacheDirPath));
2020-07-07 20:50:29 +00:00
await server.start();
2023-11-09 18:00:53 +00:00
// lets handle realtime comms
2023-11-09 20:06:06 +00:00
const tapParser = new TapParser(fileNameArg + ':chrome');
2023-11-09 18:00:53 +00:00
const wss = new plugins.ws.WebSocketServer({ port: 8080 });
wss.on('connection', (ws) => {
ws.on('message', (message) => {
2023-11-09 20:06:06 +00:00
tapParser.handleTapLog(message.toString());
2023-11-09 18:00:53 +00:00
});
});
2020-07-07 20:50:29 +00:00
// lets do the browser bit
await this.smartbrowserInstance.start();
2020-07-07 21:59:07 +00:00
const evaluation = await this.smartbrowserInstance.evaluateOnPage(
`http://localhost:3007/test?bundleName=${bundleFileName}`,
async () => {
2023-11-09 18:00:53 +00:00
// lets enable real time comms
const ws = new WebSocket('ws://localhost:8080');
await new Promise((resolve) => (ws.onopen = resolve));
2020-07-07 21:59:07 +00:00
2023-11-09 18:00:53 +00:00
// Ensure this function is declared with 'async'
const logStore = [];
const originalLog = console.log;
const originalError = console.error;
2020-07-08 00:59:55 +00:00
2023-11-09 18:00:53 +00:00
// Override console methods to capture the logs
2020-07-07 21:59:07 +00:00
console.log = (...args) => {
2023-11-09 18:00:53 +00:00
logStore.push(args.join(' '));
ws.send(args.join(' '));
originalLog(...args);
2020-07-07 21:59:07 +00:00
};
console.error = (...args) => {
2023-11-09 18:00:53 +00:00
logStore.push(args.join(' '));
ws.send(args.join(' '));
originalError(...args);
2020-07-07 21:59:07 +00:00
};
2023-11-09 18:00:53 +00:00
2020-07-09 22:00:45 +00:00
const bundleName = new URLSearchParams(window.location.search).get('bundleName');
2023-11-09 18:00:53 +00:00
originalLog(`::TSTEST IN CHROMIUM:: Relevant Script name is: ${bundleName}`);
2020-07-12 00:47:41 +00:00
try {
2023-11-09 18:00:53 +00:00
// Dynamically import the test module
const testModule = await import(`/${bundleName}`);
if (testModule && testModule.runTest) {
// Execute the exported test function
await testModule.runTest();
} else {
2024-03-07 12:10:57 +00:00
console.error('Test module does not export runTest function.');
2023-11-09 18:00:53 +00:00
}
2020-07-07 21:59:07 +00:00
} catch (err) {
2024-03-07 12:10:57 +00:00
console.error(err);
2020-07-07 21:59:07 +00:00
}
2020-07-12 00:47:41 +00:00
2023-11-09 18:00:53 +00:00
return logStore.join('\n');
2020-07-07 21:59:07 +00:00
}
);
2020-07-07 20:50:29 +00:00
await this.smartbrowserInstance.stop();
await server.stop();
2023-11-09 18:00:53 +00:00
wss.close();
2020-07-12 00:47:41 +00:00
console.log(
`${cs('=> ', 'blue')} Stopped ${cs(fileNameArg, 'orange')} chromium instance and server.`
);
2020-07-07 20:50:29 +00:00
// lets create the tap parser
2023-11-09 20:06:06 +00:00
await tapParser.evaluateFinalResult();
2020-07-07 20:50:29 +00:00
return tapParser;
}
public async runInDeno() {}
2018-08-03 17:18:42 +00:00
}