All files / src matchers.ts

100% Statements 105/105
100% Branches 53/53
100% Functions 17/17
100% Lines 80/80

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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229                                                        53x 53x 22146x 53x         93x 22192x 93x         13x 12x 12x 3x   9x 18x 1x     8x         15x 14x     14x 14x   1x   13x 13x   1x   12x                                 3x 2x 2x 2x 2x 1x       4x   14x 14x 4x         4x                       12x 11x 11x 11x     11x 11x   1x   10x 10x   1x   9x 2x                         10x                   7x               17x 13x 13x 13x     7x       10x                       23x           21x 15x 14x 13x 11x 4x 4x 1x   7x 6x 6x 6x 6x 5x 6x 5x   3x       18x 40x 9x 9x 9x   9x   31x      
import type { BodyMatcher } from '@ethercalc/shared/oracle-scenarios';
 
import { canonicalizeHtml } from './html-canonical.ts';
import {
  compareZipArchives,
  VOLATILE_ODS_META,
  VOLATILE_XLSX_DOCPROPS,
} from './zip-canonical.ts';
 
/**
 * Body matchers used by `replay.ts`. Each matcher takes the expected
 * bytes (as base64) and the actual response body (as Uint8Array) and
 * returns `null` on success or a short explanation on failure.
 *
 * Phase 3 implements `exact`, `json`, `ignore`, and `scsave`. Phase 8a
 * adds `html` via linkedom plus `xlsx`/`ods` via fflate unzip + the
 * same linkedom XML canonicalizer. See §4.4 of CLAUDE.md and the drop
 * lists documented in `html-canonical.ts` and `zip-canonical.ts`.
 */
export type MatcherResult = string | null;
 
export interface MatcherContext {
  readonly expectedBase64: string | null;
  readonly actualBytes: Uint8Array;
}
 
/** Base64-decode into a Uint8Array (cross-runtime: browser + Node + workerd). */
export function decodeBase64(b64: string): Uint8Array {
  const bin = atob(b64);
  const arr = new Uint8Array(bin.length);
  for (let i = 0; i < bin.length; i++) arr[i] = bin.charCodeAt(i);
  return arr;
}
 
/** Base64-encode a Uint8Array. */
export function encodeBase64(bytes: Uint8Array): string {
  let s = '';
  for (let i = 0; i < bytes.length; i++) s += String.fromCharCode(bytes[i]!);
  return btoa(s);
}
 
/** Byte-for-byte comparison. */
export function matchExact(ctx: MatcherContext): MatcherResult {
  if (ctx.expectedBase64 === null) return 'expected body is null but matcher is "exact"';
  const expected = decodeBase64(ctx.expectedBase64);
  if (expected.length !== ctx.actualBytes.length) {
    return `body length differs: expected ${expected.length} bytes, got ${ctx.actualBytes.length}`;
  }
  for (let i = 0; i < expected.length; i++) {
    if (expected[i] !== ctx.actualBytes[i]) {
      return `body differs at byte ${i}: expected ${expected[i]}, got ${ctx.actualBytes[i]}`;
    }
  }
  return null;
}
 
/** Parse both sides as JSON and deep-compare the resulting values. */
export function matchJson(ctx: MatcherContext): MatcherResult {
  if (ctx.expectedBase64 === null) return 'expected body is null but matcher is "json"';
  const dec = new TextDecoder();
  let expectedValue: unknown;
  let actualValue: unknown;
  try {
    expectedValue = JSON.parse(dec.decode(decodeBase64(ctx.expectedBase64)));
  } catch (err) {
    return `expected body is not valid JSON: ${(err as Error).message}`;
  }
  try {
    actualValue = JSON.parse(dec.decode(ctx.actualBytes));
  } catch (err) {
    return `actual body is not valid JSON: ${(err as Error).message}`;
  }
  return deepEqual(expectedValue, actualValue)
    ? null
    : `json mismatch: expected ${stableStringify(expectedValue)}, got ${stableStringify(actualValue)}`;
}
 
/**
 * SocialCalc save format comparison.
 *
 * The legacy server always embeds the current `socialcalc` library
 * version into a `version:N.N.N` line near the top; we ignore that
 * exact line so oracle upgrades don't invalidate every recording.
 * Everything else (sheet, cell, edit, copiedfrom, etc.) is compared
 * line-for-line after trimming trailing whitespace. Section ordering
 * is also loose — we sort the non-version lines so minor reordering
 * in metadata doesn't trip the diff (§4.4).
 */
export function matchScsave(ctx: MatcherContext): MatcherResult {
  if (ctx.expectedBase64 === null) return 'expected body is null but matcher is "scsave"';
  const dec = new TextDecoder();
  const expected = normalizeScsave(dec.decode(decodeBase64(ctx.expectedBase64)));
  const actual = normalizeScsave(dec.decode(ctx.actualBytes));
  if (expected === actual) return null;
  return `scsave mismatch:\n--- expected\n${expected}\n--- actual\n${actual}`;
}
 
