|
|
@@ -0,0 +1,259 @@ |
| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| 7 |
import { mkdtemp, rm } from "node:fs/promises"; |
| 8 |
import { tmpdir } from "node:os"; |
| 9 |
import { join } from "node:path"; |
| 10 |
import { createApp, type App } from "../src/app.ts"; |
| 11 |
import { openIssue } from "../src/actions/issues.ts"; |
| 12 |
import { openPull } from "../src/actions/pulls.ts"; |
| 13 |
import { commitToBranch, createRepo } from "../src/actions/repos.ts"; |
| 14 |
import type { Repo } from "../src/core/repos.ts"; |
| 15 |
import { createApp as createAppRecord } from "../src/core/apps.ts"; |
| 16 |
import { createRun, type Run } from "../src/core/runs.ts"; |
| 17 |
import type { EnvStatus } from "../src/apps/runtime.ts"; |
| 18 |
import { appLine, runLine } from "../src/web/unfurl.ts"; |
| 19 |
import { createUser, type User } from "../src/core/users.ts"; |
| 20 |
import { serve, type Servers } from "../src/http/server.ts"; |
| 21 |
import { quietLogs } from "./helpers/globals.ts"; |
| 22 |
|
| 23 |
quietLogs(); |
| 24 |
|
| 25 |
const PASSWORD = "an-unfurl-passphrase"; |
| 26 |
const SECRET = "the-private-body-line"; |
| 27 |
|
| 28 |
const ok = <T>( |
| 29 |
r: { ok: true; value: T } | { ok: false; error: unknown }, |
| 30 |
): T => { |
| 31 |
if (!r.ok) throw new Error(JSON.stringify(r.error)); |
| 32 |
return r.value; |
| 33 |
}; |
| 34 |
|
| 35 |
const titles = (page: string): string => |
| 36 |
page.match(/<title>([^<]*)<\/title>/)?.[1]?.replace(/ · .*$/, "") ?? ""; |
| 37 |
|
| 38 |
const og = (page: string, property: string): string => |
| 39 |
new RegExp(`<meta (?:property|name)="${property}" content="([^"]*)"`).exec( |
| 40 |
page, |
| 41 |
)?.[1] ?? ""; |
| 42 |
|
| 43 |
describe("link descriptions", () => { |
| 44 |
let dataDir = ""; |
| 45 |
let app: App; |
| 46 |
let servers: Servers; |
| 47 |
let alice: User; |
| 48 |
let cookie = ""; |
| 49 |
let open: Repo; |
| 50 |
let closed: Repo; |
| 51 |
let openRun: Run; |
| 52 |
let closedRun: Run; |
| 53 |
|
| 54 |
const get = async (path: string, signedIn = true): Promise<string> => |
| 55 |
await ( |
| 56 |
await fetch(`${servers.url}${path}`, { |
| 57 |
headers: signedIn ? { cookie } : {}, |
| 58 |
}) |
| 59 |
).text(); |
| 60 |
|
| 61 |
beforeAll(async () => { |
| 62 |
dataDir = await mkdtemp(join(tmpdir(), "forge-unfurl-")); |
| 63 |
app = createApp({ dataDir, port: 0 }); |
| 64 |
servers = await serve(app); |
| 65 |
alice = ok( |
| 66 |
await createUser(app.db, { |
| 67 |
username: "alice", |
| 68 |
email: "[email protected]", |
| 69 |
password: PASSWORD, |
| 70 |
adminIfFirst: true, |
| 71 |
trusted: true, |
| 72 |
}), |
| 73 |
); |
| 74 |
const res = await fetch(`${servers.url}/signin`, { |
| 75 |
method: "POST", |
| 76 |
body: new URLSearchParams({ username: "alice", password: PASSWORD }), |
| 77 |
redirect: "manual", |
| 78 |
}); |
| 79 |
cookie = (res.headers.get("set-cookie") ?? "").split(";")[0]!; |
| 80 |
open = ok(await createRepo(app, alice, { name: "open", visibility: "public" })); |
| 81 |
closed = ok( |
| 82 |
await createRepo(app, alice, { name: "closed", visibility: "private" }), |
| 83 |
); |
| 84 |
for (const repo of [open, closed]) { |
| 85 |
ok( |
| 86 |
await commitToBranch(app, alice, repo, { |
| 87 |
message: "Start", |
| 88 |
files: [{ path: "README.md", content: "# hi\n" }], |
| 89 |
}), |
| 90 |
); |
| 91 |
} |
| 92 |
ok( |
| 93 |
openIssue(app, alice, open, { |
| 94 |
title: "Public bug", |
| 95 |
body: "Crashes on start.\nSecond line.", |
| 96 |
}), |
| 97 |
); |
| 98 |
ok(openIssue(app, alice, closed, { title: "Hidden bug", body: SECRET })); |
| 99 |
ok( |
| 100 |
openIssue(app, alice, open, { |
| 101 |
title: "Sealed bug", |
| 102 |
body: SECRET, |
| 103 |
sealed: true, |
| 104 |
}), |
| 105 |
); |
| 106 |
const mk = (repo: Repo): Run => |
| 107 |
createRun(app.db, { |
| 108 |
repoId: repo.id, |
| 109 |
kind: "deploy", |
| 110 |
env: "production", |
| 111 |
commitOid: "abcdef1234567890", |
| 112 |
summary: SECRET, |
| 113 |
}); |
| 114 |
for (const repo of [open, closed]) |
| 115 |
ok( |
| 116 |
createAppRecord(app.db, { |
| 117 |
repoId: repo.id, |
| 118 |
slug: `${repo.name}-app`, |
| 119 |
access: "public", |
| 120 |
}), |
| 121 |
); |
| 122 |
openRun = mk(open); |
| 123 |
closedRun = mk(closed); |
| 124 |
ok( |
| 125 |
await commitToBranch(app, alice, closed, { |
| 126 |
branch: "feat", |
| 127 |
base: "main", |
| 128 |
message: "Feat", |
| 129 |
files: [{ path: "a.txt", content: "a\n" }], |
| 130 |
}), |
| 131 |
); |
| 132 |
ok( |
| 133 |
await openPull(app, alice, closed, { |
| 134 |
title: "Hidden pull", |
| 135 |
body: SECRET, |
| 136 |
baseBranch: "main", |
| 137 |
headBranch: "feat", |
| 138 |
}), |
| 139 |
); |
| 140 |
}); |
| 141 |
|
| 142 |
afterAll(async () => { |
| 143 |
servers.stop(); |
| 144 |
app.close(); |
| 145 |
await rm(dataDir, { recursive: true, force: true }); |
| 146 |
}); |
| 147 |
|
| 148 |
test("a private pull unfurls as private, never its body", async () => { |
| 149 |
const page = await get("/alice/closed/pulls/2"); |
| 150 |
expect(titles(page)).toBe("closed"); |
| 151 |
expect(page).not.toMatch(/<title>[^<]*Hidden pull/); |
| 152 |
expect(og(page, "og:title")).not.toContain("Hidden pull"); |
| 153 |
expect(page).not.toContain(`content="${SECRET}`); |
| 154 |
expect(og(page, "og:description")).toBe("Private."); |
| 155 |
expect(og(page, "og:description")).not.toContain(SECRET); |
| 156 |
}); |
| 157 |
|
| 158 |
test("a private repository's issue unfurls as private, never its body", async () => { |
| 159 |
const page = await get("/alice/closed/issues/1"); |
| 160 |
expect(og(page, "og:description")).toBe("Private."); |
| 161 |
expect(og(page, "og:title")).toBe("closed"); |
| 162 |
expect(page).not.toMatch(new RegExp(`<meta[^>]*${SECRET}`)); |
| 163 |
}); |
| 164 |
|
| 165 |
test("an anonymous reader of a private repository gets neither", async () => { |
| 166 |
const page = await get("/alice/closed/issues/1", false); |
| 167 |
expect(page).not.toContain(SECRET); |
| 168 |
}); |
| 169 |
|
| 170 |
test("a public issue unfurls as its state and first line", async () => { |
|
|
|
| 171 |
const page = await get("/alice/open/issues/1", false); |
| 172 |
expect(og(page, "og:title")).toContain("Public bug"); |
| 173 |
expect(og(page, "og:description")).toBe("Open · Crashes on start."); |
| 174 |
expect(og(page, "description")).toBe("Open · Crashes on start."); |
| 175 |
}); |
| 176 |
|
| 177 |
test("a sealed issue in a public repository unfurls as private, for its owner too", async () => { |
| 178 |
const page = await get("/alice/open/issues/2"); |
| 179 |
expect(titles(page)).toBe("open"); |
| 180 |
expect(page).not.toMatch(/<title>[^<]*Sealed bug/); |
| 181 |
expect(og(page, "og:title")).not.toContain("Sealed bug"); |
| 182 |
expect(og(page, "og:description")).toBe("Private."); |
| 183 |
expect(page).not.toMatch(new RegExp(`<meta[^>]*${SECRET}`)); |
| 184 |
}); |
| 185 |
|
| 186 |
test("a private repository's run page unfurls as private", async () => { |
| 187 |
const page = await get(`/alice/closed/runs/${closedRun.id}`); |
| 188 |
expect(og(page, "og:description")).toBe("Private."); |
| 189 |
expect(page).not.toMatch(new RegExp(`<meta[^>]*${SECRET}`)); |
| 190 |
}); |
| 191 |
|
| 192 |
test("a public run page unfurls as its verdict, summary and commit", async () => { |
| 193 |
const page = await get(`/alice/open/runs/${openRun.id}`, false); |
| 194 |
expect(og(page, "og:description")).toBe( |
| 195 |
`Queued · ${SECRET} (commit abcdef1)`, |
| 196 |
); |
| 197 |
}); |
| 198 |
|
| 199 |
test("a private repository's app page unfurls as private", async () => { |
| 200 |
const page = await get("/alice/closed"); |
| 201 |
expect(og(page, "og:description")).toBe("Private."); |
| 202 |
}); |
| 203 |
|
| 204 |
test("a public app page unfurls as its name and state", async () => { |
| 205 |
const page = await get("/alice/open", false); |
| 206 |
expect(og(page, "og:description")).toBe("alice/open app · Not deployed yet"); |
| 207 |
}); |
| 208 |
|
| 209 |
test("a person's page counts their public repositories only", async () => { |
| 210 |
const page = await get("/alice", false); |
| 211 |
expect(og(page, "og:description")).toBe("alice · 1 public repository"); |
| 212 |
}); |
| 213 |
}); |
| 214 |
|
| 215 |
describe("run and app lines", () => { |
| 216 |
const repo = (visibility: string) => |
| 217 |
({ visibility, ownerName: "alice", name: "r" }) as unknown as Repo; |
| 218 |
const run = (state: string, summary = "") => |
| 219 |
({ state, summary, commitOid: "0123456789" }) as unknown as Run; |
| 220 |
const env = (state: string, dormant?: object) => |
| 221 |
({ env: "production", state, dormant }) as unknown as EnvStatus; |
| 222 |
|
| 223 |
test("a run's verdict follows its state", () => { |
| 224 |
const verdicts = { |
| 225 |
queued: "Queued", |
| 226 |
running: "Running", |
| 227 |
ok: "Passed", |
| 228 |
failed: "Failed", |
| 229 |
cancelled: "Cancelled", |
| 230 |
}; |
| 231 |
for (const [state, word] of Object.entries(verdicts)) |
| 232 |
expect(runLine(repo("public"), run(state))).toBe(`${word} (commit 0123456)`); |
| 233 |
expect(runLine(repo("public"), run("ok", "\n All green\nmore"))).toBe( |
| 234 |
"Passed · All green (commit 0123456)", |
| 235 |
); |
| 236 |
}); |
| 237 |
|
| 238 |
test("a run in a private or internal repository says only Private.", () => { |
| 239 |
for (const v of ["private", "internal"]) |
| 240 |
expect(runLine(repo(v), run("ok", "secret"))).toBe("Private."); |
| 241 |
}); |
| 242 |
|
| 243 |
test("an app is Live, Asleep, or in the state it is in", () => { |
| 244 |
const line = (e?: EnvStatus) => appLine(repo("public"), e); |
| 245 |
expect(line(env("running"))).toBe("alice/r app · Live"); |
| 246 |
expect(line(env("asleep"))).toBe("alice/r app · Asleep"); |
| 247 |
expect(line(env("stopped", { since: "x", idleSince: "y" }))).toBe( |
| 248 |
"alice/r app · Asleep", |
| 249 |
); |
| 250 |
expect(line(env("deploying"))).toBe("alice/r app · Deploying"); |
| 251 |
expect(line(env("failed"))).toBe("alice/r app · Failing"); |
| 252 |
expect(line(env("stopped"))).toBe("alice/r app · Stopped"); |
| 253 |
expect(line(undefined)).toBe("alice/r app · Not deployed yet"); |
| 254 |
}); |
| 255 |
|
| 256 |
test("a private app says only Private.", () => { |
| 257 |
expect(appLine(repo("private"), env("running"))).toBe("Private."); |
| 258 |
}); |
| 259 |
}); |
titleon the line above still carriespull.titlefor a private repo or sealed thread. It reaches og:title. Derive the title from the repo name in those cases.