import { expect, test } from "bun:test"; import { parseConfig } from "../src/server/config.ts"; const hash = "$argon2id$v=19$m=65536,t=2,p=1$c2FsdA$aGFzaA"; const s3 = ` domain = "kepr.example" data_dir = "data" admin_password_hash = "${hash}" [bucket] endpoint = "https://s3.fr-par.scw.cloud" name = "koh" region = "fr-par" access_key_id = "SCWREAD" secret_access_key = "secret" `; test("parses the ยง19 keys with an S3 bucket", () => { expect(parseConfig(s3)).toEqual({ domain: "kepr.example", dataDir: "data", adminPasswordHash: hash, bucket: { kind: "s3", endpoint: "https://s3.fr-par.scw.cloud", name: "koh", region: "fr-par", accessKeyId: "SCWREAD", secretAccessKey: "secret", }, }); }); test("parses a folder source", () => { const text = `domain = "d"\ndata_dir = "data"\nadmin_password_hash = "${hash}"\n[bucket]\nfolder = "/tmp/b"\n`; expect(parseConfig(text).bucket).toEqual({ kind: "folder", path: "/tmp/b" }); }); test("fails loudly, naming the problem", () => { expect(() => parseConfig("domain = ")).toThrow("not valid TOML"); expect(() => parseConfig(s3.replace('domain = "kepr.example"', ""))).toThrow( "domain is required", ); expect(() => parseConfig(s3.replace(/\[bucket\][\s\S]*/, ""))).toThrow("[bucket] is missing"); expect(() => parseConfig(s3.replace('region = "fr-par"', ""))).toThrow( "[bucket] region is required", ); expect(() => parseConfig(s3.replace(hash, "plaintext"))).toThrow("Argon2id"); expect(() => parseConfig(`${s3}folder = "/tmp/b"\n`)).toThrow("cannot be combined"); expect(() => parseConfig(s3.replace("https://s3.fr-par.scw.cloud", "nope"))).toThrow( "must be a URL", ); });