function normalizeScsave(raw: string): string {
  const lines = raw
    .split(/\r?\n/)
    .map((line) => line.trimEnd())
    .filter((line) => !/^version:/i.test(line));
  return lines.sort().join('\n');
}
 
/** Ignore body entirely. Always passes. */
export function matchIgnore(_ctx: MatcherContext): MatcherResult {
  return null;
}
 
/**
 * Structural HTML matcher. Parses both sides with `linkedom`,
 * canonicalizes (drop whitespace-only text nodes, sort attributes,
 * strip volatile ids + dangling references, drop comments), and
 * byte-compares the serialized result.
 *
 * See `html-canonical.ts` for the complete rule set.
 */
export function matchHtml(ctx: MatcherContext): MatcherResult {
  if (ctx.expectedBase64 === null) return 'expected body is null but matcher is "html"';
  const dec = new TextDecoder();
  const expectedRaw = dec.decode(decodeBase64(ctx.expectedBase64));
  const actualRaw = dec.decode(ctx.actualBytes);
  let expected: string;
  let actual: string;
  try {
    expected = canonicalizeHtml(expectedRaw).canonical;
  } catch (err) {
    return `html parse error in expected body: ${(err as Error).message}`;
  }
  try {
    actual = canonicalizeHtml(actualRaw).canonical;
  } catch (err) {
    return `html parse error in actual body: ${(err as Error).message}`;
  }
  if (expected === actual) return null;
  return `html mismatch:\n--- expected\n${expected}\n--- actual\n${actual}`;
}
 
/**
 * Structural XLSX matcher. Unzips both archives, sorts entries,
 * canonicalizes each XML entry (via the same DOM walker used by the
 * HTML matcher), drops volatile elements in `docProps/core.xml` and
 * `docProps/app.xml`, and byte-compares the stable representation.
 * Binary entries (images) are hex-compared.
 *
 * See `zip-canonical.ts` for the complete drop list.
 */
export function matchXlsx(ctx: MatcherContext): MatcherResult {
  return compareZipBodies(ctx, 'xlsx', VOLATILE_XLSX_DOCPROPS);
}
 
/**
 * Structural ODS matcher. Same pipeline as xlsx but with a different
 * volatile-element map — ODS metadata lives in `meta.xml`.
 *
 * See `zip-canonical.ts` for the complete drop list.
 */
export function matchOds(ctx: MatcherContext): MatcherResult {
  return compareZipBodies(ctx, 'ods', VOLATILE_ODS_META);
}
 
function compareZipBodies(
  ctx: MatcherContext,
  label: 'xlsx' | 'ods',
  volatile: Readonly<Record<string, readonly string[]>>,
): MatcherResult {
  if (ctx.expectedBase64 === null) return `expected body is null but matcher is "${label}"`;
  const expected = decodeBase64(ctx.expectedBase64);
  const result = compareZipArchives(expected, ctx.actualBytes, volatile);
  if (result.equal) return null;
  // `compareZipArchives` always populates `diff` when `equal: false`,
  // but TS can't prove it without a union type — hence the cast.
  return `${label} mismatch: ${result.diff!}`;
}
 
/** Table of matcher functions keyed by `BodyMatcher` name. */
export const MATCHERS: Readonly<Record<BodyMatcher, (ctx: MatcherContext) => MatcherResult>> = {
  exact: matchExact,
  json: matchJson,
  scsave: matchScsave,
  ignore: matchIgnore,
  html: matchHtml,
  xlsx: matchXlsx,
  ods: matchOds,
};
 
/** Dispatch a body comparison to the right matcher. */
export function dispatchMatcher(matcher: BodyMatcher, ctx: MatcherContext): MatcherResult {
  return MATCHERS[matcher](ctx);
}
 
// ─── internal helpers ─────────────────────────────────────────────────────
 
function deepEqual(a: unknown, b: unknown): boolean {
  if (a === b) return true;
  if (a === null || b === null) return false;
  if (typeof a !== typeof b) return false;
  if (typeof a !== 'object') return false;
  if (Array.isArray(a)) {
    if (!Array.isArray(b) || a.length !== b.length) return false;
    for (let i = 0; i < a.length; i++) if (!deepEqual(a[i], b[i])) return false;
    return true;
  }
  if (Array.isArray(b)) return false;
  const ao = a as Record<string, unknown>;
  const bo = b as Record<string, unknown>;
  const keys = Object.keys(ao);
  if (keys.length !== Object.keys(bo).length) return false;
  for (const k of keys) {
    if (!Object.prototype.hasOwnProperty.call(bo, k)) return false;
    if (!deepEqual(ao[k], bo[k])) return false;
  }
  return true;
}
 
function stableStringify(value: unknown): string {
  return JSON.stringify(value, (_, v: unknown) => {
    if (v && typeof v === 'object' && !Array.isArray(v)) {
      const sorted: Record<string, unknown> = {};
      for (const k of Object.keys(v as Record<string, unknown>).sort()) {
        sorted[k] = (v as Record<string, unknown>)[k];
      }
      return sorted;
    }
    return v;
  });
}