Skip to content

Commit 75d9505

Browse files
authored
Merge pull request #10 from appwrite/tests
2 parents 6a8c374 + 785ffa5 commit 75d9505

File tree

15 files changed

+1386
-46
lines changed

15 files changed

+1386
-46
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ LICENSE
1515
# Test artifacts
1616
test-screenshots/
1717
comparison/
18+
.lighthouse/
1819

1920
# Build output
2021
dist/

.github/workflows/check.yml

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,23 @@ name: "Check"
33
on:
44
pull_request:
55
push:
6-
branches: [main]
6+
branches: [ main ]
77

88
jobs:
99
check:
1010
name: "Check"
1111
runs-on: ubuntu-latest
1212
steps:
1313
- name: Checkout code
14-
uses: actions/checkout@v2
14+
uses: actions/checkout@v4
1515

16-
- name: Set up Node.js
17-
uses: actions/setup-node@v2
16+
- name: Setup Bun
17+
uses: oven-sh/setup-bun@v1
1818
with:
19-
node-version: '18'
20-
21-
- name: Install PNPM
22-
run: npm i -g pnpm
19+
bun-version: 'latest'
2320

2421
- name: Install dependencies
25-
run: pnpm install
22+
run: bun install --frozen-lockfile
2623

27-
- name: Format
28-
run: pnpm run check
24+
- name: ESLint check
25+
run: bun run check

.github/workflows/lint.yml

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,23 @@ name: "Lint"
33
on:
44
pull_request:
55
push:
6-
branches: [main]
6+
branches: [ main ]
77

88
jobs:
99
lint:
1010
name: "Lint"
1111
runs-on: ubuntu-latest
1212
steps:
1313
- name: Checkout code
14-
uses: actions/checkout@v2
14+
uses: actions/checkout@v4
1515

16-
- name: Set up Node.js
17-
uses: actions/setup-node@v2
16+
- name: Setup Bun
17+
uses: oven-sh/setup-bun@v1
1818
with:
19-
node-version: '18'
20-
21-
- name: Install PNPM
22-
run: npm i -g pnpm
19+
bun-version: 'latest'
2320

2421
- name: Install dependencies
25-
run: pnpm install
22+
run: bun install --frozen-lockfile
2623

27-
- name: Format
28-
run: pnpm run lint
24+
- name: Biome lint
25+
run: bun run lint

.github/workflows/test.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: "Test"
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [ main ]
7+
8+
jobs:
9+
tests:
10+
name: "Unit and E2E Tests"
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v4
15+
16+
- name: Setup Bun
17+
uses: oven-sh/setup-bun@v1
18+
with:
19+
bun-version: 'latest'
20+
21+
- name: Install dependencies
22+
run: bun install --frozen-lockfile
23+
24+
- name: Run unit tests
25+
run: bun test:unit
26+
27+
- name: Start container
28+
run: docker compose up -d
29+
30+
- name: Wait for container to be ready
31+
run: |
32+
for i in {1..30}; do
33+
if curl -f http://localhost:3000/v1/health > /dev/null 2>&1; then
34+
echo "Container is ready!"
35+
exit 0
36+
fi
37+
echo "Waiting for container... ($i/30)"
38+
sleep 1
39+
done
40+
echo "Container failed to start"
41+
echo "Container logs:"
42+
docker compose logs
43+
exit 1
44+
45+
- name: Run e2e tests
46+
run: bun test:e2e
47+
48+
- name: Print logs
49+
if: failure()
50+
run: docker compose logs

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,5 @@ dist
144144
# Test artifacts
145145
test-screenshots/
146146
comparison/
147+
.lighthouse/
148+
lighthouse/

Dockerfile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ RUN apk upgrade --no-cache --available && \
1717
tini && \
1818
apk add --no-cache font-wqy-zenhei --repository=http://dl-cdn.alpinelinux.org/alpine/edge/community && \
1919
# remove unnecessary chromium files to save space
20-
rm -rf /usr/lib/chromium/chrome_crashpad_handler \
21-
/usr/lib/chromium/chrome_200_percent.pak \
20+
rm -rf /usr/lib/chromium/chrome_200_percent.pak \
2221
/usr/lib/chromium/chrome_100_percent.pak \
2322
/usr/lib/chromium/xdg-mime \
2423
/usr/lib/chromium/xdg-settings \
@@ -31,11 +30,14 @@ ENV PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 \
3130
NODE_ENV=production
3231

3332
WORKDIR /app
34-
USER chrome
3533

