Something went wrong. Try again.
Monorepo for Tangled forked from tangled.org/core
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350import type { Did } from "@atcute/lexicons/syntax";import { expect, type Browser } from "@playwright/test";
import { accounts, knotRecordFor, pulls, repos, tickets, type RepoOptions } from "./api";import { config, isSeeded, type Account } from "./config";import { Tidy, type Chore } from "./chores";import { push } from "./git";import { selfUri, stamp, type Written } from "./net";import { note } from "./record";import { bobbin, lookBefore, refsNaming, settle, type Hop } from "./services";import { Tabs, type Held } from "./tabs";import { declarationId, manifestId, refsId, repoAt, unique, type Ctx, type Repo, type Slot} from "./vocabulary";
class People { private labelled = new Map<string, Account>(); private members = new Set<string>(); private throughThePage = new Set<string>(); private met = new Map<string, Account>(); who?: Account;
constructor(private tidy: Tidy) {}
get actor() { if (!this.who) throw new Error("this step needs an .actor() before it"); return this.who; }
async switchTo(who: string | Account, options: { login?: "ui" } = {}) { if (typeof who !== "string") { this.met.set(who.did, who); this.who = who; return; } let account = this.labelled.get(who); if (!account) { const made = await accounts.create(who); this.labelled.set(who, made); this.tidy.add(`account ${made.handle}`, { kind: "account", account: made }); account = made; } if (options.login === "ui") this.throughThePage.add(account.name); this.met.set(account.did, account); this.who = account; }
get everyone() { return [...this.met.values()]; }
signsInThroughThePage(account: Account) { return config.login === "ui" || this.throughThePage.has(account.name); }
// the knot only lets its members make repos async ensureMember(account: Account) { if (isSeeded(account) || this.members.has(account.did)) return; await accounts.makeMember(account); this.oweMembership(account); }
// for a membership made outside ensureMember, like an invite redeemed in the browser oweMembership(account: Account) { this.members.add(account.did); this.tidy.add(`knot membership of ${account.handle}`, { kind: "member", account }); }}
export class Run implements Ctx { readonly tidy = new Tidy(); private people = new People(this.tidy); private tabs: Tabs; private home?: Repo; private parent?: Repo; private last?: Ctx["pushed"]; private slots = new Map<Slot, Written>(); private window = "";
constructor(browser: Browser) { this.tabs = new Tabs(browser); }
get actor() { return this.people.actor; }
// read before the test runs, so this getter must not throw get known() { return { actor: this.people.who?.name, repo: this.home?.name }; }
get pageUrl() { try { return this.page.url(); } catch { return undefined; } }
get page() { return this.tabs.page(this.actor, this.window); }
async openWindow(window: string) { await this.tabs.openWindow(this.actor, window); this.window = window; }
async openDevice(window: string) { await this.tabs.openDevice(this.actor, window); this.window = window; }
get repo() { if (!this.home) throw new Error("this step needs a .repo() before it"); return this.home; }
set repo(repo: Repo) { this.home = repo; note("repo", { name: repo.name, did: repo.did }); }
get upstream() { if (!this.parent) throw new Error("this step needs a .fork() before it"); return this.parent; }
set upstream(repo: Repo) { this.parent = repo; }
get everyone() { return this.people.everyone; }
get pushed() { if (!this.last) throw new Error("this step needs a .push() before it"); return this.last; }
set pushed(pushed: Ctx["pushed"]) { this.last = pushed; }
switchTo(who: string | Account, options: { login?: "ui" } = {}) { this.window = ""; return this.people.switchTo(who, options); }
async own(options: RepoOptions = {}) { const owner = this.actor; await this.people.ensureMember(owner); const made = await repos.create(owner, `e2e-${stamp()}`, options); // registered before the wait, since a stalled hydrant fails the run right there this.tidy.add(`repo ${made.name}`, { kind: "repo", owner, made }); // the web only shows a repo after bobbin has indexed it await bobbin.indexed(made.declaration); await bobbin.hasRepo(owner.did, made.name); this.set(declarationId, made.declaration); await this.learn(manifestId, selfUri(made.did, manifestId.nsid), "read"); this.repo = repoAt(owner, { did: made.did, name: made.name, defaultBranch: "main" }); }
becomeMember() { return this.people.ensureMember(this.actor); }
oweMembership() { this.people.oweMembership(this.actor); }
adoptRepo(made: { did: Did; name: string }) { const owner = this.actor; this.tidy.add(`repo ${made.name}`, { kind: "repo", owner, made }); this.repo = repoAt(owner, { ...made, defaultBranch: "main" }); }
owe(label: string, chore: Chore) { this.tidy.add(label, chore); }
expectNoWrite(slot: Slot) { return this.tabs.expectNoWrite(this.actor, slot); }
get = (slot: Slot) => { const written = this.slots.get(slot); if (!written) throw new Error(`nothing has filled the ${slot.nsid} slot yet`); return written; };
set(slot: Slot, written: Written) { this.slots.set(slot, written); note(slot.nsid, written); }
// nothing else erases this record, so the run has to adopt(slot: Slot, written: Written) { this.set(slot, written); this.tidy.record(slot, this.actor, written); }
async learn(slot: Slot, uri: string, doing: string) { this.set(slot, { uri, cid: (await knotRecordFor(uri, doing)).cid }); }
settle() { return this.tabs.settle(this.actor); }
openTab() { return this.tabs.open(this.actor, this.people.signsInThroughThePage(this.actor)); }
hold(slot: Slot, matching?: (body: any) => boolean) { return this.tabs.hold(this.actor, slot, matching); }
// a write let through here is never recorded, so the slot keeps what it had answer(slot: Slot, held: Held, status: number | number[]) { return this.tabs.release(this.actor, slot, held, status); }
async release(slot: Slot, held: Held) { const body = await this.tabs.release(this.actor, slot, held); if (held.method === "delete") return this.get(slot); const before = this.slots.get(slot); const written = { uri: body.uri as string, cid: body.cid as string }; if (held.method === "put" && before?.cid === written.cid) { throw new Error(`the put of ${slot.nsid} answered with the cid it started from`); } this.set(slot, written); // a put with nothing to replace is a create under a client rkey const made = held.method === "create" || (held.method === "put" && !before); if (made || this.tidy.slotHasEraser(slot)) this.tidy.record(slot, this.actor, written); return written; }
expectQuiet() { return this.tabs.expectQuiet(); }
// tabs close first so nothing writes while the world is torn down async close() { await this.tabs.close(); await this.tidy.close(); }}
export const pushFiles = async ( run: Run, files: Record<string, string>, options: { branch?: string; base?: string; options?: string[] }) => { const { actor, repo } = run; const branch = options.branch ?? repo.defaultBranch; const { sha, output } = await push(actor, repo.did, files, { branch, base: options.base, options: options.options }); run.pushed = { sha, branch, output }; repo.heads[branch] = sha; const uri = selfUri(repo.did, refsId.nsid); const stored = await refsNaming( uri, `the knot should list ${sha} on ${branch} in ${repo.did}`, (named) => named.some(([commit, ref]) => commit === sha && ref === `refs/heads/${branch}`) ); run.set(refsId, { uri, cid: stored.cid });};
// bobbin's pages read git, so wait for every pushed branch to show up thereexport const waitForMirror = async (run: Run) => { await bobbin.knowsRepo(run.repo.did); await Promise.all( Object.entries(run.repo.heads).map(([branch, sha]) => bobbin.hasBranch(run.repo.did, branch, sha) ) );};
export const openPull = async ( run: Run, pull: Slot, patch: Slot, options: { title?: string; from: string; into: string }) => { const { from, into } = options; const range = { base: run.repo.heads[into], head: run.repo.heads[from] }; if (!range.base || !range.head) throw new Error("a pull needs a .push() to both branches"); const opened = await pulls.open( run.actor, run.repo.did, options.title ?? unique("a pull"), into, range, from ); // the patch goes first at teardown, since it names a ticket that has to outlive it run.adopt(pull, opened.ticket); run.adopt(patch, opened.patch); await bobbin.indexed(opened.ticket); await bobbin.indexed(opened.patch);};
// made through the api, so the flow only clicks through what it testsexport const openTicket = async ( run: Run, slot: Slot, options: { title?: string; state?: "open" | "closed" }) => { const opened = await tickets.open(run.actor, run.repo.did, options.title ?? unique("a ticket")); run.adopt(slot, opened); if (options.state === "closed") { run.set(slot, await tickets.setState(run.actor, opened, "closed")); } await bobbin.indexed(run.get(slot));};
// the did sits in the held declaration, so register the repo before letting the write throughexport const traceNewRepo = async (run: Run, name: string, hops: Hop[]) => { const owner = run.actor; const held = await run.hold(declarationId); expect(held.host, "the declaration should go to the pds").toBe(new URL(config.pdsUrl).host); expect(held.method, "the declaration should be a create").toBe("create"); expect(held.body).toMatchObject({ repo: owner.did, collection: declarationId.nsid, rkey: name, record: { $type: declarationId.nsid } }); const did: Did = held.body.record.repo; expect(did, "the declaration should name the repo the knot minted").toMatch(/^did:/); run.adoptRepo({ did, name }); const before = await lookBefore(hops, held); const written = await run.release(declarationId, held); await settle(written, hops, owner, declarationId.nsid, "create", before); await run.learn(manifestId, selfUri(did, manifestId.nsid), "read");};