Skip to content

Commit 63ebc29

Browse files
committed
chore(test): Use docker-compose for E2E testing and reduce file I/O
1 parent bd93fdc commit 63ebc29

20 files changed

Lines changed: 258 additions & 315 deletions
File renamed without changes.

.github/workflows/ci.yml

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,6 @@ jobs:
4646
test-e2e:
4747
name: E2E Test
4848
runs-on: ubuntu-latest
49-
env:
50-
DB_USER: mastodon
51-
DB_NAME: mastodon
52-
DB_PASS: password
53-
MASTODON_CONTAINER: mastodon
54-
55-
services:
56-
db:
57-
image: postgres
58-
ports:
59-
- 5432:5432
60-
env:
61-
POSTGRES_USER: ${{ env.DB_USER }}
62-
POSTGRES_DB: ${{ env.DB_NAME }}
63-
POSTGRES_PASSWORD: ${{ env.DB_PASS }}
64-
POSTGRES_HOST_AUTH_METHOD: trust
65-
options: >-
66-
--health-cmd "pg_isready -U postgres"
67-
redis:
68-
image: redis
69-
ports:
70-
- 6379:6379
71-
options: >-
72-
--health-cmd "redis-cli ping"
7349

7450
steps:
7551
- uses: actions/checkout@v3
@@ -83,33 +59,24 @@ jobs:
8359
cache: yarn
8460

8561
- name: Setup Mastodon
86-
run: >-
87-
docker run
88-
-i
89-
--rm
90-
--env-file ./.github/.env.test
91-
docker.io/neetshin/mastodon-dev:latest
92-
bash -c "RAILS_ENV=development bundle exec rails db:setup"
93-
94-
- name: Run Mastodon
95-
run: >-
96-
docker run
97-
-d
98-
-p 3000:3000
99-
-p 4000:4000
100-
--name ${{ env.MASTODON_CONTAINER }}
101-
-e DEEPL_PLAN=${{ secrets.DEEPL_PLAN }}
102-
-e DEEPL_API_KEY=${{ secrets.DEEPL_API_KEY }}
103-
--env-file ./.github/.env.test
104-
docker.io/neetshin/mastodon-dev:latest
105-
bash -c "foreman start"
62+
run: docker compose up -d
10663

10764
- name: Install dependencies
10865
run: yarn install --frozen-lockfile
10966

67+
- name: Wait for http://localhost:3000
68+
run: |
69+
for i in {1..10}; do
70+
curl -s -o /dev/null -w "%{http_code}" http://localhost:3000/api/v2/instance && break
71+
sleep 5
72+
done
73+
11074
- name: Run tests
11175
run: yarn run test:e2e --max-workers=2
11276

77+
- name: Teardown Mastodon
78+
run: docker compose down
79+
11380
- name: Codecov
11481
uses: codecov/codecov-action@v3
11582
with:

