-
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathvitest-environment.ts
More file actions
87 lines (72 loc) · 2.12 KB
/
Copy pathvitest-environment.ts
File metadata and controls
87 lines (72 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { existsSync } from "node:fs";
import fs from "node:fs/promises";
import path from "node:path";
import { Redis } from "ioredis";
import type { Environment } from "vitest/environments";
import {
createOAuthAPIClient,
createRestAPIClient,
type mastodon,
} from "../src/index.js";
import {
createTootctl,
TokenFactoryDocker,
TokenPoolRedis,
} from "./services/index.js";
let redis!: Redis;
export default <Environment>{
name: "mastodon",
transformMode: "web",
async setup() {
redis = new Redis();
globalThis.__misc__ = await createGlobals();
return {
teardown() {
redis.disconnect();
},
};
},
};
async function createGlobals(): Promise<typeof globalThis.__misc__> {
const url = "http://localhost:3000";
const instance = await createRestAPIClient({ url }).v1.instance.fetch();
const baseCacheDir = path.join(
import.meta.dirname,
"../node_modules/.cache/masto",
);
if (!existsSync(baseCacheDir)) {
throw new Error("Cache directory does not exist");
}
const adminToken = await readAdminToken(baseCacheDir);
const container = process.env.MASTODON_CONTAINER ?? "mastodon";
const tootctl = createTootctl({ container, compose: true });
const oauth = createOAuthAPIClient({ url });
const app = await readApp(baseCacheDir);
const factory = new TokenFactoryDocker(tootctl, oauth, app);
const tokenPool = new TokenPoolRedis(redis, factory);
return {
url,
app,
instance,
tokens: tokenPool,
adminToken,
};
}
async function readApp(baseCacheDir: string): Promise<mastodon.v1.Client> {
const appFilePath = path.join(baseCacheDir, "app.json");
if (!existsSync(appFilePath)) {
throw new Error("App file does not exist");
}
const json = await fs.readFile(appFilePath, "utf8");
return JSON.parse(json);
}
async function readAdminToken(
baseCacheDir: string,
): Promise<mastodon.v1.Token> {
const tokenFilePath = path.join(baseCacheDir, "admin_token.json");
if (!existsSync(tokenFilePath)) {
throw new Error("Admin token does not exist");
}
const json = await fs.readFile(tokenFilePath, "utf8");
return JSON.parse(json);
}