|
| 1 | +/// <reference types="node" /> |
| 2 | +import { readdirSync, readFileSync, rmSync } from 'node:fs'; |
| 3 | +import type { Readable } from 'node:stream'; |
| 4 | +import { test, expect } from '@playwright/test'; |
| 5 | +import * as unzipper from 'unzipper'; |
| 6 | +import { theme } from '../../packages/theme/src/theme'; |
| 7 | + |
| 8 | +test('user can change theme', async ({ page }) => { |
| 9 | + await page.goto('/'); |
| 10 | + |
| 11 | + const heading = page.getByRole('heading', { level: 1 }); |
| 12 | + const html = page.locator('html'); |
| 13 | + |
| 14 | + // default light theme |
| 15 | + await expect(html).toHaveAttribute('data-theme', 'light'); |
| 16 | + await expect(heading).toHaveCSS('color', hexToRGB(theme.colors.gray[800])); |
| 17 | + |
| 18 | + await page.getByRole('navigation').getByRole('button', { name: 'Toggle Theme' }).click(); |
| 19 | + |
| 20 | + await expect(html).toHaveAttribute('data-theme', 'dark'); |
| 21 | + await expect(heading).toHaveCSS('color', hexToRGB(theme.colors.gray[200])); |
| 22 | +}); |
| 23 | + |
| 24 | +test('user can download project as zip', async ({ page }) => { |
| 25 | + await page.goto('/', { waitUntil: 'networkidle' }); |
| 26 | + |
| 27 | + const downloadPromise = page.waitForEvent('download'); |
| 28 | + await page.getByRole('navigation').getByRole('button', { name: 'Download lesson as zip-file' }).click(); |
| 29 | + |
| 30 | + const download = await downloadPromise; |
| 31 | + expect(download.suggestedFilename()).toBe('tests-file-tree-allow-edits-disabled.zip'); |
| 32 | + |
| 33 | + const stream = await download.createReadStream(); |
| 34 | + const files = await unzip(stream); |
| 35 | + |
| 36 | + expect(files).toMatchObject({ |
| 37 | + './tutorial/file-on-template.js': "export default 'This file is present on template';\n", |
| 38 | + './tutorial/first-level/file.js': "export default 'File in first level';\n", |
| 39 | + './tutorial/first-level/second-level/file.js': "export default 'File in second level';\n", |
| 40 | + }); |
| 41 | + |
| 42 | + expect(files['./tutorial/index.mjs']).toMatch("import http from 'node:http'"); |
| 43 | +}); |
| 44 | + |
| 45 | +function hexToRGB(hex: string) { |
| 46 | + return `rgb(${parseInt(hex.slice(1, 3), 16)}, ${parseInt(hex.slice(3, 5), 16)}, ${parseInt(hex.slice(5, 7), 16)})`; |
| 47 | +} |
| 48 | + |
| 49 | +async function unzip(stream: Readable) { |
| 50 | + await stream.pipe(unzipper.Extract({ path: './downloads' })).promise(); |
| 51 | + |
| 52 | + const files = readDirectoryContents('./downloads'); |
| 53 | + rmSync('./downloads', { recursive: true }); |
| 54 | + |
| 55 | + return files.reduce( |
| 56 | + (all, current) => ({ |
| 57 | + ...all, |
| 58 | + [current.name.replace('/downloads', '')]: current.content, |
| 59 | + }), |
| 60 | + {}, |
| 61 | + ); |
| 62 | +} |
| 63 | + |
| 64 | +function readDirectoryContents(directory: string) { |
| 65 | + const files: { name: string; content: string }[] = []; |
| 66 | + |
| 67 | + for (const entry of readdirSync(directory, { withFileTypes: true })) { |
| 68 | + const name = `${directory}/${entry.name}`; |
| 69 | + |
| 70 | + if (entry.isFile()) { |
| 71 | + files.push({ name, content: readFileSync(name, 'utf-8') }); |
| 72 | + } else if (entry.isDirectory()) { |
| 73 | + files.push(...readDirectoryContents(name)); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return files; |
| 78 | +} |
0 commit comments