skip to content

test strategy for a collection product · lesson 4 of 7 · sources checked 2026-11-30

contract tests of the upstream shape

This lesson records the upstream shape of a flow, stores it with a version, and tests each new recording against it before a release.

purpose

A site can change its interface with no notice and with no block. The extractor then gives empty or wrong records, and the team can read the fault as a detection event. A stored upstream shape lets you see the change of the interface before a release, and give it a class of its own.

objectives

  1. List the four parts of the upstream shape of a flow.
  2. Record the shape of a flow from a recording, and store it with a version.
  3. Classify each difference between a new recording and the stored shape.
  4. Decide which change of shape breaks the data contract of the collector.
  5. Explain why a store of three recordings gives fewer false key differences than one recording.

new terms

upstream shape
The endpoints, methods, payload key paths, and step order of one flow of a target.

how it works

The upstream shape of a flow has four parts:

  1. the endpoints that the flow calls;
  2. the method of each call, for example GET or POST;
  3. the key paths of each payload, for example items[].price.amount;
  4. the order of the steps.

The shape holds no values. A new price is not a change of shape. A new key items[].price.currency is a change of shape.

record the shape from a recording

The collector records its own flows on a lab page or on a target that gave permission. The recording is the same one that the collector makes for its own record. The contract test sends no extra request, and it does not test a protection of the site.

A recorder reads the recording and writes the four parts of the shape to a file. The file gets a version and a date, for example search-flow v3. A person reviews the first version before it goes into the store.

the contract test in each build

An upstream contract test is one test layer of the plan. In each build, it does two checks:

  • The extractor reads a synthetic payload of each stored shape. It must give a record that agrees with the data contract of the collector.
  • A comparison checks each new recording against the stored shape of its flow.

The first check finds a change of the own code. The second check finds a change of the site. For example, a new extractor that reads items[].cost fails the first check, and a site that removes items[].price.amount fails the second.

the class of each difference

Each difference gets one class:

classexample
New keyitems[].badge occurs, and the stored shape does not have it.
Removed keyitems[].price.amount does not occur.
New endpointThe flow calls /api/v2/search in place of /api/v1/search.
Removed endpointThe flow no longer calls /api/v1/facets.
Step orderThe list call now comes before the session call.

These classes are classes of shape. They are never a detection class. A block is a refusal by the target. A removed key is a change of the interface, and the team does not report it as a detection event.

which change breaks the contract

The data contract names the fields that each reader of the collector gets. Each field maps to one key path of the shape. Thus a removed key path that a contract field uses is a break. The release stops until the extractor gets a new map.

A new key that no contract field uses is information. For example, a new items[].badge changes no record. The team adds the key to the stored shape with a new version.

optional keys and false differences

Some keys occur only in some responses. For example, items[].discount occurs only when a product has a discount. A stored shape from one recording does not know this key.

Thus each response with a discount shows a new key, and each response without it shows a removed key. Each such difference is a false difference, because the shape did not change.

A store of three recordings of the same flow gives the union of their keys. A key that occurs in one recording and not in another gets the label "optional". The comparison does not report an optional key as a new key or a removed key.

The monitor of the same shape in production is in Silent failure and the change below the page. The contract test gives the check before a release.

the two sides

automation sidedetection side
A contract test separates a change of the target interface from a block. The team does not report a target change as a detection event.A target that changes its interface can break extractors with no block. The contract test sends no extra request, so the defender sees no new traffic.

design decisions

optiongaincostwhen it is correct
A shape from one recordingSimple and smallEach optional key gives false differences.Only for a flow with no optional keys
A shape from the union of three recordingsFewer false key differencesA key that left the site stays in the shape until the next review of the shape.For each flow with list or product payloads
A class of its own for a change of shapeThe team does not read an interface change as a block.One more class in the reportsAlways
Stop the release on a removed key that a contract field usesNo reader gets an empty field.A release waits for the new map.Always
Stop the release on each new keyNo change goes unseen.Many releases stop for keys that no reader uses.Never. Add the key to the shape with a new version.

diagram

the upstream contract test: record, extract the shape, store with a version, compare, and classify

A change of shape gets a class of its own, and only a removed contract key stops the release.source: My model of an upstream contract test

code example

The example holds a synthetic stored shape and a synthetic HAR recording of a lab flow. It reads the shape of the recording and writes the class of each difference.

