1 |
|
2 |
|
3 |
|
4 | import jsverify from 'jsverify-es-module';
|
5 | const child_process = require('child_process');
|
6 | const uuid = require('uuid/v1');
|
7 | const path = require('path');
|
8 | const fs = require('fs-extra');
|
9 |
|
10 | const PAGE_LOADED = 'PAGE_LOADED';
|
11 | const GET_RESULT = 'GET_RESULT';
|
12 | const RESULT = 'RESULT';
|
13 |
|
14 | import {
|
15 | arbPort,
|
16 | loadZwitterion,
|
17 | getPromisePieces,
|
18 | arbScriptElementsInfo,
|
19 | wait
|
20 | } from './test-utilities';
|
21 |
|
22 | class ZwitterionTest extends HTMLElement {
|
23 | prepareTests(test: any) {
|
24 | test('Load an arbitrary index html file and all of its scripts', [jsverify.number, arbPort, arbScriptElementsInfo(true)], async (arbNumber: number, arbPort: number, arbScriptElementsInfo: any) => {
|
25 | for (let i=0; i < arbScriptElementsInfo.length; i++) {
|
26 |
|
27 | const arbScriptElementInfo = arbScriptElementsInfo[i];
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | await fs.outputFile(arbScriptElementInfo.srcPath, arbScriptElementInfo.contents);
|
38 |
|
39 | for (let j=0; j < arbScriptElementInfo.moduleDependencies.length; j++) {
|
40 | const moduleDependency = arbScriptElementInfo.moduleDependencies[j];
|
41 | await fs.outputFile(moduleDependency.srcPath, moduleDependency.contents);
|
42 | }
|
43 |
|
44 | }
|
45 |
|
46 | const html = `
|
47 | <!DOCTYPE html>
|
48 |
|
49 | <html>
|
50 | <head>
|
51 | <script>
|
52 | window.addEventListener('load', (e) => {
|
53 | window.opener.postMessage({
|
54 | type: '${PAGE_LOADED}'
|
55 | });
|
56 | });
|
57 |
|
58 | window.addEventListener('message', (e) => {
|
59 | if (e.data === '${GET_RESULT}') {
|
60 | window.opener.postMessage({
|
61 | type: '${RESULT}',
|
62 | body: document.body.innerText,
|
63 | zwitterionTest: window.ZWITTERION_TEST
|
64 | });
|
65 | }
|
66 | });
|
67 | </script>
|
68 |
|
69 | ${arbScriptElementsInfo.map((arbScriptElementInfo: any) => {
|
70 | return arbScriptElementInfo.element;
|
71 | }).join('\n')}
|
72 | </head>
|
73 |
|
74 | <body>${arbNumber}</body>
|
75 | </html>
|
76 | `;
|
77 | await fs.writeFile('./index.html', html);
|
78 |
|
79 | const zwitterionProcess = await loadZwitterion(arbPort);
|
80 | const {thePromise, theResolve} = getPromisePieces();
|
81 | window.addEventListener('message', async (e) => {
|
82 | if (e.data.type === PAGE_LOADED) {
|
83 |
|
84 | testWindow.postMessage(GET_RESULT, `http://localhost:${arbPort}`);
|
85 | }
|
86 |
|
87 | if (e.data.type === RESULT) {
|
88 |
|
89 | const bodyHasCorrectContent = +e.data.body === arbNumber;
|
90 |
|
91 |
|
92 | const allScriptsExecuted = (e.data.zwitterionTest === undefined && arbScriptElementsInfo.length === 0) ||
|
93 | e.data.zwitterionTest && arbScriptElementsInfo.filter((arbScriptElementInfo: any) => {
|
94 | return e.data.zwitterionTest[arbScriptElementInfo.srcPath] &&
|
95 | arbScriptElementInfo.moduleDependencies.filter((moduleDependency: any) => {
|
96 | return e.data.zwitterionTest[moduleDependency.srcPath];
|
97 | }).length === arbScriptElementInfo.moduleDependencies.length;
|
98 | }).length === arbScriptElementsInfo.length;
|
99 |
|
100 | if (
|
101 | bodyHasCorrectContent &&
|
102 | allScriptsExecuted
|
103 | ) {
|
104 | theResolve(true);
|
105 | }
|
106 | else {
|
107 | theResolve(false);
|
108 | }
|
109 | }
|
110 | });
|
111 |
|
112 | const testWindow = window.open(`http://localhost:${arbPort}`, '_blank');
|
113 |
|
114 | const result = await thePromise;
|
115 |
|
116 |
|
117 |
|
118 | if (result) {
|
119 | (<any> zwitterionProcess).kill('SIGINT');
|
120 | await fs.unlink('./index.html');
|
121 | for (let i=0; i < arbScriptElementsInfo.length; i++) {
|
122 | const arbScriptElementInfo = arbScriptElementsInfo[i];
|
123 | await fs.remove(`./${arbScriptElementInfo.topLevelDirectory || arbScriptElementInfo.srcPath}`);
|
124 |
|
125 | for (let j=0; j < arbScriptElementInfo.moduleDependencies.length; j++) {
|
126 | const moduleDependency = arbScriptElementInfo.moduleDependencies[j];
|
127 | await fs.remove(`./${moduleDependency.topLevelDirectory || moduleDependency.srcPath}`);
|
128 | }
|
129 | }
|
130 | }
|
131 |
|
132 | return result;
|
133 | });
|
134 |
|
135 |
|
136 |
|
137 | }
|
138 | }
|
139 |
|
140 | window.customElements.define('zwitterion-test', ZwitterionTest);
|