2022-07-19 21:55:05 +02:00
|
|
|
import { expect, type Page } from '@playwright/test'
|
2023-05-14 13:52:47 +02:00
|
|
|
import { execFile } from 'node:child_process'
|
|
|
|
import { promisify } from 'node:util'
|
2023-05-25 10:17:01 +02:00
|
|
|
import { getFileChecksum } from './files'
|
2023-05-14 13:52:47 +02:00
|
|
|
|
|
|
|
const exec = promisify(execFile)
|
2022-07-19 21:55:05 +02:00
|
|
|
|
2023-05-25 10:17:01 +02:00
|
|
|
type CreatePage = {
|
|
|
|
views?: number
|
|
|
|
expiration?: number
|
|
|
|
error?: string
|
|
|
|
password?: string
|
2024-08-27 00:09:51 +02:00
|
|
|
} & (
|
|
|
|
| {
|
|
|
|
text: string
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
files: string[]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
async function createNote(page: Page, options: CreatePage): Promise<void> {
|
2022-07-19 21:55:05 +02:00
|
|
|
await page.goto('/')
|
|
|
|
|
2024-08-27 00:09:51 +02:00
|
|
|
if ('text' in options) {
|
2023-05-25 10:17:01 +02:00
|
|
|
await page.getByTestId('text-field').fill(options.text)
|
2022-07-19 21:55:05 +02:00
|
|
|
} else if (options.files) {
|
2023-05-25 10:17:01 +02:00
|
|
|
await page.getByTestId('switch-file').click()
|
2022-07-19 21:55:05 +02:00
|
|
|
|
|
|
|
const [fileChooser] = await Promise.all([
|
|
|
|
page.waitForEvent('filechooser'),
|
|
|
|
page.locator('text=No Files Selected').click(),
|
|
|
|
])
|
|
|
|
await fileChooser.setFiles(options.files)
|
|
|
|
}
|
|
|
|
|
2023-05-25 10:17:01 +02:00
|
|
|
if (options.views || options.expiration || options.password) await page.getByTestId('switch-advanced').click()
|
2022-07-19 21:55:05 +02:00
|
|
|
if (options.views) {
|
2023-05-25 10:17:01 +02:00
|
|
|
await page.getByTestId('field-views').fill(options.views.toString())
|
2024-08-27 00:09:51 +02:00
|
|
|
}
|
|
|
|
if (options.expiration) {
|
2023-05-25 10:17:01 +02:00
|
|
|
await page.getByTestId('switch-advanced-toggle').click()
|
|
|
|
await page.getByTestId('field-expiration').fill(options.expiration.toString())
|
|
|
|
}
|
|
|
|
if (options.password) {
|
|
|
|
await page.getByTestId('custom-password').click()
|
|
|
|
await page.getByTestId('password').fill(options.password)
|
2022-07-19 21:55:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
await page.locator('button:has-text("create")').click()
|
2024-08-27 00:09:51 +02:00
|
|
|
}
|
2022-07-19 21:55:05 +02:00
|
|
|
|
2024-08-27 00:09:51 +02:00
|
|
|
export async function createNoteSuccessfully(page: Page, options: CreatePage): Promise<string> {
|
|
|
|
await createNote(page, options)
|
2023-05-25 10:17:01 +02:00
|
|
|
return await page.getByTestId('share-link').inputValue()
|
|
|
|
}
|
|
|
|
|
2024-08-27 00:09:51 +02:00
|
|
|
export async function createNoteError(page: Page, options: CreatePage, error: string): Promise<void> {
|
|
|
|
await createNote(page, options)
|
|
|
|
await expect(page.locator('._toastContainer')).toContainText(error)
|
|
|
|
}
|
|
|
|
|
2023-05-25 10:17:01 +02:00
|
|
|
type CheckLinkBase = {
|
|
|
|
link: string
|
|
|
|
text: string
|
|
|
|
password?: string
|
2022-07-19 21:55:05 +02:00
|
|
|
}
|
|
|
|
|
2023-05-25 10:17:01 +02:00
|
|
|
export async function checkLinkForDownload(page: Page, options: CheckLinkBase & { checksum: string }) {
|
2022-07-19 21:55:05 +02:00
|
|
|
await page.goto('/')
|
2023-05-25 10:17:01 +02:00
|
|
|
await page.goto(options.link)
|
|
|
|
if (options.password) await page.getByTestId('show-note-password').fill(options.password)
|
|
|
|
await page.getByTestId('show-note-button').click()
|
2022-07-19 21:55:05 +02:00
|
|
|
|
|
|
|
const [download] = await Promise.all([
|
|
|
|
page.waitForEvent('download'),
|
2023-05-25 10:17:01 +02:00
|
|
|
page.getByTestId(`result`).locator(`text=${options.text}`).click(),
|
2022-07-19 21:55:05 +02:00
|
|
|
])
|
|
|
|
const path = await download.path()
|
|
|
|
if (!path) throw new Error('Download failed')
|
|
|
|
const cs = await getFileChecksum(path)
|
2024-08-27 00:09:51 +02:00
|
|
|
expect(cs).toBe(options.checksum)
|
2022-07-19 21:55:05 +02:00
|
|
|
}
|
2023-05-25 10:17:01 +02:00
|
|
|
|
|
|
|
export async function checkLinkForText(page: Page, options: CheckLinkBase) {
|
2022-07-19 21:55:05 +02:00
|
|
|
await page.goto('/')
|
2023-05-25 10:17:01 +02:00
|
|
|
await page.goto(options.link)
|
|
|
|
if (options.password) await page.getByTestId('show-note-password').fill(options.password)
|
|
|
|
await page.getByTestId('show-note-button').click()
|
|
|
|
const text = await page.getByTestId('result').locator('.note').innerText()
|
2024-08-27 00:09:51 +02:00
|
|
|
expect(text).toContain(options.text)
|
2022-07-19 21:55:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function checkLinkDoesNotExist(page: Page, link: string) {
|
|
|
|
await page.goto('/') // Required due to firefox: https://github.com/microsoft/playwright/issues/15781
|
|
|
|
await page.goto(link)
|
|
|
|
await expect(page.locator('main')).toContainText('note was not found or was already deleted')
|
|
|
|
}
|
|
|
|
|
2023-05-14 13:52:47 +02:00
|
|
|
export async function CLI(...args: string[]) {
|
2024-03-03 01:35:17 +01:00
|
|
|
return await exec('./packages/cli/dist/cli.cjs', args, {
|
2023-05-14 13:52:47 +02:00
|
|
|
env: {
|
|
|
|
...process.env,
|
2024-08-23 14:27:17 +02:00
|
|
|
CRYPTGEON_SERVER: 'http://localhost:3000',
|
2023-05-14 13:52:47 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2023-05-25 10:17:01 +02:00
|
|
|
|
|
|
|
export function getLinkFromCLI(output: string): string {
|
|
|
|
const match = output.match(/(https?:\/\/[^\s]+)/)
|
|
|
|
if (!match) throw new Error('No link found in CLI output')
|
|
|
|
return match[0]
|
|
|
|
}
|