m54-x04-shape.tstypescript

Extract the endpoints, methods, payload key paths, and step order from a HAR recording, and compare them with the stored shape.

type Entry = { request: { method: string; url: string }; response: { content?: { text?: string } } };
type Shape = { steps: string[]; keys: Record<string, string[]>; optional?: string[] };

function keyPaths(value: unknown, prefix = "", paths = new Set<string>()): Set<string> {
  if (Array.isArray(value)) for (const child of value) keyPaths(child, `${prefix}[]`, paths);
  else if (value !== null && typeof value === "object") {
    for (const [key, child] of Object.entries(value)) {
      const path = prefix ? `${prefix}.${key}` : key;
      paths.add(path);
      keyPaths(child, path, paths);
    }
  }
  return paths;
}
function shapeOf(entries: Entry[]): Shape {
  const steps: string[] = [];
  const keys: Record<string, string[]> = {};
  for (const entry of entries) {
    // Keep the method and the path only: no host, no query, no header.
    const url = entry.request.url;
    const step = `${entry.request.method} ${url.slice(url.indexOf("/")).split("?")[0]}`;
    steps.push(step);
    try {
      keys[step] = [...keyPaths(JSON.parse(entry.response.content?.text ?? ""))].sort();
    } catch {
      keys[step] = [];
    }
  }
  return { steps, keys };
}

function compare(stored: Shape, next: Shape): string[] {
  const out: string[] = [];
  for (const s of next.steps) if (!stored.steps.includes(s)) out.push(`new endpoint: ${s}`);
  for (const s of stored.steps) if (!next.steps.includes(s)) out.push(`removed endpoint: ${s}`);
  const common = next.steps.filter((s) => stored.steps.includes(s));
  const oldOrder = stored.steps.filter((s) => next.steps.includes(s));
  if (common.join("\n") !== oldOrder.join("\n")) out.push(`step order: ${next.steps.join(" > ")}`);
  const optional = new Set(stored.optional ?? []);
  for (const step of common) {
    const oldKeys = new Set(stored.keys[step]), newKeys = new Set(next.keys[step]);
    for (const k of [...newKeys].sort()) if (!oldKeys.has(k) && !optional.has(k)) out.push(`new key: ${step} ${k}`);
    for (const k of [...oldKeys].sort()) if (!newKeys.has(k) && !optional.has(k)) out.push(`removed key: ${step} ${k}`);
  }
  return out;
}
// Synthetic input: the stored shape "search-flow v3" and a new HAR recording of a lab flow.
const stored: Shape = {
  steps: ["GET /api/v1/session", "GET /api/v1/search"],
  keys: { "GET /api/v1/session": ["ready"], "GET /api/v1/search": ["items", "items[].price", "items[].price.amount", "items[].title"] },
  optional: ["items[].discount"],
};
const entry = (path: string, body: unknown): Entry =>
  ({ request: { method: "GET", url: `lab.example${path}` }, response: { content: { text: JSON.stringify(body) } } });
const har = { log: { entries: [
  entry("/api/v1/search?q=lamp", { items: [{ title: "Lamp", price: { value: "19.90" }, badge: "new" }, { title: "Desk", price: { value: "89.00" }, discount: 10 }] }),
  entry("/api/v1/session", { ready: true }),
  entry("/api/v1/facets", { facets: [] }),
] } };
for (const line of compare(stored, shapeOf(har.log.entries))) console.log(line);

output

One line for each difference, with its class.

new endpoint: GET /api/v1/facets
step order: GET /api/v1/search > GET /api/v1/session > GET /api/v1/facets
new key: GET /api/v1/search items[].badge
new key: GET /api/v1/search items[].price.value
removed key: GET /api/v1/search items[].price.amount

The key items[].discount is optional in the stored shape, so the output does not report it. If a contract field uses the removed key items[].price.amount, the release stops.

data

contract differences on synthetic lab flows, by change class and detector

status of this figure: simulated

contract differences on synthetic lab flows, by change class and detectorone recordingunion of 3 recordings14053201410316unit: flow-weeks1. new key2. removed key3. new endpoint4. step order
  1. new key: one recording 1405; union of 3 recordings 320
  2. removed key: one recording 1410; union of 3 recordings 316
  3. new endpoint: one recording 94; union of 3 recordings 94
  4. step order: one recording 99; union of 3 recordings 99

