Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions server/og/fetchPluginOgMeta.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* @vitest-environment node */

import { afterEach, describe, expect, it, vi } from "vitest";
import { fetchPluginOgMeta } from "./fetchPluginOgMeta";

describe("fetchPluginOgMeta", () => {
afterEach(() => {
vi.unstubAllGlobals();
});

it("reads installs from package API stats", async () => {
const fetchMock = vi.fn(async () => ({
ok: true,
json: async () => ({
package: {
name: "@openclaw/codex",
displayName: "Codex",
summary: "OpenClaw Codex harness.",
latestVersion: "1.0.0",
stats: { downloads: 99, installs: 1200 },
verification: { scanStatus: "clean" },
},
owner: { handle: "openclaw", image: null },
}),
}));
vi.stubGlobal("fetch", fetchMock);

const meta = await fetchPluginOgMeta("@openclaw/codex", "https://clawhub.ai");

expect(fetchMock).toHaveBeenCalledWith(
"https://clawhub.ai/api/v1/packages/%40openclaw%2Fcodex",
{ headers: { Accept: "application/json" } },
);
expect(meta?.stats.installs).toBe(1200);
});
});
4 changes: 2 additions & 2 deletions server/og/fetchPluginOgMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type PluginOgMeta = {
ownerImage: string | null;
latestVersion: string | null;
stats: {
downloads: number;
installs: number;
};
verification: {
scanStatus: string | null;
Expand Down Expand Up @@ -41,7 +41,7 @@ export async function fetchPluginOgMeta(
ownerImage: payload.owner?.image ?? null,
latestVersion: payload.package?.latestVersion ?? null,
stats: {
downloads: readNumber(stats.downloads),
installs: readNumber(stats.installs),
},
verification: payload.package?.verification
? { scanStatus: payload.package.verification.scanStatus ?? null }
Expand Down
51 changes: 51 additions & 0 deletions server/og/fetchPublisherOgMeta.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* @vitest-environment node */

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

const queryMock = vi.fn();
const clientCtorMock = vi.fn();

vi.mock("convex/browser", () => ({
ConvexHttpClient: class ConvexHttpClientMock {
constructor(url: string) {
clientCtorMock(url);
}

query = queryMock;
},
}));

vi.mock("../../convex/_generated/api", () => ({
api: { publishers: { getProfileByHandle: "publishers.getProfileByHandle" } },
}));

describe("fetchPublisherOgMeta", () => {
beforeEach(() => {
queryMock.mockReset();
clientCtorMock.mockReset();
});

afterEach(() => {
vi.resetModules();
});

it("reads installs from publisher profile stats", async () => {
queryMock.mockResolvedValue({
handle: "openclaw",
kind: "org",
displayName: "OpenClaw",
bio: "Build with claws.",
image: null,
stats: { downloads: 99, installs: 1200 },
});

const { fetchPublisherOgMeta } = await import("./fetchPublisherOgMeta");
const meta = await fetchPublisherOgMeta("openclaw", "https://example.convex.cloud");

expect(clientCtorMock).toHaveBeenCalledWith("https://example.convex.cloud");
expect(queryMock).toHaveBeenCalledWith("publishers.getProfileByHandle", {
handle: "openclaw",
});
expect(meta?.stats.installs).toBe(1200);
});
});
6 changes: 3 additions & 3 deletions server/og/fetchPublisherOgMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type PublisherOgMeta = {
bio: string | null;
image: string | null;
stats: {
downloads: number;
installs: number;
};
};

Expand All @@ -19,7 +19,7 @@ type PublisherProfileResult = {
bio?: string | null;
image?: string | null;
stats?: {
downloads?: number;
installs?: number;
};
} | null;

Expand All @@ -40,7 +40,7 @@ export async function fetchPublisherOgMeta(
bio: profile.bio ?? null,
image: profile.image ?? null,
stats: {
downloads: readNumber(profile.stats?.downloads),
installs: readNumber(profile.stats?.installs),
},
};
} catch {
Expand Down
37 changes: 37 additions & 0 deletions server/og/fetchSkillOgMeta.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* @vitest-environment node */

import { afterEach, describe, expect, it, vi } from "vitest";
import { fetchSkillOgMeta } from "./fetchSkillOgMeta";

describe("fetchSkillOgMeta", () => {
afterEach(() => {
vi.unstubAllGlobals();
});

it("reads all-time installs from the public skill API stats", async () => {
const fetchMock = vi.fn(async () => ({
ok: true,
json: async () => ({
skill: {
displayName: "Gifgrep",
summary: "Search GIFs fast",
stats: { downloads: 99, installsAllTime: 1200 },
},
owner: { handle: "steipete", image: "https://avatars.githubusercontent.com/u/1?v=4" },
latestVersion: { version: "1.0.1" },
moderation: { verdict: "clean", isSuspicious: false, isMalwareBlocked: false },
}),
}));
vi.stubGlobal("fetch", fetchMock);

const meta = await fetchSkillOgMeta("gifgrep", "https://clawhub.ai", "@steipete");

expect(fetchMock).toHaveBeenCalledWith(
"https://clawhub.ai/api/v1/skills/gifgrep?ownerHandle=steipete",
{
headers: { Accept: "application/json" },
},
);
expect(meta?.stats.installsAllTime).toBe(1200);
});
});
12 changes: 9 additions & 3 deletions server/og/fetchSkillOgMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type SkillOgMeta = {
ownerImage: string | null;
version: string | null;
stats: {
downloads: number;
installsAllTime: number;
};
moderation: {
verdict: "clean" | "suspicious" | "malicious" | null;
Expand All @@ -14,9 +14,15 @@ export type SkillOgMeta = {
} | null;
};

export async function fetchSkillOgMeta(slug: string, apiBase: string): Promise<SkillOgMeta | null> {
export async function fetchSkillOgMeta(
slug: string,
apiBase: string,
ownerHandle?: string | null,
): Promise<SkillOgMeta | null> {
try {
const url = new URL(`/api/v1/skills/${encodeURIComponent(slug)}`, apiBase);
const owner = ownerHandle?.trim().replace(/^@+/, "");
if (owner) url.searchParams.set("ownerHandle", owner);
const response = await fetch(url.toString(), { headers: { Accept: "application/json" } });
if (!response.ok) return null;
const payload = (await response.json()) as {
Expand All @@ -37,7 +43,7 @@ export async function fetchSkillOgMeta(slug: string, apiBase: string): Promise<S
ownerImage: payload.owner?.image ?? null,
version: payload.latestVersion?.version ?? null,
stats: {
downloads: readNumber(stats.downloads),
installsAllTime: readNumber(stats.installsAllTime),
},
moderation: payload.moderation
? {
Expand Down
2 changes: 1 addition & 1 deletion server/og/skillOgSvg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe("skill OG SVG", () => {
target: "discord-doctor",
},
stats: [
{ value: "1.2k", label: "Downloads" },
{ value: "1.2k", label: "Installs" },
{ value: "PASS", label: "Audit" },
],
});
Expand Down
35 changes: 30 additions & 5 deletions server/routes/og/plugin.png.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe("plugin og route", () => {
latestVersion: null,
displayName: "Codex",
summary: "OpenClaw Codex harness.",
stats: { downloads: 1200 },
stats: { installs: 1200 },
verification: { scanStatus: "pending" },
});

Expand All @@ -126,7 +126,7 @@ describe("plugin og route", () => {
expect(buildPluginOgSvgMock).toHaveBeenCalledWith(
expect.objectContaining({
stats: [
{ value: "1.2k", label: "Downloads" },
{ value: "1.2k", label: "Installs" },
{ value: "PENDING", label: "Audit" },
],
}),
Expand All @@ -142,7 +142,7 @@ describe("plugin og route", () => {
latestVersion: "1.0.0",
displayName: "Codex",
summary: "OpenClaw Codex harness.",
stats: { downloads: 1200 },
stats: { installs: 1200 },
verification: { scanStatus: "clean" },
});

Expand All @@ -153,7 +153,7 @@ describe("plugin og route", () => {
expect(buildPluginOgSvgMock).toHaveBeenCalledWith(
expect.objectContaining({
stats: [
{ value: "1.2k", label: "Downloads" },
{ value: "1.2k", label: "Installs" },
{ value: "PASS", label: "Audit" },
],
}),
Expand All @@ -166,6 +166,7 @@ describe("plugin og route", () => {
owner: "openclaw",
title: "Codex",
description: "OpenClaw Codex harness.",
installs: "0",
});

const handler = (await import("./plugin.png")).default;
Expand All @@ -175,7 +176,31 @@ describe("plugin og route", () => {
expect(buildPluginOgSvgMock).toHaveBeenCalledWith(
expect.objectContaining({
stats: [
{ value: "0", label: "Downloads" },
{ value: "0", label: "Installs" },
{ value: "UNKNOWN", label: "Audit" },
],
}),
);
});

it("uses explicit installs over legacy downloads query params", async () => {
getQueryMock.mockReturnValue({
name: "@openclaw/codex",
owner: "openclaw",
title: "Codex",
description: "OpenClaw Codex harness.",
installs: "0",
downloads: "9.9k",
});

const handler = (await import("./plugin.png")).default;
await handler({} as never);

expect(fetchPluginOgMetaMock).not.toHaveBeenCalled();
expect(buildPluginOgSvgMock).toHaveBeenCalledWith(
expect.objectContaining({
stats: [
{ value: "0", label: "Installs" },
{ value: "UNKNOWN", label: "Audit" },
],
}),
Expand Down
8 changes: 4 additions & 4 deletions server/routes/og/plugin.png.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type OgQuery = {
owner?: string;
title?: string;
description?: string;
downloads?: string;
installs?: string;
audit?: string;
avatar?: string;
v?: string;
Expand Down Expand Up @@ -63,7 +63,7 @@ export default defineEventHandler(async (event) => {
const ownerFromQuery = cleanString(query.owner);
const titleFromQuery = cleanString(query.title);
const descriptionFromQuery = cleanString(query.description);
const downloadsFromQuery = cleanString(query.downloads);
const installsFromQuery = cleanString(query.installs);
const auditFromQuery = cleanString(query.audit);
const avatarFromQuery = cleanString(query.avatar);
const needFetch = !ownerFromQuery || !titleFromQuery || !descriptionFromQuery;
Expand Down Expand Up @@ -100,8 +100,8 @@ export default defineEventHandler(async (event) => {
},
stats: [
{
value: downloadsFromQuery || formatOgStat(meta?.stats.downloads),
label: "Downloads",
value: installsFromQuery || formatOgStat(meta?.stats.installs),
label: "Installs",
},
{
value: (auditFromQuery || getAuditLabel(meta?.verification?.scanStatus)).replace(
Expand Down
10 changes: 5 additions & 5 deletions server/routes/og/profile.png.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type OgQuery = {
handle?: string;
title?: string;
description?: string;
downloads?: string;
installs?: string;
kind?: string;
avatar?: string;
v?: string;
Expand All @@ -43,11 +43,11 @@ export default defineEventHandler(async (event) => {

const titleFromQuery = cleanString(query.title);
const descriptionFromQuery = cleanString(query.description);
const downloadsFromQuery = cleanString(query.downloads);
const installsFromQuery = cleanString(query.installs);
const kindFromQuery = cleanString(query.kind);
const avatarFromQuery = cleanString(query.avatar);
const convexUrl = getConvexUrl();
const needFetch = !titleFromQuery || !descriptionFromQuery || !downloadsFromQuery;
const needFetch = !titleFromQuery || !descriptionFromQuery || !installsFromQuery;
const meta = needFetch && convexUrl ? await fetchPublisherOgMeta(handle, convexUrl) : null;
const handleLabel = `@${meta?.handle || handle}`;
const title = titleFromQuery || meta?.displayName || handleLabel;
Expand All @@ -70,8 +70,8 @@ export default defineEventHandler(async (event) => {
handleLabel,
stats: [
{
value: downloadsFromQuery || formatOgStat(meta?.stats.downloads),
label: "Downloads",
value: installsFromQuery || formatOgStat(meta?.stats.installs),
label: "Installs",
},
],
});
Expand Down
Loading
Loading