Files
openwork/services/openwork-share/api/b/render-bundle-page.test.js
ben 28d1f4206c feat(app,share): deep-link bundle shares into new-worker imports (#664)
* docs(share): add free-first and org hub sharing redesign flows

* feat(app,share): open bundle links into new-worker imports
2026-02-24 19:01:04 -08:00

93 lines
3.4 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import { buildBundleUrls, renderBundlePage, wantsDownload, wantsJsonResponse } from "./render-bundle-page.js";
function makeReq({ accept = "", query = {}, host = "share.openwork.software" } = {}) {
return {
query,
headers: {
accept,
host,
"x-forwarded-proto": "https",
"x-forwarded-host": host,
},
};
}
test("wantsJsonResponse honors explicit format query", () => {
assert.equal(wantsJsonResponse(makeReq({ query: { format: "json" }, accept: "text/html" })), true);
assert.equal(wantsJsonResponse(makeReq({ query: { format: "html" }, accept: "application/json" })), false);
});
test("wantsJsonResponse defaults to json unless browser html accept is present", () => {
assert.equal(wantsJsonResponse(makeReq()), true);
assert.equal(wantsJsonResponse(makeReq({ accept: "application/json" })), true);
assert.equal(wantsJsonResponse(makeReq({ accept: "text/html,application/xhtml+xml" })), false);
});
test("wantsDownload only enables on download=1", () => {
assert.equal(wantsDownload(makeReq({ query: { download: "1" } })), true);
assert.equal(wantsDownload(makeReq({ query: { download: "0" } })), false);
assert.equal(wantsDownload(makeReq()), false);
});
test("buildBundleUrls uses forwarded origin", () => {
const urls = buildBundleUrls(makeReq({ host: "example.test" }), "01ABC");
assert.equal(urls.shareUrl, "https://example.test/b/01ABC");
assert.equal(urls.jsonUrl, "https://example.test/b/01ABC?format=json");
assert.equal(urls.downloadUrl, "https://example.test/b/01ABC?format=json&download=1");
});
test("renderBundlePage includes machine-readable metadata and escaped json script", () => {
const rawJson = JSON.stringify({
schemaVersion: 1,
type: "skill",
name: "demo </script> skill",
description: "Install me",
trigger: "daily",
content: "# Skill\nHello",
});
const html = renderBundlePage({
id: "01TEST",
rawJson,
req: makeReq({ accept: "text/html", host: "share.openwork.software" }),
});
assert.match(html, /data-openwork-share="true"/);
assert.match(html, /data-openwork-bundle-type="skill"/);
assert.match(html, /meta name="openwork:bundle-id" content="01TEST"/);
assert.match(html, /\?format=json/);
assert.match(html, /openwork:\/\/import-bundle\?/);
assert.match(html, /ow_bundle=https%3A%2F%2Fshare\.openwork\.software%2Fb%2F01TEST/);
assert.match(html, /ow_intent=new_worker/);
assert.match(html, /ow_source=share_service/);
assert.match(html, /id="openwork-bundle-json" type="application\/json"/);
assert.match(html, /demo \\u003c\/script\\u003e skill/);
});
test("renderBundlePage shows workspace profile metadata", () => {
const rawJson = JSON.stringify({
schemaVersion: 1,
type: "workspace-profile",
name: "Team Workspace",
workspace: {
opencode: { model: "gpt-5.3" },
openwork: { reload: { auto: true } },
skills: [{ name: "workspace-guide", content: "..." }, { name: "skill-creator", content: "..." }],
commands: [{ name: "standup", template: "..." }],
},
});
const html = renderBundlePage({
id: "01WORKSPACE",
rawJson,
req: makeReq({ accept: "text/html", host: "share.openwork.software" }),
});
assert.match(html, /<dt>Skills<\/dt><dd>2<\/dd>/);
assert.match(html, /<dt>Commands<\/dt><dd>1<\/dd>/);
assert.match(html, /<dt>Config<\/dt><dd>yes<\/dd>/);
});