source: Lab result of this course: contract differences on synthetic recordings of lab flows; date 2026-11-13; sample of 2,400; condition: Synthetic recordings from a flow model, not a recording of lab pages. No browser noise. One host. The false differences come only from the optional keys. Lab teaching values.

data table
contract differences on synthetic lab flows, by change class and detector
seriesdetectorflow-weeks
new keyone recording1405
new keyunion of 3 recordings320
removed keyone recording1410
removed keyunion of 3 recordings316
new endpointone recording94
new endpointunion of 3 recordings94
step orderone recording99
step orderunion of 3 recordings99
Flows with a detected difference over 12 lab weeks, by change class. Optional keys cause the false differences. The union of 3 recordings flags far fewer key differences than one recording. The files are synthetic.

The chart is a lab result of this course, on synthetic recordings with no browser noise. Most key differences of the single recording are false differences from optional keys. The endpoint and step-order differences are the same for the two stores.

The chart leaves out the class "removed endpoint", because no store found one. The chart shows the differences that the comparison found, not the differences that matter to a reader.

failure modes

problemcausecorrection
A change of the interface shows as a detection event.The report has no class for a change of shape.Give each difference a class of shape, separate from the detection classes.
Each week brings many key differences, and the team stops reading them.The stored shape comes from one recording, and optional keys give false differences.Store the union of three recordings, and label the optional keys.
Readers get an empty field after a release.A removed key path of a contract field did not stop the release.Map each contract field to its key path, and stop the release on a removed path.
A new price looks like a change of shape.The recorder stores values, not key paths.Store only the endpoints, the methods, the key paths, and the step order.
A stored shape holds a host name or a token.The recorder copies the full URL and the headers.Keep the method and the path only, and scrub each recording.

check questions

  1. List the four parts of the upstream shape of a flow.

    sample answer

    The endpoints that the flow calls, the method of each call, the key paths of each payload, and the order of the steps.

  2. Which item goes into a stored upstream shape?

    1. The price value of each product
    2. The full URL with its query
    3. The key path items[].price.amount
    4. The cookie of the web session
    answer

    c. The key path items[].price.amount

    The shape holds the endpoints, the methods, the key paths, and the step order. It holds no values, no host, no query, and no header.

  3. A new recording calls a search path that the stored shape does not have. Which class does the difference get?

    1. New endpoint
    2. Block
    3. New key
    answer

    a. New endpoint

    A path that the stored shape does not have is a new endpoint. It is a class of shape, never a detection class.

  4. Which difference stops the release?

    1. A new key that no contract field uses
    2. A removed key path that a contract field uses
    3. A new price value
    4. A new key of an optional field
    answer

    b. A removed key path that a contract field uses

    Each contract field maps to one key path. When that path does not occur, the readers get an empty field, so the release stops until the extractor gets a new map.

  5. Explain why a stored shape from three recordings gives fewer false key differences than a shape from one recording.

    sample answer

    Some keys occur only in some responses. One recording does not know these keys, so each response with or without them gives a difference. The union of three recordings labels such keys as optional, and the comparison does not report them.

exercise

exercise Do this task on paper. The input is a synthetic search flow:

  • Contract field name: from items[].title.
  • Contract field price: from items[].price.amount.
  • Stored shape search-flow v3: the steps GET /api/v1/session, then GET /api/v1/search.

A new recording gives these differences:

  • items[].badge occurs in the search step, and the stored shape does not have it.
  • items[].price.amount does not occur, and items[].price.value occurs.
  • The search step is now GET /api/v2/search.
  1. Give each difference its class.
  2. For each difference, write if it breaks the data contract.
  3. Write if the release can go on.
sample answer

The answer uses the two contract fields and the three differences of the task.

The key items[].badge is a new key. No contract field uses it, so it breaks nothing, and the shape gets a new version. The change from amount to value gives a removed key and a new key. The field price uses the removed path, so it is a break.

The new search path gives a new endpoint and a removed endpoint. The extractor must read the new endpoint. The release stops until the extractor maps price to items[].price.value and reads /api/v2/search. None of the differences is a block, and none goes into the detection report.

sources

  1. My model of an upstream contract test, its classes of difference, and the union of recordings, 2026-11 (not public).