project-name.test.ts
This file
Path
packages/protocol/test/project-name.test.ts
266 lines · 8.1 KiB · TypeScript
Last changed by
Merge redesign into mainoscabrielbefore adoptioncaedd21
History of this fileTake it
Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
lines 1–147why, one step back1import { describe, expect, it } from "@effect/vitest";2import { Option, Result, Schema } from "effect";34import {5 isSourceNamespace,6 keyOf,7 nameOfKey,8 Owner,9 ownerOf,10 ProjectKey,11 ProjectName,12 projectAddressOf,13 projectNameOf,14 reservedProjectNames,15 ShortName,16 sourceHosts,17 sourceNamespaceOf,18 shortNameOf,19} from "../src/ids.ts";20import { legacyProjects } from "../src/legacy.ts";2122const decode = Schema.decodeResult(ProjectName);2324const decoded = (input: string) =>25 Result.match(decode(input), {26 onFailure: () => Option.none<string>(),27 onSuccess: (name) => Option.some<string>(name),28 });2930describe("a project's name", () => {31 it.each([32 "oscabriel/datekit",33 "github.com/pallets/click",34 "codeberg.org/dnkl/fuzzel",35 "github.com/tj/commander.js",36 "alice/the_thing",37 "0day/x",38 ])("is an owner, then a short name: %j", (name) => {39 expect(decoded(name)).toStrictEqual(Option.some(name));40 expect(ProjectName.make(name)).toBe(name);41 });4243 it("is lowercase, whatever the request spelled", () => {44 expect(decoded("OscaBriel/Datekit")).toStrictEqual(45 Option.some("oscabriel/datekit")46 );47 expect(decoded("GitHub.com/Pallets/Click")).toStrictEqual(48 Option.some("github.com/pallets/click")49 );50 });5152 it("reads a legacy key as the name the table gives it", () => {53 expect(decoded("datekit")).toStrictEqual(Option.some("oscabriel/datekit"));54 expect(decoded("burrow")).toStrictEqual(Option.some("oscabriel/burrow"));55 expect(decoded("click")).toStrictEqual(56 Option.some("github.com/pallets/click")57 );58 expect(decoded("fuzzel")).toStrictEqual(59 Option.some("codeberg.org/dnkl/fuzzel")60 );61 expect(decoded("commander")).toStrictEqual(62 Option.some("github.com/tj/commander")63 );64 });6566 it.each([67 "foo",68 "smoke-20251010",69 "docs/foo",70 "import/foo",71 "people/foo",72 "github.com/foo",73 "gitlab.com/a/b/c",74 "example.com/a/b",75 "oscabriel/foo--bar",76 "oscabriel/LLMS.txt",77 "oscabriel/notes.md",78 "oscabriel/-foo",79 "oscabriel/foo.",80 "a--b/foo",81 "oscabriel/",82 "/datekit",83 "oscabriel/datekit/",84 "",85 ])("refuses %j", (input) => {86 expect(decoded(input)).toStrictEqual(Option.none());87 });8889 it("is made only in its full form", () => {90 expect(() => ProjectName.make("datekit")).toThrow(91 "Schema validation failed"92 );93 expect(() => ProjectName.make("Oscabriel/datekit")).toThrow(94 "Schema validation failed"95 );96 });9798 it("encodes to the full name, never to a key", () => {99 expect(100 Schema.encodeSync(ProjectName)(ProjectName.make("oscabriel/datekit"))101 ).toBe("oscabriel/datekit");102 });103104 it("is a string with a pattern in JSON Schema, with no null in it", () => {105 const json = JSON.stringify(Schema.toJsonSchemaDocument(ProjectName));106107 expect(json).toContain('"type":"string"');108 expect(json).toContain('"pattern"');109 expect(json).not.toContain("null");110 });111112 it("splits into its owner and its short name", () => {113 const name = ProjectName.make("github.com/pallets/click");114115 expect(ownerOf(name)).toBe("github.com/pallets");116 expect(shortNameOf(name)).toBe("click");117 expect(ownerOf(ProjectName.make("oscabriel/datekit"))).toBe("oscabriel");118 expect(119 projectNameOf(Owner.make("oscabriel"), ShortName.make("datekit"))120 ).toBe("oscabriel/datekit");121 });122});123124describe("an owner", () => {125 it("is a handle or a source namespace, never both", () => {126 expect(isSourceNamespace(Owner.make("oscabriel"))).toBeFalsy();127 expect(isSourceNamespace(Owner.make("github.com/pallets"))).toBeTruthy();128 expect(Schema.is(Owner)("github.com")).toBeFalsy();129 expect(Schema.is(Owner)("docs")).toBeFalsy();130 });131132 it("is read from an import's URL when its host is one the platform knows", () => {133 expect(134 sourceNamespaceOf("https://github.com/Pallets/click.git")135 ).toStrictEqual(Option.some("github.com/pallets"));136 expect(sourceNamespaceOf("https://codeberg.org/dnkl/fuzzel")).toStrictEqual(137 Option.some("codeberg.org/dnkl")138 );139 expect(sourceNamespaceOf("https://example.com/a/b")).toStrictEqual(140 Option.none()141 );142 expect(sourceNamespaceOf("https://github.com/pallets")).toStrictEqual(143 Option.none()144 );145 expect(sourceHosts).toContain("gitlab.com");146 });147Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 148–162why, one step back148 // ADR 0041: the pages the redesign adds, and the old ones kept for their redirects.149 it("is never a page the four shells add, nor one kept for its redirect", () => {150 expect(reservedProjectNames).toStrictEqual(151 expect.arrayContaining([152 "agents",153 "explore",154 "home",155 "inbox",156 "install",157 "settings",158 "today",159 ])160 );161 });162Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
lines 163–266why, one step back163 it("is never a word the web serves its own pages at", () => {164 expect(reservedProjectNames).toContain("import");165166 for (const word of reservedProjectNames) {167 expect(decoded(`${word}/foo`)).toStrictEqual(Option.none());168 }169 });170});171172describe("a project's key", () => {173 it("is the name with its slashes made underscores", () => {174 expect(keyOf(ProjectName.make("oscabriel/foo"))).toBe("oscabriel__foo");175 expect(keyOf(ProjectName.make("github.com/pallets/werkzeug"))).toBe(176 "github.com__pallets__werkzeug"177 );178 });179180 it("is the legacy key for a project made before owners", () => {181 expect(keyOf(ProjectName.make("oscabriel/datekit"))).toBe("datekit");182 expect(keyOf(ProjectName.make("github.com/pallets/click"))).toBe("click");183 });184185 it("reverses to the name, and to nothing for a key no project has", () => {186 expect(nameOfKey(ProjectKey.make("datekit"))).toStrictEqual(187 Option.some("oscabriel/datekit")188 );189 expect(nameOfKey(ProjectKey.make("oscabriel__foo"))).toStrictEqual(190 Option.some("oscabriel/foo")191 );192 expect(193 nameOfKey(ProjectKey.make("github.com__pallets__werkzeug"))194 ).toStrictEqual(Option.some("github.com/pallets/werkzeug"));195 expect(nameOfKey(ProjectKey.make("smoke-20251010"))).toStrictEqual(196 Option.none()197 );198 expect(nameOfKey(ProjectKey.make("docs_foo"))).toStrictEqual(Option.none());199 });200201 it("round-trips every legacy row and every new name", () => {202 for (const { key, name } of legacyProjects) {203 expect(keyOf(ProjectName.make(name))).toBe(key);204 expect(nameOfKey(ProjectKey.make(key))).toStrictEqual(Option.some(name));205 }206207 expect(() => ProjectKey.make("a--b")).toThrow("Schema validation failed");208 });209});210211describe("the legacy table", () => {212 it("names the two examples and the imports, each key once", () => {213 const keys = legacyProjects.map((row) => row.key);214215 expect(new Set(keys).size).toBe(keys.length);216 expect(keys).toContain("datekit");217 expect(keys).toContain("burrow");218 expect(keys).toHaveLength(40);219 });220});221222const addressOf = (path: string) =>223 Option.map(224 projectAddressOf(path.split("/").filter((segment) => segment !== "")),225 ({ legacy, rest, spelled }) => ({ legacy, rest: rest.join("/"), spelled })226 );227228describe("a project's address", () => {229 it.each([230 ["/oscabriel/datekit/tree/main", "oscabriel/datekit", "tree/main", false],231 ["/github.com/pallets/click", "github.com/pallets/click", "", false],232 [233 "/GitHub.com/Pallets/click/asks/x",234 "GitHub.com/Pallets/click",235 "asks/x",236 false,237 ],238 ["/datekit/ledger", "datekit", "ledger", true],239 ["/Click", "Click", "", true],240 ])("reads %j as its name and its page", (path, spelled, rest, legacy) => {241 expect(addressOf(path)).toStrictEqual(242 Option.some({ legacy, rest, spelled })243 );244 });245246 it.each(["/", "/oscabriel", "/github.com/pallets"])(247 "reads nothing in %j, too short for a name",248 (path) => {249 expect(addressOf(path)).toStrictEqual(Option.none());250 }251 );252});253254describe("a key of a name whose segments hold underscores", () => {255 it("reads back to its name", () => {256 const name = ProjectName.make("alice/the_thing");257258 expect(nameOfKey(keyOf(name))).toStrictEqual(Option.some(name));259 });260261 it("is not another name's key", () => {262 expect(keyOf(ProjectName.make("github.com/a_b/c"))).not.toBe(263 keyOf(ProjectName.make("github.com/a/b_c"))264 );265 });266});