1import { expect, test } from "bun:test";2import { parseConfig } from "../src/server/config.ts";34const hash = "$argon2id$v=19$m=65536,t=2,p=1$c2FsdA$aGFzaA";5const s3 = `6domain = "kepr.example"7data_dir = "data"8admin_password_hash = "${hash}"910[bucket]11endpoint = "https://s3.fr-par.scw.cloud"12name = "koh"13region = "fr-par"14access_key_id = "SCWREAD"15secret_access_key = "secret"16`;1718test("parses the §19 keys with an S3 bucket", () => {19 expect(parseConfig(s3)).toEqual({20 domain: "kepr.example",21 dataDir: "data",22 adminPasswordHash: hash,23 bucket: {24 kind: "s3",25 endpoint: "https://s3.fr-par.scw.cloud",26 name: "koh",27 region: "fr-par",28 accessKeyId: "SCWREAD",29 secretAccessKey: "secret",30 },31 });32});3334test("parses a folder source", () => {35 const text = `domain = "d"\ndata_dir = "data"\nadmin_password_hash = "${hash}"\n[bucket]\nfolder = "/tmp/b"\n`;36 expect(parseConfig(text).bucket).toEqual({ kind: "folder", path: "/tmp/b" });37});3839test("fails loudly, naming the problem", () => {40 expect(() => parseConfig("domain = ")).toThrow("not valid TOML");41 expect(() => parseConfig(s3.replace('domain = "kepr.example"', ""))).toThrow(42 "domain is required",43 );44 expect(() => parseConfig(s3.replace(/\[bucket\][\s\S]*/, ""))).toThrow("[bucket] is missing");45 expect(() => parseConfig(s3.replace('region = "fr-par"', ""))).toThrow(46 "[bucket] region is required",47 );48 expect(() => parseConfig(s3.replace(hash, "plaintext"))).toThrow("Argon2id");49 expect(() => parseConfig(`${s3}folder = "/tmp/b"\n`)).toThrow("cannot be combined");50 expect(() => parseConfig(s3.replace("https://s3.fr-par.scw.cloud", "nope"))).toThrow(51 "must be a URL",52 );53});5455test("refuses a domain that is not a bare hostname, since install.sh embeds it as is", () => {56 for (const domain of ["kepr.example:8443", "localhost", "a-b.c1.example"]) {57 expect(parseConfig(s3.replace("kepr.example", domain)).domain).toBe(domain);58 }59 for (const domain of [60 "kepr.example/x",61 "https://kepr.example",62 "kepr.example$(id)",63 "kepr.example; rm x",64 "kepr.example\\nx",65 "-kepr.example",66 "kepr..example",67 "kepr.example:",68 "[email protected]",69 ]) {70 expect(() => parseConfig(s3.replace("kepr.example", domain))).toThrow("bare hostname");71 }72});