From a2cf75101267f87d8f525206590140bbd63c063d Mon Sep 17 00:00:00 2001 From: Niccolo Borgioli Date: Fri, 12 May 2023 20:22:49 +0200 Subject: [PATCH] testing --- package.json | 6 ++++-- playwright.config.ts | 8 +++---- test/cli/text/simple.spec.ts | 13 +++++++++++ test/cross/text/simple.spec.ts | 19 ++++++++++++++++ test/file/multiple.spec.ts | 11 ---------- test/file/simple.spec.ts | 24 --------------------- test/file/too-big.spec.ts | 8 ------- test/text.ts | 37 -------------------------------- test/text/expiration.spec.ts | 14 ------------ test/text/simple.spec.ts | 8 ------- test/text/views.spec.ts | 18 ---------------- test/utils.ts | 13 +++++++++++ test/{ => web}/file/files.ts | 0 test/web/file/multiple.spec.ts | 13 +++++++++++ test/web/file/simple.spec.ts | 26 ++++++++++++++++++++++ test/web/file/too-big.spec.ts | 10 +++++++++ test/web/text/expiration.spec.ts | 16 ++++++++++++++ test/web/text/simple.spec.ts | 10 +++++++++ test/web/text/views.spec.ts | 20 +++++++++++++++++ 19 files changed, 148 insertions(+), 126 deletions(-) create mode 100644 test/cli/text/simple.spec.ts create mode 100644 test/cross/text/simple.spec.ts delete mode 100644 test/file/multiple.spec.ts delete mode 100644 test/file/simple.spec.ts delete mode 100644 test/file/too-big.spec.ts delete mode 100644 test/text.ts delete mode 100644 test/text/expiration.spec.ts delete mode 100644 test/text/simple.spec.ts delete mode 100644 test/text/views.spec.ts rename test/{ => web}/file/files.ts (100%) create mode 100644 test/web/file/multiple.spec.ts create mode 100644 test/web/file/simple.spec.ts create mode 100644 test/web/file/too-big.spec.ts create mode 100644 test/web/text/expiration.spec.ts create mode 100644 test/web/text/simple.spec.ts create mode 100644 test/web/text/views.spec.ts diff --git a/package.json b/package.json index ad596ef..c610986 100644 --- a/package.json +++ b/package.json @@ -4,10 +4,12 @@ "dev:docker": "docker-compose -f docker-compose.dev.yaml up redis", "dev:packages": "pnpm --parallel run dev", "dev": "run-p dev:*", + "docker:up": "docker compose -f docker-compose.dev.yaml up", + "docker:build": "docker compose -f docker-compose.dev.yaml build", "test": "playwright test --project chrome firefox safari", "test:local": "playwright test --project local", - "test:server": "docker compose -f docker-compose.dev.yaml up", - "test:prepare": "docker compose -f docker-compose.dev.yaml build", + "test:server": "run-s docker:up", + "test:prepare": "run-p build docker:build", "build": "pnpm run --recursive --filter=!@cryptgeon/backend build" }, "devDependencies": { diff --git a/playwright.config.ts b/playwright.config.ts index e0fc696..7618e7e 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -21,10 +21,10 @@ const config: PlaywrightTestConfig = { { name: 'chrome', use: { ...devices['Desktop Chrome'] } }, { name: 'firefox', use: { ...devices['Desktop Firefox'] } }, { name: 'safari', use: { ...devices['Desktop Safari'] } }, - { - name: 'local', - use: { ...devices['Desktop Chrome'] }, - }, + + { name: 'cli', use: { ...devices['Desktop Chrome'] }, grep: [/@cli/] }, + { name: 'web', use: { ...devices['Desktop Chrome'] }, grep: [/@web/] }, + { name: 'cross', use: { ...devices['Desktop Chrome'] }, grep: [/@cross/] }, ], } diff --git a/test/cli/text/simple.spec.ts b/test/cli/text/simple.spec.ts new file mode 100644 index 0000000..0434f25 --- /dev/null +++ b/test/cli/text/simple.spec.ts @@ -0,0 +1,13 @@ +import { test } from '@playwright/test' +import { CLI } from '../../utils' + +test.describe('text @cli', () => { + test('simple', async ({ page }) => { + const text = `Endless prejudice endless play derive joy eternal-return selfish burying. Of decieve play pinnacle faith disgust. Spirit reason salvation burying strong of joy ascetic selfish against merciful sea truth. Ubermensch moral prejudice derive chaos mountains ubermensch justice philosophy justice ultimate joy ultimate transvaluation. Virtues convictions war ascetic eternal-return spirit. Ubermensch transvaluation noble revaluation sexuality intentions salvation endless decrepit hope noble fearful. Justice ideal ultimate snare god joy evil sexuality insofar gains oneself ideal.` + const note = await CLI('send', 'text', text) + const link = note.stdout.trim().replace(/(.|\s)*http/g, 'http') + + const retrieved = await CLI('open', link) + test.expect(retrieved.stdout.trim()).toBe(text) + }) +}) diff --git a/test/cross/text/simple.spec.ts b/test/cross/text/simple.spec.ts new file mode 100644 index 0000000..676c0c0 --- /dev/null +++ b/test/cross/text/simple.spec.ts @@ -0,0 +1,19 @@ +import { test } from '@playwright/test' +import { CLI, checkLinkForText, createNote } from '../../utils' + +const text = `Endless prejudice endless play derive joy eternal-return selfish burying. Of decieve play pinnacle faith disgust. Spirit reason salvation burying strong of joy ascetic selfish against merciful sea truth. Ubermensch moral prejudice derive chaos mountains ubermensch justice philosophy justice ultimate joy ultimate transvaluation. Virtues convictions war ascetic eternal-return spirit. Ubermensch transvaluation noble revaluation sexuality intentions salvation endless decrepit hope noble fearful. Justice ideal ultimate snare god joy evil sexuality insofar gains oneself ideal.` + +test.describe('text @cross', () => { + test('cli to web', async ({ page }) => { + const note = await CLI('send', 'text', text) + const link = note.stdout.trim().replace(/(.|\s)*http/g, 'http') + + await checkLinkForText(page, link, text) + }) + + test('web to cli', async ({ page }) => { + const link = await createNote(page, { text }) + const retrieved = await CLI('open', link) + test.expect(retrieved.stdout.trim()).toBe(text) + }) +}) diff --git a/test/file/multiple.spec.ts b/test/file/multiple.spec.ts deleted file mode 100644 index d25984c..0000000 --- a/test/file/multiple.spec.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { test } from '@playwright/test' -import { checkLinkForDownload, createNote, getFileChecksum } from '../utils' -import Files from './files' - -test('multiple', async ({ page }) => { - const files = [Files.PDF, Files.Image] - const checksums = await Promise.all(files.map(getFileChecksum)) - const link = await createNote(page, { files, views: 2 }) - await checkLinkForDownload(page, link, 'image.jpg', checksums[1]) - await checkLinkForDownload(page, link, 'AES.pdf', checksums[0]) -}) diff --git a/test/file/simple.spec.ts b/test/file/simple.spec.ts deleted file mode 100644 index 8d5e251..0000000 --- a/test/file/simple.spec.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { test } from '@playwright/test' -import { checkLinkDoesNotExist, checkLinkForDownload, checkLinkForText, createNote, getFileChecksum } from '../utils' -import Files from './files' - -test('simple pdf', async ({ page }) => { - const files = [Files.PDF] - const link = await createNote(page, { files }) - await checkLinkForText(page, link, 'AES.pdf') - await checkLinkDoesNotExist(page, link) -}) - -test('pdf content', async ({ page }) => { - const files = [Files.PDF] - const checksum = await getFileChecksum(files[0]) - const link = await createNote(page, { files }) - await checkLinkForDownload(page, link, 'AES.pdf', checksum) -}) - -test('image content', async ({ page }) => { - const files = [Files.Image] - const checksum = await getFileChecksum(files[0]) - const link = await createNote(page, { files }) - await checkLinkForDownload(page, link, 'image.jpg', checksum) -}) diff --git a/test/file/too-big.spec.ts b/test/file/too-big.spec.ts deleted file mode 100644 index d5a5578..0000000 --- a/test/file/too-big.spec.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { test } from '@playwright/test' -import { createNote } from '../utils' -import Files from './files' - -test.skip('to big zip', async ({ page }) => { - const files = [Files.Zip] - const link = await createNote(page, { files, error: 'note is to big' }) -}) diff --git a/test/text.ts b/test/text.ts deleted file mode 100644 index 98dc197..0000000 --- a/test/text.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { expect, test, type Page } from '@playwright/test' - -async function createNote(page: Page, text: string): Promise { - await page.goto('/') - await page.locator('textarea').click() - await page.locator('textarea').fill(text) - await page.locator('button:has-text("create")').click() - - await page.locator('[data-testid="share-link"]').click() - const shareLink = await page.locator('[data-testid="share-link"]').inputValue() - return shareLink -} - -async function checkLinkForText(page: Page, link: string, text: string) { - await page.goto(link) - await page.locator('[data-testid="show-note-button"]').click() - expect(await page.locator('[data-testid="result"] >> .note').innerText()).toBe(text) -} - -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') -} - -test('simple', async ({ page }) => { - const text = `Endless prejudice endless play derive joy eternal-return selfish burying. Of decieve play pinnacle faith disgust. Spirit reason salvation burying strong of joy ascetic selfish against merciful sea truth. Ubermensch moral prejudice derive chaos mountains ubermensch justice philosophy justice ultimate joy ultimate transvaluation. Virtues convictions war ascetic eternal-return spirit. Ubermensch transvaluation noble revaluation sexuality intentions salvation endless decrepit hope noble fearful. Justice ideal ultimate snare god joy evil sexuality insofar gains oneself ideal.` - const shareLink = await createNote(page, text) - await checkLinkForText(page, shareLink, text) -}) - -test('only shown once', async ({ page }) => { - const text = `Christian victorious reason suicide dead. Right ultimate gains god hope truth burying selfish society joy. Ultimate.` - const shareLink = await createNote(page, text) - await checkLinkForText(page, shareLink, text) - await checkLinkDoesNotExist(page, shareLink) -}) diff --git a/test/text/expiration.spec.ts b/test/text/expiration.spec.ts deleted file mode 100644 index fb9bc21..0000000 --- a/test/text/expiration.spec.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { test } from '@playwright/test' -import { checkLinkDoesNotExist, checkLinkForText, createNote } from '../utils' - -test('1 minute', async ({ page }) => { - const text = `Virtues value ascetic revaluation sea dead strong burying.` - const minutes = 1 - const timeout = minutes * 60_000 - test.setTimeout(timeout * 2) - const shareLink = await createNote(page, { text, expiration: minutes }) - await checkLinkForText(page, shareLink, text) - await checkLinkForText(page, shareLink, text) - await page.waitForTimeout(timeout) - await checkLinkDoesNotExist(page, shareLink) -}) diff --git a/test/text/simple.spec.ts b/test/text/simple.spec.ts deleted file mode 100644 index 6db0e5f..0000000 --- a/test/text/simple.spec.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { test } from '@playwright/test' -import { checkLinkForText, createNote } from '../utils' - -test('simple', async ({ page }) => { - const text = `Endless prejudice endless play derive joy eternal-return selfish burying. Of decieve play pinnacle faith disgust. Spirit reason salvation burying strong of joy ascetic selfish against merciful sea truth. Ubermensch moral prejudice derive chaos mountains ubermensch justice philosophy justice ultimate joy ultimate transvaluation. Virtues convictions war ascetic eternal-return spirit. Ubermensch transvaluation noble revaluation sexuality intentions salvation endless decrepit hope noble fearful. Justice ideal ultimate snare god joy evil sexuality insofar gains oneself ideal.` - const shareLink = await createNote(page, { text }) - await checkLinkForText(page, shareLink, text) -}) diff --git a/test/text/views.spec.ts b/test/text/views.spec.ts deleted file mode 100644 index 0e9b451..0000000 --- a/test/text/views.spec.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { test } from '@playwright/test' -import { checkLinkDoesNotExist, checkLinkForText, createNote } from '../utils' - -test('only shown once', async ({ page }) => { - const text = `Christian victorious reason suicide dead. Right ultimate gains god hope truth burying selfish society joy. Ultimate.` - const shareLink = await createNote(page, { text }) - await checkLinkForText(page, shareLink, text) - await checkLinkDoesNotExist(page, shareLink) -}) - -test('view 3 times', async ({ page }) => { - const text = `Justice holiest overcome fearful strong ultimate holiest christianity.` - const shareLink = await createNote(page, { text, views: 3 }) - await checkLinkForText(page, shareLink, text) - await checkLinkForText(page, shareLink, text) - await checkLinkForText(page, shareLink, text) - await checkLinkDoesNotExist(page, shareLink) -}) diff --git a/test/utils.ts b/test/utils.ts index 7c841d2..99d01d8 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -1,6 +1,10 @@ import { expect, type Page } from '@playwright/test' import { createHash } from 'crypto' import { readFile } from 'fs/promises' +import { execFile } from 'node:child_process' +import { promisify } from 'node:util' + +const exec = promisify(execFile) type CreatePage = { text?: string; files?: string[]; views?: number; expiration?: number; error?: string } export async function createNote(page: Page, options: CreatePage): Promise { @@ -69,3 +73,12 @@ export async function getFileChecksum(file: string) { const hash = createHash('sha3-256').update(buffer).digest('hex') return hash } + +export async function CLI(...args: string[]) { + return await exec('./packages/cli/dist/index.cjs', args, { + env: { + ...process.env, + CRYPTGEON_SERVER: 'http://localhost:1234', + }, + }) +} diff --git a/test/file/files.ts b/test/web/file/files.ts similarity index 100% rename from test/file/files.ts rename to test/web/file/files.ts diff --git a/test/web/file/multiple.spec.ts b/test/web/file/multiple.spec.ts new file mode 100644 index 0000000..d3082fe --- /dev/null +++ b/test/web/file/multiple.spec.ts @@ -0,0 +1,13 @@ +import { test } from '@playwright/test' +import { checkLinkForDownload, createNote, getFileChecksum } from '../../utils' +import Files from './files' + +test.describe('@web', () => { + test('multiple', async ({ page }) => { + const files = [Files.PDF, Files.Image] + const checksums = await Promise.all(files.map(getFileChecksum)) + const link = await createNote(page, { files, views: 2 }) + await checkLinkForDownload(page, link, 'image.jpg', checksums[1]) + await checkLinkForDownload(page, link, 'AES.pdf', checksums[0]) + }) +}) diff --git a/test/web/file/simple.spec.ts b/test/web/file/simple.spec.ts new file mode 100644 index 0000000..35fd5c7 --- /dev/null +++ b/test/web/file/simple.spec.ts @@ -0,0 +1,26 @@ +import { test } from '@playwright/test' +import { checkLinkDoesNotExist, checkLinkForDownload, checkLinkForText, createNote, getFileChecksum } from '../../utils' +import Files from './files' + +test.describe('@web', () => { + test('simple pdf', async ({ page }) => { + const files = [Files.PDF] + const link = await createNote(page, { files }) + await checkLinkForText(page, link, 'AES.pdf') + await checkLinkDoesNotExist(page, link) + }) + + test('pdf content', async ({ page }) => { + const files = [Files.PDF] + const checksum = await getFileChecksum(files[0]) + const link = await createNote(page, { files }) + await checkLinkForDownload(page, link, 'AES.pdf', checksum) + }) + + test('image content', async ({ page }) => { + const files = [Files.Image] + const checksum = await getFileChecksum(files[0]) + const link = await createNote(page, { files }) + await checkLinkForDownload(page, link, 'image.jpg', checksum) + }) +}) diff --git a/test/web/file/too-big.spec.ts b/test/web/file/too-big.spec.ts new file mode 100644 index 0000000..c2f4794 --- /dev/null +++ b/test/web/file/too-big.spec.ts @@ -0,0 +1,10 @@ +import { test } from '@playwright/test' +import { createNote } from '../../utils' +import Files from './files' + +test.describe('@web', () => { + test.skip('to big zip', async ({ page }) => { + const files = [Files.Zip] + const link = await createNote(page, { files, error: 'note is to big' }) + }) +}) diff --git a/test/web/text/expiration.spec.ts b/test/web/text/expiration.spec.ts new file mode 100644 index 0000000..44a6b24 --- /dev/null +++ b/test/web/text/expiration.spec.ts @@ -0,0 +1,16 @@ +import { test } from '@playwright/test' +import { checkLinkDoesNotExist, checkLinkForText, createNote } from '../../utils' + +test.describe('@web', () => { + test('1 minute', async ({ page }) => { + const text = `Virtues value ascetic revaluation sea dead strong burying.` + const minutes = 1 + const timeout = minutes * 60_000 + test.setTimeout(timeout * 2) + const shareLink = await createNote(page, { text, expiration: minutes }) + await checkLinkForText(page, shareLink, text) + await checkLinkForText(page, shareLink, text) + await page.waitForTimeout(timeout) + await checkLinkDoesNotExist(page, shareLink) + }) +}) diff --git a/test/web/text/simple.spec.ts b/test/web/text/simple.spec.ts new file mode 100644 index 0000000..a26f5db --- /dev/null +++ b/test/web/text/simple.spec.ts @@ -0,0 +1,10 @@ +import { test } from '@playwright/test' +import { checkLinkForText, createNote } from '../../utils' + +test.describe('@web', () => { + test('simple', async ({ page }) => { + const text = `Endless prejudice endless play derive joy eternal-return selfish burying. Of decieve play pinnacle faith disgust. Spirit reason salvation burying strong of joy ascetic selfish against merciful sea truth. Ubermensch moral prejudice derive chaos mountains ubermensch justice philosophy justice ultimate joy ultimate transvaluation. Virtues convictions war ascetic eternal-return spirit. Ubermensch transvaluation noble revaluation sexuality intentions salvation endless decrepit hope noble fearful. Justice ideal ultimate snare god joy evil sexuality insofar gains oneself ideal.` + const shareLink = await createNote(page, { text }) + await checkLinkForText(page, shareLink, text) + }) +}) diff --git a/test/web/text/views.spec.ts b/test/web/text/views.spec.ts new file mode 100644 index 0000000..524fa5d --- /dev/null +++ b/test/web/text/views.spec.ts @@ -0,0 +1,20 @@ +import { test } from '@playwright/test' +import { checkLinkDoesNotExist, checkLinkForText, createNote } from '../../utils' + +test.describe('@web', () => { + test('only shown once', async ({ page }) => { + const text = `Christian victorious reason suicide dead. Right ultimate gains god hope truth burying selfish society joy. Ultimate.` + const shareLink = await createNote(page, { text }) + await checkLinkForText(page, shareLink, text) + await checkLinkDoesNotExist(page, shareLink) + }) + + test('view 3 times', async ({ page }) => { + const text = `Justice holiest overcome fearful strong ultimate holiest christianity.` + const shareLink = await createNote(page, { text, views: 3 }) + await checkLinkForText(page, shareLink, text) + await checkLinkForText(page, shareLink, text) + await checkLinkForText(page, shareLink, text) + await checkLinkDoesNotExist(page, shareLink) + }) +})