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
6 changes: 5 additions & 1 deletion site/src/lib/embeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ export const badge = (live: SiteEntry[]) => ({
});

// shortcut: widths are estimated at 6.6px a character, not measured; fine for short ASCII labels.
export function flatBadge(label: string, message: string, color: string): string {
export function flatBadge(
label: string,
message: string,
color: string,
): string {
const lw = Math.round(label.length * 6.6 + 10);
const rw = Math.round(message.length * 6.6 + 10);
const w = lw + rw;
Expand Down
3 changes: 2 additions & 1 deletion site/src/pages/scorecard.svg.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { flatBadge } from "../lib/embeds.ts";

const URL_ = "https://api.scorecard.dev/projects/github.com/devstreamhq/devstream";
const URL_ =
"https://api.scorecard.dev/projects/github.com/devstreamhq/devstream";

export async function GET() {
let msg = "pending";
Expand Down
18 changes: 13 additions & 5 deletions site/tests/badges.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { GET as scorecard } from "../src/pages/scorecard.svg.ts";

const realFetch = globalThis.fetch;
const mock = (fn) => (globalThis.fetch = fn);
const json = (body, ok = true) => async () => ({ ok, json: async () => body });
const json =
(body, ok = true) =>
async () => ({ ok, json: async () => body });
const text = async (r) => r.text();
const checks = (svg) => {
assert.ok(!/<script/i.test(svg), "no script");
Expand Down Expand Up @@ -35,7 +37,9 @@ test("scorecard.svg renders the numeric score", async () => {

test("scorecard.svg says pending on throw or garbage", async () => {
for (const f of [
async () => { throw new Error("down"); },
async () => {
throw new Error("down");
},
json({ score: "9" }),
json({ score: NaN }),
json(null),
Expand All @@ -50,9 +54,13 @@ test("scorecard.svg says pending on throw or garbage", async () => {
});

test("both SVGs escape their input", () => {
const svg = flatBadge('<b>"', '<script>&', '"><x');
assert.ok(!svg.includes("<script>") && !svg.includes("<b>") && !svg.includes('"><x'));
const svg = flatBadge('<b>"', "<script>&", '"><x');
assert.ok(
!svg.includes("<script>") && !svg.includes("<b>") && !svg.includes('"><x'),
);
assert.ok(svg.includes("&lt;script&gt;&amp;"));
});

test.after(() => { globalThis.fetch = realFetch; });
test.after(() => {
globalThis.fetch = realFetch;
});
Loading