testFetchesAreCappedAndOnlyWhatsAsked
A fetch capped by what its namespaces name stops before fetching any code, and a server sending namespaces it wasn't asked for is refused.
import { Codebase } from "#codebase/tcmts/cli/codebase"; import { fetchNamespaces } from "#codebase/tcmts/cli/sync"; import type { Remote } from "#codebase/tcmts/cli/sync"; import { tempDir } from "#codebase/tcmts/cli/test/e2e/harness"; import assert from "node:assert/strict"; export const testFetchesAreCappedAndOnlyWhatsAsked = async () => { const session = process.env.TCM_BRANCH; delete process.env.TCM_BRANCH; try { const cb = Codebase.init(tempDir("capped fetch")); const body = JSON.stringify([ "ns", [ ["a", ["h1"]], ["b", ["h2"]], ["c", ["h3"]], ], [], ]); let fetchedCode = false; const remote = { label: "a server", getNamespaces: async (hs: string[]) => hs.map((hash) => ({ hash, body })), get: async () => { fetchedCode = true; return []; }, } as unknown as Remote; await assert.rejects( fetchNamespaces(cb, remote, ["root"], { maxNames: 2 }), /names more than 2 definitions/, ); assert.equal(fetchedCode, false); const liar = { label: "a liar", getNamespaces: async () => [{ hash: "another", body }], } as unknown as Remote; await assert.rejects( fetchNamespaces(cb, liar, ["root"]), /sent namespaces it wasn't asked for/, ); } finally { if (session !== undefined) process.env.TCM_BRANCH = session; } }