clone

nostr relays on demand at <name>.bind.ws: claim one with your key, pay in zaps past the free allowance

dependabot/github_actions/actions-9b61906d8b
bindws / test / object / git-capacity-policy.test.ts
git-capacity-policy.test.ts 3.9K
1import { SELF, env, runInDurableObject } from "cloudflare:test";2import { describe, it, expect } from "vitest";3import { generateSecretKey } from "nostr-tools/pure";4import type { Relay } from "../../src/relay.ts";5import { DEFAULT_GIT_LIMITS, GiB, MiB } from "../../src/git-limits.ts";6import { Settings } from "../../src/settings.ts";7import { rpc } from "../helpers/relay.ts";89describe("per-relay Git capacity", () => {10 it("preserves partial changes through storage and config export while advertising the effective limits", async () => {11 const host = "git-capacity.bind.ws", other = "git-capacity-other.bind.ws";12 const owner = generateSecretKey();13 await rpc(host, owner, "claim");14 await rpc(other, owner, "claim");15 await rpc(host, owner, "setpolicy", { features: { grasp: true }, git: { maxRepositories: 256, maxRelayBytes: 6 * GiB } });16 const changed = await rpc(host, owner, "setpolicy", { git: { maxObjectBytes: 64 * MiB } });17 expect(changed.status).toBe(200);18 expect(changed.result.git).toEqual({ ...DEFAULT_GIT_LIMITS, maxRepositories: 256, maxRelayBytes: 6 * GiB, maxObjectBytes: 64 * MiB });19 expect((await rpc(other, owner, "getpolicy")).result.git).toEqual(DEFAULT_GIT_LIMITS);20 const doc = await (await SELF.fetch(`https://${host}/`, { headers: { accept: "application/nostr+json" } })).json<Record<string, unknown>>();21 expect(doc.git_limits).toEqual(changed.result.git);22 expect(doc.repo_acceptance_criteria).toContain("256 repositories");23 expect(doc.repo_acceptance_criteria).toContain("64 MiB per new object");24 const exported = (await rpc(host, owner, "exportconfig")).result;25 expect((await rpc(other, owner, "importconfig", exported)).status).toBe(200);26 expect((await rpc(other, owner, "getpolicy")).result.git).toEqual(changed.result.git);27 await runInDurableObject(env.RELAY.getByName("git-capacity"), async (instance) => {28 const settings = new Settings((instance as Relay).sql);29 settings.load();30 expect(settings.policy.git).toEqual(changed.result.git);31 });32 });3334 it("refuses unauthorized and malformed settings without applying part of the patch", async () => {35 const host = "git-capacity-invalid.bind.ws", owner = generateSecretKey();36 await rpc(host, owner, "claim");37 expect((await rpc(host, generateSecretKey(), "setpolicy", { git: { maxRepositories: 512 } })).status).toBe(403);38 for (const git of [{ maxRepositories: -1 }, { maxPackBytes: 0 }, { maxRelayBytes: 10 * GiB }, { maxObjects: 1.5 }, { maxObjectBytes: "64" }, { maxRepositories: 256, typo: 1 }]) {39 expect((await rpc(host, owner, "setpolicy", { name: "should not change", git })).status).toBe(400);40 }41 const policy = (await rpc(host, owner, "getpolicy")).result;42 expect(policy.git).toEqual(DEFAULT_GIT_LIMITS);43 expect(policy.name).not.toBe("should not change");44 const plan = (await rpc(host, owner, "importconfig", { format: "bind.ws/relay-config/2", policy: { git: { maxObjects: 0 } } }, { dryRun: true })).result;45 expect(plan.warnings).toContain("policy.git: value not accepted");46 expect(plan.changes.policy).toEqual([]);47 });4849 it("loads older saved policies with the new defaults and keeps valid overrides", async () => {50 await runInDurableObject(env.RELAY.getByName("git-capacity-upgrade"), async (instance) => {51 const relay = instance as Relay;52 relay.sql.exec("INSERT INTO settings(key,value) VALUES('policy',?) ON CONFLICT(key) DO UPDATE SET value=excluded.value", JSON.stringify({ name: "Older relay" }));53 const settings = new Settings(relay.sql);54 settings.load();55 expect(settings.policy.git).toEqual(DEFAULT_GIT_LIMITS);56 expect(settings.policy.name).toBe("Older relay");57 settings.update({ git: { ...settings.policy.git, maxRepositories: 300 } });58 const reloaded = new Settings(relay.sql);59 reloaded.load();60 expect(reloaded.policy.git.maxRepositories).toBe(300);61 });62 });63});