CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | /**
* Tests for Block C2 — npm-compatible package registry.
*
* Covers the pure helpers in `src/lib/packages.ts` and a handful of
* route-level behaviour guarantees (401 without auth, 404 for unknown
* packages). The integration paths — actual publish → install cycles —
* are exercised by higher-level tests once a real test DB is wired.
*/
import { describe, it, expect } from "bun:test";
import app from "../app";
import {
parsePackageName,
parseRepoUrl,
computeShasum,
computeIntegrity,
buildPackument,
resolveRepoFromPackageJson,
tarballFilename,
} from "../lib/packages";
describe("parsePackageName", () => {
it("parses a plain name", () => {
const r = parsePackageName("left-pad");
expect(r).not.toBeNull();
expect(r!.scope).toBeNull();
expect(r!.name).toBe("left-pad");
expect(r!.full).toBe("left-pad");
});
it("parses a scoped name", () => {
const r = parsePackageName("@acme/widgets");
expect(r).not.toBeNull();
expect(r!.scope).toBe("@acme");
expect(r!.name).toBe("widgets");
expect(r!.full).toBe("@acme/widgets");
});
it("accepts a URL-encoded scoped name (%2F)", () => {
const r = parsePackageName("@acme%2Fwidgets");
expect(r).not.toBeNull();
expect(r!.scope).toBe("@acme");
expect(r!.name).toBe("widgets");
});
it("rejects empty strings", () => {
expect(parsePackageName("")).toBeNull();
expect(parsePackageName(" ")).toBeNull();
});
it("rejects malformed scope-only input", () => {
expect(parsePackageName("@")).toBeNull();
expect(parsePackageName("@acme")).toBeNull();
expect(parsePackageName("@/foo")).toBeNull();
});
it("rejects names with spaces or weird chars", () => {
expect(parsePackageName("foo bar")).toBeNull();
expect(parsePackageName("foo/bar")).toBeNull(); // would be scope-style without @
expect(parsePackageName("../etc")).toBeNull();
});
it("allows legal characters (dot, dash, underscore, digits)", () => {
expect(parsePackageName("a.b_c-1")).not.toBeNull();
expect(parsePackageName("@s_c.o-pe/n.a_m-e1")).not.toBeNull();
});
});
describe("computeShasum", () => {
it("returns a 40-char lowercase hex string", () => {
const bytes = new TextEncoder().encode("hello world");
const out = computeShasum(bytes);
expect(out).toMatch(/^[0-9a-f]{40}$/);
});
it("matches the known sha1 of 'hello world'", () => {
const bytes = new TextEncoder().encode("hello world");
expect(computeShasum(bytes)).toBe(
"2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"
);
});
it("is stable across calls", () => {
const bytes = new TextEncoder().encode("stable-input");
expect(computeShasum(bytes)).toBe(computeShasum(bytes));
});
});
describe("computeIntegrity", () => {
it("prefixes with sha512-", () => {
const bytes = new TextEncoder().encode("integrity-test");
const out = computeIntegrity(bytes);
expect(out.startsWith("sha512-")).toBe(true);
});
it("base64 body decodes to 64 bytes (sha512 digest length)", () => {
const bytes = new TextEncoder().encode("xyz");
const out = computeIntegrity(bytes);
const body = out.slice("sha512-".length);
const decoded = Buffer.from(body, "base64");
expect(decoded.length).toBe(64);
});
});
describe("resolveRepoFromPackageJson", () => {
it("accepts the object form with url", () => {
const r = resolveRepoFromPackageJson({
repository: { type: "git", url: "https://gluecron.com/alice/foo.git" },
});
expect(r).toEqual({ owner: "alice", repo: "foo" });
});
it("accepts the string shorthand", () => {
const r = resolveRepoFromPackageJson({
repository: "https://gluecron.com/bob/bar.git",
});
expect(r).toEqual({ owner: "bob", repo: "bar" });
});
it("accepts git+https URLs", () => {
const r = resolveRepoFromPackageJson({
repository: { url: "git+https://gluecron.com/alice/foo.git" },
});
expect(r).toEqual({ owner: "alice", repo: "foo" });
});
it("accepts SCP-style git@ URLs", () => {
const r = resolveRepoFromPackageJson({
repository: "git@gluecron.com:alice/foo.git",
});
expect(r).toEqual({ owner: "alice", repo: "foo" });
});
it("returns null when repository is missing", () => {
expect(resolveRepoFromPackageJson({})).toBeNull();
expect(resolveRepoFromPackageJson(null)).toBeNull();
expect(resolveRepoFromPackageJson("not-an-object")).toBeNull();
});
it("returns null for empty / malformed URLs", () => {
expect(resolveRepoFromPackageJson({ repository: "" })).toBeNull();
expect(resolveRepoFromPackageJson({ repository: "noslashes" })).toBeNull();
});
});
describe("parseRepoUrl (direct)", () => {
it("strips trailing .git", () => {
expect(parseRepoUrl("http://localhost:3000/a/b.git")).toEqual({
owner: "a",
repo: "b",
});
});
it("handles bare owner/repo path", () => {
expect(parseRepoUrl("alice/foo")).toEqual({ owner: "alice", repo: "foo" });
});
});
describe("buildPackument", () => {
const pkg = {
id: "pkg-1",
repositoryId: "repo-1",
ecosystem: "npm",
scope: null,
name: "widgets",
description: "A package of widgets",
readme: "# widgets",
homepage: "https://example.com",
license: "MIT",
visibility: "public",
createdAt: new Date("2024-01-01T00:00:00Z"),
updatedAt: new Date("2024-01-01T00:00:00Z"),
} as const;
const v1 = {
id: "v1",
packageId: "pkg-1",
version: "1.0.0",
shasum: "aaaa",
integrity: "sha512-deadbeef",
sizeBytes: 123,
metadata: JSON.stringify({ name: "widgets", version: "1.0.0" }),
tarball: null,
publishedBy: "user-1",
yanked: false,
yankedReason: null,
publishedAt: new Date("2024-01-02T00:00:00Z"),
} as const;
const v2 = {
...v1,
id: "v2",
version: "1.1.0",
shasum: "bbbb",
publishedAt: new Date("2024-01-10T00:00:00Z"),
} as const;
it("returns name, dist-tags, and versions", () => {
const doc = buildPackument(
pkg as any,
[v1, v2] as any,
[
{
id: "t1",
packageId: "pkg-1",
tag: "latest",
versionId: "v2",
updatedAt: new Date(),
} as any,
],
"http://host:3000"
);
expect(doc.name).toBe("widgets");
expect((doc["dist-tags"] as Record<string, string>).latest).toBe("1.1.0");
expect((doc.versions as Record<string, unknown>)["1.0.0"]).toBeDefined();
expect((doc.versions as Record<string, unknown>)["1.1.0"]).toBeDefined();
});
it("falls back to most-recent version for latest if no tag rows", () => {
const doc = buildPackument(
pkg as any,
[v1, v2] as any,
[],
"http://host:3000"
);
expect((doc["dist-tags"] as Record<string, string>).latest).toBe("1.1.0");
});
it("embeds tarball URLs under dist", () => {
const doc = buildPackument(
pkg as any,
[v1] as any,
[],
"http://host:3000"
);
const ver = (doc.versions as any)["1.0.0"];
expect(ver.dist.tarball).toContain("http://host:3000/npm/widgets/-/widgets-1.0.0.tgz");
expect(ver.dist.shasum).toBe("aaaa");
expect(ver.dist.integrity).toBe("sha512-deadbeef");
});
it("uses full scoped name in url for scoped packages", () => {
const scoped = { ...pkg, scope: "@acme", name: "widgets" } as any;
const doc = buildPackument(scoped, [v1] as any, [], "http://h");
expect(doc.name).toBe("@acme/widgets");
const ver = (doc.versions as any)["1.0.0"];
// Tarball path encodes @acme/widgets but filename uses just "widgets".
expect(ver.dist.tarball).toContain("/npm/@acme/widgets/-/widgets-1.0.0.tgz");
});
});
describe("tarballFilename", () => {
it("builds <name>-<version>.tgz for plain packages", () => {
const parsed = parsePackageName("widgets")!;
expect(tarballFilename(parsed, "1.2.3")).toBe("widgets-1.2.3.tgz");
});
it("omits the scope from the filename for scoped packages", () => {
const parsed = parsePackageName("@acme/widgets")!;
expect(tarballFilename(parsed, "1.2.3")).toBe("widgets-1.2.3.tgz");
});
});
// ---------------------------------------------------------------------------
// Route-level guard tests
// ---------------------------------------------------------------------------
describe("packages routes — unauthed behaviour", () => {
it("PUT /npm/:name without auth returns 401 (JSON)", async () => {
const res = await app.request("/npm/some-package", {
method: "PUT",
headers: {
"content-type": "application/json",
authorization: "Bearer glct_does-not-exist",
},
body: JSON.stringify({
name: "some-package",
versions: { "1.0.0": { name: "some-package", version: "1.0.0" } },
_attachments: {
"some-package-1.0.0.tgz": {
content_type: "application/octet-stream",
data: "aGVsbG8=",
length: 5,
},
},
}),
});
// Either 401 (bad bearer) or 404 (route may not be mounted in main yet;
// we don't edit app.tsx). Assert tolerant but meaningful.
expect([401, 404]).toContain(res.status);
});
it("GET /npm/does-not-exist returns 404", async () => {
const res = await app.request("/npm/does-not-exist-package-xyz");
// 404 from our handler, 503 if DB down, or 404 from app-level notFound if
// the route isn't yet mounted.
expect([404, 503]).toContain(res.status);
});
});
|