cspell.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
{
2-
"version": "0.1",
2+
"version": "0.2",
33
"language": "en,en-GB",
4-
"ignorePaths": [
5-
"**/node_modules/**",
6-
"**/dist/**"
7-
],
4+
"ignorePaths": ["**/node_modules/**", "**/dist/**"],
85
"words": [
96
"AGPL",
107
"asynckit",

docker-compose.yaml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
version: "3"
3+
services:
4+
db:
5+
restart: always
6+
image: postgres
7+
ports:
8+
- "5432:5432"
9+
environment:
10+
- POSTGRES_USER=mastodon
11+
- POSTGRES_PASSWORD=mastodon
12+
- POSTGRES_DB=mastodon
13+
- POSTGRES_HOST_AUTH_METHOD=trust
14+
healthcheck:
15+
test: ["CMD", "pg_isready", "-U", "postgres"]
16+
17+
redis:
18+
restart: always
19+
image: redis
20+
ports:
21+
- "6379:6379"
22+
healthcheck:
23+
test: ["CMD", "redis-cli", "ping"]
24+
25+
mastodon:
26+
restart: always
27+
image: neetshin/mastodon-dev:latest
28+
ports:
29+
- "3000:3000"
30+
- "4000:4000"
31+
depends_on:
32+
- db
33+
- redis
34+
healthcheck:
35+
test:
36+
[
37+
"CMD-SHELL",
38+
"wget -q --spider --proxy=off localhost:3000/health || exit 1",
39+
]
40+
env_file:
41+
- .env.test
42+
environment:
43+
- RAILS_ENV=development
44+
command: >
45+
/bin/bash -c "
46+
bundle exec rails db:setup &&
47+
foreman start
48+
"

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
"@size-limit/preset-small-lib": "^10.0.1",
4646
"@types/jest": "^29.5.6",
4747
"@types/node": "^20.8.9",
48-
"@types/proper-lockfile": "^4.1.3",
4948
"@types/ws": "^8.5.8",
5049
"@typescript-eslint/eslint-plugin": "^6.9.0",
5150
"@typescript-eslint/parser": "^6.9.0",
@@ -58,11 +57,11 @@
5857
"eslint-plugin-simple-import-sort": "^10.0.0",
5958
"eslint-plugin-unicorn": "^48.0.1",
6059
"get-port": "^5.1.1",
60+
"ioredis": "^5.3.2",
6161
"iterator-helpers-polyfill": "^2.3.3",
6262
"jest": "^29.6.4",
6363
"npm-run-all": "^4.1.5",
6464
"prettier": "^3.0.3",
65-
"proper-lockfile": "^4.1.2",
6665
"rollup": "^4.1.4",
6766
"rollup-plugin-auto-external": "^2.0.0",
6867
"rollup-plugin-dts": "^6.1.0",
@@ -73,7 +72,7 @@
7372
"tslib": "^2.6.2",
7473
"typedoc": "^0.25.2",
7574
"typescript": "^5.2.2",
76-
"undici": "^5.27.0"
75+
"undici": "^6.6.2"
7776
},
7877
"files": [
7978
"README.md",

src/adapters/action/dispatcher-ws.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class WebSocketActionDispatcher implements ActionDispatcher {
2626
}
2727

2828
if (action.type !== "subscribe") {
29-
throw new MastoUnexpectedError("Unknown action type");
29+
throw new MastoUnexpectedError(`Unknown action type ${action.type}`);
3030
}
3131

3232
const data = action.data ?? {};

src/mastodon/streaming/client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export interface Client {
5858
};
5959

6060
close(): void;
61+
6162
/** @internal */
6263
prepare(): Promise<void>;
6364
}

test-utils/jest-environment.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,34 @@ import { existsSync } from "node:fs";
33
import fs from "node:fs/promises";
44
import path from "node:path";
55

6+
import Redis from "ioredis";
67
import NodeEnvironment from "jest-environment-node";
78

89
import {
910
createOAuthAPIClient,
1011
createRestAPIClient,
1112
type mastodon,
1213
} from "../src";
13-
import { TokenPoolFsImpl } from "./pools";
14-
import { TokenFactoryDocker } from "./pools/token-factory-docker";
15-
import { TokenRepositoryFs } from "./pools/token-repository-fs";
14+
import { TokenFactoryDocker, TokenPoolRedis } from "./pools";
1615
import { createTootctl } from "./tootctl";
1716

1817
class CustomEnvironment extends NodeEnvironment {
18+
redis!: Redis;
19+
1920
override async setup(): Promise<void> {
2021
await super.setup();
21-
const misc = await this.createGlobals();
22-
this.global.__misc__ = misc;
23-
this.global.Symbol = Symbol;
22+
this.redis = new Redis();
23+
this.global.__misc__ = await this.createGlobals();
24+
25+
/* eslint-disable @typescript-eslint/no-explicit-any */
26+
(this.global.Symbol as any).dispose ??= Symbol("Symbol.dispose");
27+
(this.global.Symbol as any).asyncDispose ??= Symbol("Symbol.asyncDispose");
28+
/* eslint-enable @typescript-eslint/no-explicit-any */
29+
}
30+
31+
override async teardown(): Promise<void> {
32+
await super.teardown();
33+
this.redis.disconnect();
2434
}
2535

2636
private async createGlobals(): Promise<typeof globalThis.__misc__> {
@@ -33,15 +43,12 @@ class CustomEnvironment extends NodeEnvironment {
3343
}
3444

3545
const adminToken = await this.readAdminToken(baseCacheDir);
36-
const repository = new TokenRepositoryFs(
37-
path.join(baseCacheDir, "tokens.json"),
38-
);
3946
const container = process.env.MASTODON_CONTAINER ?? "mastodon";
40-
const tootctl = createTootctl({ container });
47+
const tootctl = createTootctl({ container, compose: true });
4148
const oauth = createOAuthAPIClient({ url });
4249
const app = await this.readApp(baseCacheDir);
4350
const factory = new TokenFactoryDocker(tootctl, oauth, app);
44-
const tokenPool = new TokenPoolFsImpl(repository, factory);
51+
const tokenPool = new TokenPoolRedis(this.redis, factory);
4552

4653
return {
4754
url,

test-utils/jest-global-setup.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,6 @@ const readOrCreateAdminToken = async (
5757
return token;
5858
};
5959

60-
const initTokens = async (baseCacheDir: string) => {
61-
const tokensFilePath = path.join(baseCacheDir, "tokens.json");
62-
63-
if (existsSync(tokensFilePath)) {
64-
return;
65-
}
66-
67-
await fs.writeFile(path.join(baseCacheDir, "tokens.json"), "[]", "utf8");
68-
};
69-
7060
export default async function main(): Promise<void> {
7161
const baseCacheDir = path.join(__dirname, "../node_modules/.cache/masto");
7262
if (!existsSync(baseCacheDir)) {
@@ -78,5 +68,4 @@ export default async function main(): Promise<void> {
7868

7969
const app = await readOrCreateApp(baseCacheDir, masto);
8070
await readOrCreateAdminToken(baseCacheDir, oauth, app);
81-
await initTokens(baseCacheDir);
8271
}

test-utils/pools/fs-exclusive-lock.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)