|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// #7256 — the `contains` assertion used to SILENTLY PASS when the actual value was |
| 4 | +// neither an array nor a string. The `case 'contains':` block handled the two |
| 5 | +// evaluable shapes and had no `else`, so `undefined` (a typo'd `field` path, or a |
| 6 | +// response shape that moved), `null`, a number or an object fell out of the switch |
| 7 | +// throwing nothing, and the scenario reported ✅. A suite asserting `contains` |
| 8 | +// against a field the result does not have was asserting NOTHING, and CI believed it. |
| 9 | +// |
| 10 | +// These pin both halves of the fix: the three non-evaluable shapes now fail LOUD with |
| 11 | +// a message that names the runtime type and says which of the fixture or the assertion |
| 12 | +// is the suspect, and the two evaluable shapes keep their pre-existing behaviour in |
| 13 | +// BOTH directions (a match still passes, a miss still fails). |
| 14 | + |
| 15 | +import { describe, it, expect } from 'vitest'; |
| 16 | +import * as QA from '@objectstack/spec/qa'; |
| 17 | +import { TestRunner } from './runner.js'; |
| 18 | +import type { TestExecutionAdapter } from './adapter.js'; |
| 19 | + |
| 20 | +/** An adapter that hands the runner one fixed result — the assertion is the unit under test. */ |
| 21 | +class StubAdapter implements TestExecutionAdapter { |
| 22 | + constructor(private result: unknown) {} |
| 23 | + async execute(): Promise<unknown> { |
| 24 | + return this.result; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +/** Run one assertion against one canned adapter result, through the public runner surface. */ |
| 29 | +async function runAssertion( |
| 30 | + result: unknown, |
| 31 | + assertion: QA.TestAssertion, |
| 32 | +): Promise<{ passed: boolean; error: string }> { |
| 33 | + const runner = new TestRunner(new StubAdapter(result)); |
| 34 | + const [outcome] = await runner.runSuite({ |
| 35 | + name: 'contains-pins', |
| 36 | + scenarios: [ |
| 37 | + { |
| 38 | + id: 'scenario-1', |
| 39 | + name: 'single assertion', |
| 40 | + steps: [ |
| 41 | + { |
| 42 | + name: 'step-1', |
| 43 | + action: { type: 'api_call', target: '/api/v1/accounts' }, |
| 44 | + assertions: [assertion], |
| 45 | + }, |
| 46 | + ], |
| 47 | + }, |
| 48 | + ], |
| 49 | + }); |
| 50 | + const error = outcome.error; |
| 51 | + return { |
| 52 | + passed: outcome.passed, |
| 53 | + error: error instanceof Error ? error.message : String(error ?? ''), |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +const containsAcme: QA.TestAssertion = { |
| 58 | + field: 'body.data.items', |
| 59 | + operator: 'contains', |
| 60 | + expectedValue: 'acme', |
| 61 | +}; |
| 62 | + |
| 63 | +describe("TestRunner — `contains` against a non-array/non-string actual fails loud (#7256)", () => { |
| 64 | + it('fails when the field path resolves to nothing (the filed case: a missing path)', async () => { |
| 65 | + const { passed, error } = await runAssertion({ body: { data: {} } }, containsAcme); |
| 66 | + |
| 67 | + expect(passed).toBe(false); |
| 68 | + // Names the field, the operator and the runtime type... |
| 69 | + expect(error).toContain('body.data.items'); |
| 70 | + expect(error).toContain("'contains'"); |
| 71 | + expect(error).toContain('got undefined'); |
| 72 | + // ...and points at the FIXTURE, because the path is what did not resolve. |
| 73 | + expect(error).toContain('absent from the result'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('fails when the whole response shape is missing, not just the leaf', async () => { |
| 77 | + const { passed, error } = await runAssertion(undefined, containsAcme); |
| 78 | + |
| 79 | + expect(passed).toBe(false); |
| 80 | + expect(error).toContain('got undefined'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('fails when the field path resolves to null', async () => { |
| 84 | + const { passed, error } = await runAssertion({ body: { data: { items: null } } }, containsAcme); |
| 85 | + |
| 86 | + expect(passed).toBe(false); |
| 87 | + expect(error).toContain('got null'); |
| 88 | + // `null` is not reported as `object` — the author needs to see which one it is. |
| 89 | + expect(error).not.toContain('got object'); |
| 90 | + expect(error).toContain('resolved to null'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('fails when the field path resolves to a number', async () => { |
| 94 | + const { passed, error } = await runAssertion({ body: { data: { items: 42 } } }, containsAcme); |
| 95 | + |
| 96 | + expect(passed).toBe(false); |
| 97 | + expect(error).toContain('got number'); |
| 98 | + // The path resolved fine here, so the ASSERTION is the suspect, not the fixture. |
| 99 | + expect(error).toContain('array membership and string substrings only'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('fails when the field path resolves to an object', async () => { |
| 103 | + const { passed, error } = await runAssertion( |
| 104 | + { body: { data: { items: { acme: true } } } }, |
| 105 | + containsAcme, |
| 106 | + ); |
| 107 | + |
| 108 | + expect(passed).toBe(false); |
| 109 | + expect(error).toContain('got object'); |
| 110 | + expect(error).toContain('array membership and string substrings only'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('fails when the field path resolves to a boolean', async () => { |
| 114 | + const { passed, error } = await runAssertion({ body: { data: { items: false } } }, containsAcme); |
| 115 | + |
| 116 | + expect(passed).toBe(false); |
| 117 | + expect(error).toContain('got boolean'); |
| 118 | + }); |
| 119 | + |
| 120 | + it('every non-evaluable shape reports the same failure, not a pass', async () => { |
| 121 | + const nonEvaluable: unknown[] = [undefined, null, 0, 42, false, true, { acme: true }]; |
| 122 | + |
| 123 | + for (const value of nonEvaluable) { |
| 124 | + const { passed, error } = await runAssertion({ body: { data: { items: value } } }, containsAcme); |
| 125 | + expect(passed, `contains against ${String(value)} must not pass`).toBe(false); |
| 126 | + expect(error).toContain("cannot be evaluated by 'contains'"); |
| 127 | + } |
| 128 | + }); |
| 129 | +}); |
| 130 | + |
| 131 | +describe('TestRunner — `contains` keeps its behaviour on the two evaluable shapes (#7256)', () => { |
| 132 | + it('passes when the array contains the expected member', async () => { |
| 133 | + const { passed } = await runAssertion({ body: { data: { items: ['acme', 'globex'] } } }, containsAcme); |
| 134 | + |
| 135 | + expect(passed).toBe(true); |
| 136 | + }); |
| 137 | + |
| 138 | + it('fails when the array does not contain the expected member', async () => { |
| 139 | + const { passed, error } = await runAssertion({ body: { data: { items: ['globex'] } } }, containsAcme); |
| 140 | + |
| 141 | + expect(passed).toBe(false); |
| 142 | + expect(error).toContain('array does not contain acme'); |
| 143 | + // Still the membership failure, NOT the new inapplicable-shape failure. |
| 144 | + expect(error).not.toContain("cannot be evaluated by 'contains'"); |
| 145 | + }); |
| 146 | + |
| 147 | + it('passes when the string contains the expected substring', async () => { |
| 148 | + const { passed } = await runAssertion({ body: { data: { items: 'acme corp' } } }, containsAcme); |
| 149 | + |
| 150 | + expect(passed).toBe(true); |
| 151 | + }); |
| 152 | + |
| 153 | + it('fails when the string does not contain the expected substring', async () => { |
| 154 | + const { passed, error } = await runAssertion({ body: { data: { items: 'globex corp' } } }, containsAcme); |
| 155 | + |
| 156 | + expect(passed).toBe(false); |
| 157 | + expect(error).toContain('string does not contain acme'); |
| 158 | + expect(error).not.toContain("cannot be evaluated by 'contains'"); |
| 159 | + }); |
| 160 | + |
| 161 | + it('an empty array is evaluable — it simply does not contain the member', async () => { |
| 162 | + const { passed, error } = await runAssertion({ body: { data: { items: [] } } }, containsAcme); |
| 163 | + |
| 164 | + expect(passed).toBe(false); |
| 165 | + expect(error).toContain('array does not contain acme'); |
| 166 | + }); |
| 167 | + |
| 168 | + it('an empty string is evaluable — every string contains the empty substring', async () => { |
| 169 | + const { passed } = await runAssertion( |
| 170 | + { body: { data: { items: '' } } }, |
| 171 | + { field: 'body.data.items', operator: 'contains', expectedValue: '' }, |
| 172 | + ); |
| 173 | + |
| 174 | + expect(passed).toBe(true); |
| 175 | + }); |
| 176 | +}); |
| 177 | + |
| 178 | +describe('TestRunner — the sibling operators are unchanged by #7256', () => { |
| 179 | + it('`equals` still compares unconditionally, including against a missing path', async () => { |
| 180 | + const { passed, error } = await runAssertion( |
| 181 | + { body: {} }, |
| 182 | + { field: 'body.status', operator: 'equals', expectedValue: 'active' }, |
| 183 | + ); |
| 184 | + |
| 185 | + expect(passed).toBe(false); |
| 186 | + expect(error).toContain('expected active'); |
| 187 | + }); |
| 188 | + |
| 189 | + it('`is_null` still passes on a missing path — absence is what it asserts', async () => { |
| 190 | + const { passed } = await runAssertion( |
| 191 | + { body: {} }, |
| 192 | + { field: 'body.status', operator: 'is_null', expectedValue: null }, |
| 193 | + ); |
| 194 | + |
| 195 | + expect(passed).toBe(true); |
| 196 | + }); |
| 197 | + |
| 198 | + it('`not_null` still fails on a missing path', async () => { |
| 199 | + const { passed } = await runAssertion( |
| 200 | + { body: {} }, |
| 201 | + { field: 'body.status', operator: 'not_null', expectedValue: null }, |
| 202 | + ); |
| 203 | + |
| 204 | + expect(passed).toBe(false); |
| 205 | + }); |
| 206 | + |
| 207 | + // `not_contains` / `gt` / `gte` / `lt` / `lte` / `error` are declared in |
| 208 | + // `TestAssertionTypeSchema` and have no branch in the runner. That is a DIFFERENT |
| 209 | + // defect from #7256 (a declared operator the engine refuses is annoying but honest, |
| 210 | + // where a silent pass is a lie), and it is pinned here so a later implementation is |
| 211 | + // a deliberate change rather than an accident. |
| 212 | + it('a declared-but-unimplemented operator is refused, not silently passed', async () => { |
| 213 | + const { passed, error } = await runAssertion( |
| 214 | + { body: { data: { items: ['globex'] } } }, |
| 215 | + { field: 'body.data.items', operator: 'not_contains', expectedValue: 'acme' }, |
| 216 | + ); |
| 217 | + |
| 218 | + expect(passed).toBe(false); |
| 219 | + expect(error).toContain('Unknown assertion operator: not_contains'); |
| 220 | + }); |
| 221 | +}); |
0 commit comments