|
| 1 | +import { expect, Page, test } from '@playwright/test'; |
| 2 | + |
| 3 | +import { readFileSync } from 'fs'; |
| 4 | +import { join } from 'path'; |
| 5 | + |
| 6 | +import { gotoHtmlWidget } from './utlis/server/playground'; |
| 7 | + |
| 8 | +import { RESULT_SELECTOR, WIDGET_SELECTOR } from './utlis/selectors'; |
| 9 | + |
| 10 | +import { prepareNetwork, printlnCode } from './utlis'; |
| 11 | +import { mockRunRequest, waitRunRequest } from './utlis/mocks/compiler'; |
| 12 | +import { runButton } from './utlis/interactions'; |
| 13 | +import { makeJSPrintCode } from './utlis/mocks/result'; |
| 14 | + |
| 15 | +const OUTPUTS = Object.freeze({ |
| 16 | + 'js-ir': { |
| 17 | + jsCode: makeJSPrintCode('Hello, world!'), |
| 18 | + errors: { 'File.kt': [] }, |
| 19 | + exception: null, |
| 20 | + text: '<outStream>Hello, world!\n</outStream>', |
| 21 | + }, |
| 22 | + wasm: JSON.parse( |
| 23 | + readFileSync(join(__dirname, 'utlis/mocks/wasm.json'), 'utf-8'), |
| 24 | + ), |
| 25 | +}); |
| 26 | + |
| 27 | +const VERSIONS = [ |
| 28 | + { version: '1.3.10' }, |
| 29 | + { version: '1.9.20', latestStable: true }, |
| 30 | + { version: '2.0.1' }, |
| 31 | +] as const; |
| 32 | + |
| 33 | +test.describe('platform restrictions', () => { |
| 34 | + test.beforeEach(async ({ page, baseURL }) => { |
| 35 | + await prepareNetwork(page, baseURL, { |
| 36 | + versions: (route) => |
| 37 | + route.fulfill({ |
| 38 | + body: JSON.stringify(VERSIONS), |
| 39 | + }), |
| 40 | + }); // offline mode |
| 41 | + }); |
| 42 | + |
| 43 | + test('JS_IR for unsupported version', async ({ page }) => { |
| 44 | + await shouldFailedRun( |
| 45 | + page, |
| 46 | + 'js-ir', |
| 47 | + '1.3.10', |
| 48 | + 'JS IR compiler backend accessible only since 1.5.0 version', |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + test('JS_IR for supported by minor version', async ({ page }) => { |
| 53 | + await shouldSuccessRun(page, 'js-ir', '1.9.0'); |
| 54 | + }); |
| 55 | + |
| 56 | + test('JS_IR for supported by major version', async ({ page }) => { |
| 57 | + await shouldSuccessRun(page, 'js-ir', '2.0.1'); |
| 58 | + }); |
| 59 | + |
| 60 | + test('WASM for unsupported version', async ({ page }) => { |
| 61 | + await shouldFailedRun( |
| 62 | + page, |
| 63 | + 'wasm', |
| 64 | + '1.3.10', |
| 65 | + 'Wasm compiler backend accessible only since 1.9.0 version', |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + test('WASM for supported by minor version', async ({ page, browserName }) => { |
| 70 | + test.skip( |
| 71 | + browserName !== 'chromium', |
| 72 | + "WASM doesn't supported in this browser", |
| 73 | + ); |
| 74 | + await shouldSuccessRun(page, 'wasm', '1.9.0'); |
| 75 | + }); |
| 76 | + |
| 77 | + test('WASM for supported by major version', async ({ page, browserName }) => { |
| 78 | + test.skip( |
| 79 | + browserName !== 'chromium', |
| 80 | + "WASM doesn't supported in this browser", |
| 81 | + ); |
| 82 | + await shouldSuccessRun(page, 'wasm', '2.0.1'); |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +async function shouldSuccessRun( |
| 87 | + page: Page, |
| 88 | + platform: keyof typeof OUTPUTS, |
| 89 | + version: string, |
| 90 | +) { |
| 91 | + await gotoHtmlWidget( |
| 92 | + page, |
| 93 | + { selector: 'code', version: version }, |
| 94 | + /* language=html */ ` |
| 95 | + <code data-target-platform='${platform}'>${printlnCode( |
| 96 | + 'Hello, world!', |
| 97 | + )}</code> |
| 98 | + `, |
| 99 | + ); |
| 100 | + |
| 101 | + const resolveRun = await mockRunRequest(page); |
| 102 | + |
| 103 | + const editor = page.locator(WIDGET_SELECTOR); |
| 104 | + |
| 105 | + await Promise.all([waitRunRequest(page), runButton(editor)]); |
| 106 | + |
| 107 | + resolveRun({ |
| 108 | + json: Object.freeze(OUTPUTS[platform]), |
| 109 | + }); |
| 110 | + |
| 111 | + // playground loaded |
| 112 | + await expect(editor.locator(RESULT_SELECTOR)).toBeVisible(); |
| 113 | + await expect(editor.locator(RESULT_SELECTOR)).toContainText('Hello, world!'); |
| 114 | +} |
| 115 | + |
| 116 | +async function shouldFailedRun( |
| 117 | + page: Page, |
| 118 | + platform: string, |
| 119 | + version: string, |
| 120 | + text: string, |
| 121 | +) { |
| 122 | + await gotoHtmlWidget( |
| 123 | + page, |
| 124 | + { selector: 'code', version: version }, |
| 125 | + /* language=html */ ` |
| 126 | + <code data-target-platform='${platform}'>${printlnCode( |
| 127 | + 'Hello, world!', |
| 128 | + )}</code> |
| 129 | + `, |
| 130 | + ); |
| 131 | + |
| 132 | + const editor = page.locator(WIDGET_SELECTOR); |
| 133 | + await runButton(editor); |
| 134 | + |
| 135 | + await expect(editor.locator(RESULT_SELECTOR)).toBeVisible(); |
| 136 | + await expect( |
| 137 | + editor.locator(RESULT_SELECTOR).locator('.test-fail'), |
| 138 | + ).toContainText(text); |
| 139 | +} |
0 commit comments