Cotton code
48 lines · 3.8 KB · TypeScriptRaw
/** * RULE (live, 21 Nov 2026): a connector at the public door * (https://cottondb.com/mcp, the database found from the sign-in) with the * grant on Worker-wide is Your Claude: its first private read answers the * account card, filed as it connected and standing until answered, never the * timed window's words. */import { SELF } from "cloudflare:test";import { expect, it } from "vitest";const OPEN = "https://cottondb.com";const REDIRECT = "https://claude.ai/api/mcp/auth_callback";const post = (url: string, body: Record<string, string>) => SELF.fetch(url, { method: "POST", redirect: "manual", headers: { "content-type": "application/x-www-form-urlencoded", "cf-connecting-ip": "203.0.113.200" }, body: new URLSearchParams(body).toString() });const b64 = (b: Uint8Array) => btoa(String.fromCharCode(...b)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");function fields(html: string): Record<string, string> {  const out: Record<string, string> = {};  for (const m of html.matchAll(/<input type="hidden" name="([^"]+)" value="([^"]*)">/g)) out[m[1]!] = m[2]!.replace(/&quot;/g, '"').replace(/&amp;/g, "&");  return out;}it("a connection at the public door gets the account card, not the window words", async () => {  const reg = await SELF.fetch(`${OPEN}/oauth/register`, { method: "POST", headers: { "content-type": "application/json", "cf-connecting-ip": "203.0.113.200" }, body: JSON.stringify({ client_name: "Claude", redirect_uris: [REDIRECT], grant_types: ["authorization_code", "refresh_token"], response_types: ["code"], token_endpoint_auth_method: "none" }) });  const clientId = ((await reg.json()) as { client_id: string }).client_id;  const v = b64(crypto.getRandomValues(new Uint8Array(40)));  const ch = b64(new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(v))));  const q = new URLSearchParams({ client_id: clientId, redirect_uri: REDIRECT, response_type: "code", code_challenge: ch, code_challenge_method: "S256", state: "s", scope: "owner", resource: `${OPEN}/mcp` });  const start = await SELF.fetch(`${OPEN}/oauth/authorize?${q}`, { redirect: "manual", headers: { "cf-connecting-ip": "203.0.113.200" } });  const sent = await post(`${OPEN}/oauth/authorize`, { ...fields(await start.text()), phone: "+919000799999" });  const done = await post(`${OPEN}/oauth/authorize`, { ...fields(await sent.text()), code: "123456" });  const code = new URL(done.headers.get("location")!).searchParams.get("code")!;  const tok = (await (await post(`${OPEN}/oauth/token`, { grant_type: "authorization_code", client_id: clientId, code, code_verifier: v, redirect_uri: REDIRECT, resource: `${OPEN}/mcp` })).json()) as { access_token: string };  const call = async (name: string, args: Record<string, unknown>) => {    const res = await SELF.fetch(`${OPEN}/mcp`, { method: "POST", headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${tok.access_token}` }, body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name, arguments: args } }) });    return { status: res.status, text: await res.text() };  };  let first = "";  for (const table of ["note", "book"]) {    const r = await call("list_records", { table });    expect(r.status).toBe(200);    const result = (JSON.parse(r.text) as { result: { content: { text: string }[]; structuredContent: Record<string, unknown> } }).result;    expect(result.structuredContent).toMatchObject({ code: "window_needed", card: "account", expires_at: "9999-12-31T23:59:59.999Z" });    expect(result.content[0]!.text).toContain("Let your Claude read your data");    expect(result.content[0]!.text).not.toContain("has not opened a window");    // One card for the connection, filed at connect.    first ||= String(result.structuredContent.request_id);    expect(result.structuredContent.request_id).toBe(first);  }});