aboutsummaryrefslogtreecommitdiff
path: root/workers/plugin-marketplace/src/index.test.ts
blob: b84ed14ddfc7238e2e3bf55bd52a55856f7e5940 (plain)
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import { describe, expect, test } from "bun:test";
import worker, { normalizeRepositories, refreshPlugins, type Env } from "./index";

class MemoryR2 {
  objects = new Map<string, { value: string; options: unknown }>();

  async put(key: string, value: string, options?: unknown): Promise<void> {
    this.objects.set(key, { value, options });
  }
}

class MemoryKV {
  constructor(private readonly keyNames: string[]) {}

  async list(options?: { prefix?: string }): Promise<{
    keys: Array<{ name: string }>;
  }> {
    return {
      keys: this.keyNames
        .filter((name) => !options?.prefix || name.startsWith(options.prefix))
        .map((name) => ({ name })),
    };
  }
}

function repo(overrides: Record<string, unknown> = {}): Record<string, unknown> {
  return {
    id: 1,
    full_name: "ogulcancelik/herdr-plugin-example",
    owner: { login: "ogulcancelik" },
    name: "herdr-plugin-example",
    description: "Example plugin",
    html_url: "https://github.com/ogulcancelik/herdr-plugin-example",
    stargazers_count: 5,
    forks_count: 1,
    open_issues_count: 0,
    language: "TypeScript",
    topics: ["herdr-plugin"],
    created_at: "2026-06-01T00:00:00Z",
    updated_at: "2026-06-02T00:00:00Z",
    pushed_at: "2026-06-03T00:00:00Z",
    archived: false,
    fork: false,
    disabled: false,
    private: false,
    visibility: "public",
    ...overrides,
  };
}

function env(bucket = new MemoryR2(), blacklist?: MemoryKV): Env {
  return {
    PLUGIN_MARKETPLACE_BUCKET: bucket,
    PLUGIN_MARKETPLACE_BLACKLIST: blacklist,
    GITHUB_TOKEN: "token",
  };
}

describe("normalizeRepositories", () => {
  test("normalizes repository fields into the public snapshot schema", () => {
    const [plugin] = normalizeRepositories([repo()]);

    expect(plugin).toEqual({
      id: 1,
      fullName: "ogulcancelik/herdr-plugin-example",
      owner: "ogulcancelik",
      name: "herdr-plugin-example",
      description: "Example plugin",
      url: "https://github.com/ogulcancelik/herdr-plugin-example",
      stars: 5,
      forks: 1,
      openIssues: 0,
      language: "TypeScript",
      topics: ["herdr-plugin"],
      createdAt: "2026-06-01T00:00:00Z",
      updatedAt: "2026-06-02T00:00:00Z",
      pushedAt: "2026-06-03T00:00:00Z",
    });
  });

  test("sorts by stars, pushed date, and full name", () => {
    const plugins = normalizeRepositories([
      repo({
        id: 1,
        full_name: "z/z",
        owner: { login: "z" },
        name: "z",
        html_url: "https://github.com/z/z",
        stargazers_count: 3,
        pushed_at: "2026-06-01T00:00:00Z",
      }),
      repo({
        id: 2,
        full_name: "a/a",
        owner: { login: "a" },
        name: "a",
        html_url: "https://github.com/a/a",
        stargazers_count: 3,
        pushed_at: "2026-06-02T00:00:00Z",
      }),
      repo({
        id: 3,
        full_name: "m/m",
        owner: { login: "m" },
        name: "m",
        html_url: "https://github.com/m/m",
        stargazers_count: 10,
      }),
    ]);

    expect(plugins.map((plugin) => plugin.fullName)).toEqual(["m/m", "a/a", "z/z"]);
  });

  test("drops unsafe urls, archived repositories, forks, disabled repositories, and private repositories", () => {
    const plugins = normalizeRepositories([
      repo({ html_url: "https://example.com/ogulcancelik/herdr-plugin-example" }),
      repo({ archived: true }),
      repo({ fork: true }),
      repo({ disabled: true }),
      repo({ private: true }),
      repo({ visibility: "private" }),
      repo({ id: 5 }),
    ]);

    expect(plugins.map((plugin) => plugin.id)).toEqual([5]);
  });

  test("uses safe defaults for missing nullable fields", () => {
    const [plugin] = normalizeRepositories([
      repo({
        id: undefined,
        description: undefined,
        stargazers_count: undefined,
        forks_count: undefined,
        open_issues_count: undefined,
        language: undefined,
        topics: undefined,
        created_at: "not a date",
        updated_at: undefined,
        pushed_at: undefined,
      }),
    ]);

    expect(plugin.id).toBe(0);
    expect(plugin.description).toBeNull();
    expect(plugin.stars).toBe(0);
    expect(plugin.forks).toBe(0);
    expect(plugin.openIssues).toBe(0);
    expect(plugin.language).toBeNull();
    expect(plugin.topics).toEqual([]);
    expect(plugin.createdAt).toBeNull();
    expect(plugin.updatedAt).toBeNull();
    expect(plugin.pushedAt).toBeNull();
  });
});

