mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-20 22:00:43 +08:00
Covers the pure modules the rest of the app builds on, starting with the cheapest layer of the testing strategy.
156 lines
4.3 KiB
TypeScript
156 lines
4.3 KiB
TypeScript
import type { RepoRoute } from "@app/views/repo/router";
|
|
|
|
import { repoRouteToPath, repoUrlToRoute } from "@app/views/repo/router";
|
|
import { describe, expect, test } from "vitest";
|
|
|
|
const rid = "rad:z3fpY7nttPPa6MBnAv2DccHzQJnqe";
|
|
const peer = "z6MkqGC3nWZhYieEVTVDKW5v588CiGfsDSmRVG9ZwwWTvLSK";
|
|
const sha = "a".repeat(40);
|
|
|
|
function parse(path: string): RepoRoute | null {
|
|
const url = new URL(path, "http://localhost");
|
|
const segments = url.pathname.substring(1).split("/");
|
|
expect(segments.shift()).toBe("repos");
|
|
return repoUrlToRoute(segments, url.searchParams);
|
|
}
|
|
|
|
describe("repoRouteToPath and repoUrlToRoute round trip", () => {
|
|
test.each<[string, RepoRoute]>([
|
|
["/repos/RID/home", { resource: "repo.home", rid }],
|
|
["/repos/RID/home/remotes/PEER", { resource: "repo.home", rid, peer }],
|
|
[
|
|
"/repos/RID/home/feature/branch",
|
|
{ resource: "repo.home", rid, revision: "feature/branch" },
|
|
],
|
|
[
|
|
"/repos/RID/home/remotes/PEER/feature/branch",
|
|
{ resource: "repo.home", rid, peer, revision: "feature/branch" },
|
|
],
|
|
["/repos/RID/commits", { resource: "repo.commits", rid }],
|
|
[
|
|
"/repos/RID/commits/main",
|
|
{ resource: "repo.commits", rid, revision: "main" },
|
|
],
|
|
[
|
|
"/repos/RID/commits/remotes/PEER/SHA",
|
|
{ resource: "repo.commits", rid, peer, revision: sha },
|
|
],
|
|
["/repos/RID/commits/SHA", { resource: "repo.commit", rid, commit: sha }],
|
|
["/repos/RID/identity", { resource: "repo.identity", rid }],
|
|
[
|
|
"/repos/RID/issues?status=all",
|
|
{ resource: "repo.issues", rid, status: "all" },
|
|
],
|
|
[
|
|
"/repos/RID/issues?status=closed",
|
|
{ resource: "repo.issues", rid, status: "closed" },
|
|
],
|
|
[
|
|
"/repos/RID/issues/abc?status=open",
|
|
{ resource: "repo.issue", rid, issue: "abc", status: "open" },
|
|
],
|
|
[
|
|
"/repos/RID/patches",
|
|
{ resource: "repo.patches", rid, status: undefined },
|
|
],
|
|
[
|
|
"/repos/RID/patches?status=merged",
|
|
{ resource: "repo.patches", rid, status: "merged" },
|
|
],
|
|
[
|
|
"/repos/RID/patches/abc",
|
|
{
|
|
resource: "repo.patch",
|
|
rid,
|
|
patch: "abc",
|
|
status: undefined,
|
|
reviewId: undefined,
|
|
},
|
|
],
|
|
[
|
|
"/repos/RID/patches/abc?status=draft&review=def&view=changes",
|
|
{
|
|
resource: "repo.patch",
|
|
rid,
|
|
patch: "abc",
|
|
status: "draft",
|
|
reviewId: "def",
|
|
view: "changes",
|
|
},
|
|
],
|
|
])("%s", (template, route) => {
|
|
const path = template
|
|
.replace("RID", rid)
|
|
.replace("PEER", peer)
|
|
.replace("SHA", sha);
|
|
|
|
expect(repoRouteToPath(route)).toBe(path);
|
|
expect(parse(path)).toEqual(route);
|
|
});
|
|
});
|
|
|
|
describe("repoUrlToRoute", () => {
|
|
test("defaults issues listing status to all", () => {
|
|
expect(parse(`/repos/${rid}/issues`)).toEqual({
|
|
resource: "repo.issues",
|
|
rid,
|
|
status: "all",
|
|
});
|
|
});
|
|
|
|
test("falls back to all for an unknown issues listing status", () => {
|
|
expect(parse(`/repos/${rid}/issues?status=bogus`)).toEqual({
|
|
resource: "repo.issues",
|
|
rid,
|
|
status: "all",
|
|
});
|
|
});
|
|
|
|
test("defaults single issue status to all", () => {
|
|
expect(parse(`/repos/${rid}/issues/abc`)).toEqual({
|
|
resource: "repo.issue",
|
|
rid,
|
|
issue: "abc",
|
|
status: "all",
|
|
});
|
|
});
|
|
|
|
test("does not route issue creation", () => {
|
|
expect(parse(`/repos/${rid}/issues/create`)).toBeNull();
|
|
});
|
|
|
|
test("ignores an unknown patch view", () => {
|
|
expect(parse(`/repos/${rid}/patches/abc?view=bogus`)).toEqual({
|
|
resource: "repo.patch",
|
|
rid,
|
|
patch: "abc",
|
|
});
|
|
});
|
|
|
|
test("treats a sha as a revision when a peer is given", () => {
|
|
expect(parse(`/repos/${rid}/commits/remotes/${peer}/${sha}`)).toEqual({
|
|
resource: "repo.commits",
|
|
rid,
|
|
peer,
|
|
revision: sha,
|
|
});
|
|
});
|
|
|
|
test("treats a commits revision that is a sha as a single commit", () => {
|
|
const route: RepoRoute = { resource: "repo.commits", rid, revision: sha };
|
|
|
|
expect(parse(repoRouteToPath(route))).toEqual({
|
|
resource: "repo.commit",
|
|
rid,
|
|
commit: sha,
|
|
});
|
|
});
|
|
|
|
test.each([`/repos/${rid}`, `/repos/${rid}/bogus`, "/repos/"])(
|
|
"returns null for %s",
|
|
path => {
|
|
expect(parse(path)).toBeNull();
|
|
},
|
|
);
|
|
});
|