3634
COPY package.json ./
3735
COPY --from=base /app/node_modules ./node_modules
3836
COPY src/ ./src/
3937

38+
RUN chown -R chrome:chrome /app
39+
40+
USER chrome
41+
4042
ENTRYPOINT ["tini", "--"]
4143
CMD ["bun", "run", "src/server.ts"]

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@
99
"dev": "bun --watch src/server.ts",
1010
"build": "tsc",
1111
"type-check": "tsc --noEmit",
12-
"format": "biome check --write ./src",
13-
"lint": "biome check ./src",
14-
"check": "eslint ./src",
15-
"test": "echo \"Error: no test specified\" && exit 1"
12+
"format": "biome check --write ./src ./tests",
13+
"lint": "biome check ./src ./tests",
14+
"check": "eslint ./src ./tests",
15+
"test": "bun test",
16+
"test:e2e": "bun test tests/e2e",
17+
"test:unit": "bun test tests/unit"
1618
},
1719
"keywords": [],
1820
"author": "",
1921
"license": "ISC",
20-
"packageManager": "pnpm@10.20.0",
22+
"packageManager": "bun@1.3.2",
2123
"dependencies": {
2224
"lighthouse": "^12.2.1",
2325
"playwright-core": "^1.52.0",

src/config/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export * from "./browser.js";
22
export * from "./lighthouse.js";
33

4-
export const port = Number(process.env.PORT) || 3000;
4+
export const port = process.env.PORT ? Number(process.env.PORT) : 3000;

src/routes/reports.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import type { BrowserContextOptions } from "playwright-core";
1+
import type { BrowserContext, BrowserContextOptions } from "playwright-core";
22
import { playAudit } from "playwright-lighthouse";
33
import { browser, defaultContext, lighthouseConfigs } from "../config";
44
import { lighthouseSchema } from "../schemas";
55

66
export async function handleReportsRequest(req: Request): Promise<Response> {
7+
let context: BrowserContext | undefined;
8+
79
try {
810
const json = await req.json();
911
const body = lighthouseSchema.parse(json);
@@ -19,7 +21,7 @@ export async function handleReportsRequest(req: Request): Promise<Response> {
1921
if (body.locale) contextOptions.locale = body.locale;
2022
if (body.timezoneId) contextOptions.timezoneId = body.timezoneId;
2123

22-
const context = await browser.newContext(contextOptions);
24+
context = await browser.newContext(contextOptions);
2325

2426
// Grant permissions if specified
2527
if (body.permissions && body.permissions.length > 0) {
@@ -72,7 +74,6 @@ export async function handleReportsRequest(req: Request): Promise<Response> {
7274
thresholds,
7375
});
7476

75-
await context.close();
7677
const report = Array.isArray(results.report)
7778
? results.report.join("")
7879
: results.report;
@@ -85,5 +86,7 @@ export async function handleReportsRequest(req: Request): Promise<Response> {
8586
status: 400,
8687
headers: { "Content-Type": "application/json" },
8788
});
89+
} finally {
90+
await context?.close();
8891
}
8992
}

src/routes/screenshots.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type {
2+
BrowserContext,
23
BrowserContextOptions,
34
PageScreenshotOptions,
45
} from "playwright-core";
@@ -8,6 +9,8 @@ import { screenshotSchema } from "../schemas";
89
export async function handleScreenshotsRequest(
910
req: Request,
1011
): Promise<Response> {
12+
let context: BrowserContext | undefined;
13+
1114
try {
1215
const json = await req.json();
1316
const body = screenshotSchema.parse(json);
@@ -28,7 +31,7 @@ export async function handleScreenshotsRequest(
2831
if (body.timezoneId) contextOptions.timezoneId = body.timezoneId;
2932
if (body.geolocation) contextOptions.geolocation = body.geolocation;
3033

31-
const context = await browser.newContext(contextOptions);
34+
context = await browser.newContext(contextOptions);
3235

3336
// Grant permissions if specified
3437
if (body.permissions && body.permissions.length > 0) {
@@ -78,8 +81,6 @@ export async function handleScreenshotsRequest(
7881

7982
const screen = await page.screenshot(screenshotOptions);
8083

81-
await context.close();
82-
8384
return new Response(Buffer.from(screen), {
8485
headers: {
8586
"Content-Type": `image/${body.format}`,
@@ -91,5 +92,7 @@ export async function handleScreenshotsRequest(
9192
status: 400,
9293
headers: { "Content-Type": "application/json" },
9394
});
95+
} finally {
96+
await context?.close();
9497
}
9598
}

0 commit comments

Comments
 (0)