describe("refreshPlugins", () => {
  test("fetches pages and writes a sorted snapshot to R2 with cache metadata", async () => {
    const calls: string[] = [];
    const fetch = async (input: RequestInfo | URL): Promise<Response> => {
      const url = new URL(input.toString());
      calls.push(url.searchParams.get("page") ?? "");
      const page = url.searchParams.get("page");
      const item =
        page === "1"
          ? repo({ id: 1, full_name: "b/b", owner: { login: "b" }, name: "b", html_url: "https://github.com/b/b" })
          : repo({
              id: 2,
              full_name: "a/a",
              owner: { login: "a" },
              name: "a",
              html_url: "https://github.com/a/a",
              stargazers_count: 9,
            });
      return Response.json({ total_count: 2, items: [item] });
    };
    const bucket = new MemoryR2();

    const result = await refreshPlugins(env(bucket), {
      fetch,
      now: new Date("2026-06-20T12:00:00.000Z"),
      logger: { error() {} },
    });

    expect(result.ok).toBe(true);
    expect(calls).toEqual(["1", "2"]);
    const object = bucket.objects.get("plugins/index.json");
    expect(object?.options).toEqual({
      httpMetadata: {
        contentType: "application/json; charset=utf-8",
        cacheControl: "public, max-age=300, s-maxage=1800, stale-while-revalidate=3600",
      },
    });
    const snapshot = JSON.parse(object?.value ?? "");
    expect(snapshot.generatedAt).toBe("2026-06-20T12:00:00.000Z");
    expect(snapshot.source).toMatchObject({
      provider: "github",
      query: "topic:herdr-plugin is:public",
      totalCount: 2,
      collectedCount: 2,
      truncated: false,
    });
    expect(snapshot.plugins.map((plugin: { fullName: string }) => plugin.fullName)).toEqual([
      "a/a",
      "b/b",
    ]);
  });

  test("excludes repositories listed in the KV blacklist", async () => {
    const fetch = async (): Promise<Response> =>
      Response.json({
        total_count: 2,
        items: [
          repo({
            id: 1,
            full_name: "example/not-a-plugin",
            owner: { login: "example" },
            name: "not-a-plugin",
            html_url: "https://github.com/example/not-a-plugin",
          }),
          repo({
            id: 2,
            full_name: "ogulcancelik/herdr-plugin-example",
            owner: { login: "ogulcancelik" },
            name: "herdr-plugin-example",
            html_url: "https://github.com/ogulcancelik/herdr-plugin-example",
          }),
        ],
      });
    const bucket = new MemoryR2();

    const result = await refreshPlugins(env(bucket, new MemoryKV(["repo:example/not-a-plugin"])), {
      fetch,
      logger: { error() {} },
    });

    expect(result.ok).toBe(true);
    const snapshot = JSON.parse(bucket.objects.get("plugins/index.json")?.value ?? "");
    expect(snapshot.plugins.map((plugin: { fullName: string }) => plugin.fullName)).toEqual([
      "ogulcancelik/herdr-plugin-example",
    ]);
  });

  test("writes an empty snapshot when every listable repository is blacklisted", async () => {
    const fetch = async (): Promise<Response> =>
      Response.json({
        total_count: 1,
        items: [
          repo({
            id: 1,
            full_name: "example/not-a-plugin",
            owner: { login: "example" },
            name: "not-a-plugin",
            html_url: "https://github.com/example/not-a-plugin",
          }),
        ],
      });
    const bucket = new MemoryR2();
    await bucket.put("plugins/index.json", '{"schemaVersion":1,"plugins":[{"id":1}]}');

    const result = await refreshPlugins(env(bucket, new MemoryKV(["repo:example/not-a-plugin"])), {
      fetch,
      logger: { error() {} },
    });

    expect(result.ok).toBe(true);
    const snapshot = JSON.parse(bucket.objects.get("plugins/index.json")?.value ?? "");
    expect(snapshot.plugins).toEqual([]);
  });

  test("marks snapshots truncated at the GitHub search cap", async () => {
    const fetch = async (): Promise<Response> => {
      const items = Array.from({ length: 100 }, (_, index) =>
        repo({
          id: index,
          full_name: `owner/repo-${index}`,
          owner: { login: "owner" },
          name: `repo-${index}`,
          html_url: `https://github.com/owner/repo-${index}`,
        }),
      );
      return Response.json({ total_count: 1200, items });
    };

    const result = await refreshPlugins(env(), {
      fetch,
      now: new Date("2026-06-20T12:00:00.000Z"),
      logger: { error() {} },
    });

    expect(result.ok).toBe(true);
    if (!result.ok) return;
    expect(result.snapshot.source.collectedCount).toBe(1000);
    expect(result.snapshot.source.truncated).toBe(true);
    expect(result.snapshot.source.warnings?.[0]).toContain("1200");
  });

  for (const { name, fetch } of [
    {
      name: "GitHub failure",
      fetch: async (): Promise<Response> => new Response("rate limited", { status: 429 }),
    },
    {
      name: "no listable repositories",
      fetch: async (): Promise<Response> => Response.json({ total_count: 0, items: [] }),
    },
    {
      name: "incomplete search results",
      fetch: async (): Promise<Response> =>
        Response.json({ total_count: 1, incomplete_results: true, items: [repo()] }),
    },
  ]) {
    test(`does not overwrite the R2 snapshot on ${name}`, async () => {
      const bucket = new MemoryR2();
      await bucket.put("plugins/index.json", '{"schemaVersion":1,"plugins":[{"id":1}]}');

      const result = await refreshPlugins(env(bucket), {
        fetch,
        logger: { error() {} },
      });

      expect(result.ok).toBe(false);
      expect(bucket.objects.get("plugins/index.json")?.value).toBe(
        '{"schemaVersion":1,"plugins":[{"id":1}]}',
      );
    });
  }
});

describe("fetch handler", () => {
  test("does not expose a public Worker API", async () => {
    const response = await worker.fetch(new Request("https://herdr.dev/api/plugins"), env());

    expect(response.status).toBe(404);
    expect(response.headers.get("Cache-Control")).toBe("no-store");
  });
});