All files hostParity.ts

100% Statements 34/34
92% Branches 23/25
100% Functions 6/6
100% Lines 34/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47      1x     1x 1x 1x   1x 6x 6x 6x 6x 6x 6x 5x 6x 6x 6x 1x 1x   1x 3x 3x     1x 6x 6x 6x 2x 2x     1x 5x 5x 5x   1x 20x 20x 20x  
// SPDX-License-Identifier: Apache-2.0
// Why: the TypeScript copy of the host rules shared across languages (bearer token hash
// and admit, Lambda readiness, debit delta, neighbor limit), checked against one vector.
import { createHash } from "node:crypto";
 
/** r[impl integrator.mcp.auth.http_bearer_pat] */
export function tokenSk(secret: string): string {
  return `sha256:${createHash("sha256").update(secret).digest("hex")}`;
}
 
export function admitToken(
  header: string | null,
  status: string | null,
  expiresAt: string | null,
  now: string,
): boolean {
  if (!header) return false;
  const parts = header.trim().split(/\s+/);
  if (parts.length < 2 || parts[0].toLowerCase() !== "bearer" || !parts[1]) return false;
  if (status !== "active") return false;
  if (expiresAt && now >= expiresAt) return false;
  return true;
}
 
export function lambdaReady(tableEnv: string, isLambda: boolean): boolean {
  return !isLambda || tableEnv.trim().length > 0;
}
 
/** r[impl integrator.mcp.auth.quota_via_token_account+3] */
export function debitDelta(tool: string, tier: string, quotable: boolean): number {
  if (tier.toLowerCase() === "patron") return 0;
  if (tool === "search") return 1;
  if (tool === "fetch" && quotable) return 1;
  return 0;
}
 
/** r[impl integrator.mcp.reconstruction.run_cap+1] */
export function neighborAllow(tier: string, chainLen: number): boolean {
  const cap = tier.toLowerCase() === "free" ? 3 : 5;
  return chainLen <= cap;
}
 
export function fetchArgumentsOk(value: Record<string, unknown>): boolean {
  const keys = Object.keys(value);
  return keys.length === 1 && typeof value.isa === "string" && value.isa.length > 0;
}