@thymian/rules-testing 0.0.0-stage → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/README.md +227 -2
  2. package/dist/conforming-response.d.ts +12 -0
  3. package/dist/conforming-response.d.ts.map +1 -0
  4. package/dist/conforming-response.js +68 -0
  5. package/dist/conforming-response.js.map +1 -0
  6. package/dist/create-rule-tester.d.ts +101 -0
  7. package/dist/create-rule-tester.d.ts.map +1 -0
  8. package/dist/create-rule-tester.js +50 -0
  9. package/dist/create-rule-tester.js.map +1 -0
  10. package/dist/index.d.ts +5 -0
  11. package/dist/index.d.ts.map +1 -0
  12. package/dist/index.js +3 -0
  13. package/dist/index.js.map +1 -0
  14. package/dist/recorded.d.ts +43 -0
  15. package/dist/recorded.d.ts.map +1 -0
  16. package/dist/recorded.js +75 -0
  17. package/dist/recorded.js.map +1 -0
  18. package/dist/rule-test-execution.d.ts +87 -0
  19. package/dist/rule-test-execution.d.ts.map +1 -0
  20. package/dist/rule-test-execution.js +118 -0
  21. package/dist/rule-test-execution.js.map +1 -0
  22. package/dist/rule-under-test.d.ts +21 -0
  23. package/dist/rule-under-test.d.ts.map +1 -0
  24. package/dist/rule-under-test.js +24 -0
  25. package/dist/rule-under-test.js.map +1 -0
  26. package/dist/run-analyze.d.ts +20 -0
  27. package/dist/run-analyze.d.ts.map +1 -0
  28. package/dist/run-analyze.js +179 -0
  29. package/dist/run-analyze.js.map +1 -0
  30. package/dist/run-lint.d.ts +10 -0
  31. package/dist/run-lint.d.ts.map +1 -0
  32. package/dist/run-lint.js +37 -0
  33. package/dist/run-lint.js.map +1 -0
  34. package/dist/run-test.d.ts +25 -0
  35. package/dist/run-test.d.ts.map +1 -0
  36. package/dist/run-test.js +195 -0
  37. package/dist/run-test.js.map +1 -0
  38. package/dist/stated-applicability.d.ts +46 -0
  39. package/dist/stated-applicability.d.ts.map +1 -0
  40. package/dist/stated-applicability.js +66 -0
  41. package/dist/stated-applicability.js.map +1 -0
  42. package/package.json +50 -4
@@ -0,0 +1,195 @@
1
+ import { createHttpTestContext, isGroupedHttpTestCaseStep, isRuleLevelNotApplicable, isRuleViolationGroupLocation, isStatusWithoutSeverity, NoopLogger, resolveViolationLocation, runRules, } from '@thymian/core';
2
+ import { exampleRequestSampler, identityHookRunner, } from '@thymian/core-testing';
3
+ import { createRuns, HttpTestApiContext, } from '@thymian/plugin-http-tester';
4
+ import { conformingResponse } from './conforming-response.js';
5
+ import { executionsElsewhere, executionsOn, listExecutions, reportedExecutions, withEngineLabel, } from './rule-test-execution.js';
6
+ const logger = new NoopLogger();
7
+ /**
8
+ * Runs a rule in `test`: the tester's own context evaluates it, sending the
9
+ * requests it builds to `respond` instead of a server, and the tester's own
10
+ * report builder turns what it returns into executions, as for `thymian test`.
11
+ */
12
+ export async function runTest(underTest, format, respond = ({ conforming }) => conforming) {
13
+ const { rule, rulesConfig, declaredSeverity } = underTest;
14
+ const context = createHttpTestContext({
15
+ format,
16
+ logger,
17
+ locals: {},
18
+ sampleRequest: exampleRequestSampler,
19
+ ...respondingWith(respond),
20
+ });
21
+ const adapter = {
22
+ errorName: 'TestLinterError',
23
+ mode: 'test',
24
+ getRuleFn: (rule) => rule.testRule,
25
+ createContext: (rule) => new HttpTestApiContext(rule.meta.name, context),
26
+ };
27
+ const result = await runRules(logger, [rule], format, rulesConfig, adapter);
28
+ const [run] = createRuns('@thymian/rules-testing', result, [rule], format, logger);
29
+ // An execution the tester could tie to no request it sent carries only the
30
+ // heading of its location as its name; the location itself is the one the
31
+ // rule returned under that heading.
32
+ const returnedLocations = new Map();
33
+ for (const { location } of result[rule.meta.name]?.ruleFnResult ?? []) {
34
+ // A group is located on its members, further down.
35
+ if (isRuleViolationGroupLocation(location)) {
36
+ continue;
37
+ }
38
+ const { heading } = resolveViolationLocation(location, format, rule.meta.name);
39
+ if (!returnedLocations.has(heading)) {
40
+ returnedLocations.set(heading, location);
41
+ }
42
+ }
43
+ const onItsLocation = (execution) => {
44
+ const { status, steps } = execution;
45
+ return {
46
+ location: withEngineLabel(locationOf(execution, returnedLocations), format, rule),
47
+ status: status.kind,
48
+ ...(status.kind === 'failed'
49
+ ? {
50
+ // The tester leaves the reason out when the rule gave no
51
+ // message, where the other engines name the rule.
52
+ message: status.reason ??
53
+ rule.meta.summary ??
54
+ rule.meta.description ??
55
+ rule.meta.name,
56
+ severity: declaredSeverity,
57
+ }
58
+ : {}),
59
+ ...(isStatusWithoutSeverity(status.kind) &&
60
+ 'reason' in status &&
61
+ status.reason !== undefined
62
+ ? { reason: status.reason }
63
+ : {}),
64
+ // The tester repeats a violation as a `rule-violation` finding on its
65
+ // step, which a rule cannot attach itself and `message` already says.
66
+ findings: steps
67
+ .flatMap(({ findings }) => findings)
68
+ .filter(({ kind }) => kind !== 'rule-violation'),
69
+ };
70
+ };
71
+ // A rule that applied to nothing gets one execution of its own, on no
72
+ // transaction; the list below already says `notApplicable` for each of them.
73
+ const executions = (run?.runType === 'test' ? (run.executions ?? []) : []).filter((execution) => !isRuleLevelNotApplicable(execution));
74
+ // The tester reports one execution per test case, in the order it ran them.
75
+ const groups = (result[rule.meta.name]?.diagnostics?.calls ?? []).flatMap(({ testResult, placements, rejectedCases = [] }) => testResult.cases.map((testCase, testCaseIndex) => groupOf(testCase, placements
76
+ .filter((placement) => placement.testCaseIndex === testCaseIndex)
77
+ .map((placement) => placement.result), rejectedCases.find((rejected) => rejected.testCaseIndex === testCaseIndex)?.rejected ?? [])));
78
+ const reported = executions.flatMap((execution, index) => {
79
+ const group = groups[index];
80
+ return group === undefined
81
+ ? [onItsLocation(execution)]
82
+ : executionsOnMembers(group, execution, underTest, format);
83
+ });
84
+ // The tester reports an execution for every transaction it sent a request
85
+ // for, so one it reported nothing on is one the rule did not apply to.
86
+ return listExecutions(format, reported, () => false);
87
+ }
88
+ /** The group of a test case of a grouped rule; none for any other case. */
89
+ function groupOf({ steps }, returned, rejected) {
90
+ const [step] = steps;
91
+ if (step === undefined || !isGroupedHttpTestCaseStep(step)) {
92
+ return undefined;
93
+ }
94
+ // `rejected` indexes the transactions of the step, members without a
95
+ // source included.
96
+ return {
97
+ members: step.transactions.flatMap(({ source }, transactionIndex) => {
98
+ if (source === undefined) {
99
+ return [];
100
+ }
101
+ const rejection = rejected.find((pair) => pair.transactionIndex === transactionIndex)?.reason;
102
+ return [
103
+ {
104
+ transaction: source,
105
+ ...(rejection !== undefined ? { rejection } : {}),
106
+ },
107
+ ];
108
+ }),
109
+ returned,
110
+ };
111
+ }
112
+ /**
113
+ * The tester reports a group as one execution, on the group key: failed when
114
+ * the rule returned a violation on any of it. Who is at fault is in what the
115
+ * rule returned, read as core's report builder reads it in `static`: a member
116
+ * it located a violation on, or the group, which is every judged member. A
117
+ * member the live re-check rejected is `notApplicable`, with its own reason
118
+ * (ADR-0030). A judged member the rule returned nothing on has the status of
119
+ * the execution, and where that is the failure of another member, it passed.
120
+ */
121
+ function executionsOnMembers({ members, returned }, { status }, underTest, format) {
122
+ const said = reportedExecutions({
123
+ [underTest.rule.meta.name]: {
124
+ diagnostics: undefined,
125
+ ruleFnResult: returned,
126
+ },
127
+ }, underTest, format, 'lint');
128
+ const skipped = status.kind === 'skipped'
129
+ ? {
130
+ status: 'skipped',
131
+ ...(status.reason !== undefined ? { reason: status.reason } : {}),
132
+ }
133
+ : {};
134
+ return [
135
+ ...members.flatMap(({ transaction, rejection }) => rejection !== undefined
136
+ ? executionsOn(transaction, said, 'notApplicable').map((execution) => said.includes(execution)
137
+ ? execution
138
+ : { ...execution, reason: rejection })
139
+ : executionsOn(transaction, said, 'passed').map((execution) => said.includes(execution) ? execution : { ...execution, ...skipped })),
140
+ ...executionsElsewhere(members.map(({ transaction }) => transaction), said),
141
+ ];
142
+ }
143
+ /**
144
+ * The two members of the tester's context that stand for the server.
145
+ *
146
+ * The tester hands `runRequest` the request alone, and two answers of one
147
+ * operation are asked for with the same request, so the transaction a request
148
+ * is aimed at is known only where the tester passes the response on together
149
+ * with it: the `afterResponse` step of its context. The responder answers
150
+ * there. No hook is registered or run; the other steps pass their value
151
+ * through.
152
+ */
153
+ function respondingWith(respond) {
154
+ const unanswered = {
155
+ statusCode: 0,
156
+ headers: {},
157
+ trailers: {},
158
+ duration: 0,
159
+ };
160
+ return {
161
+ runRequest: async () => unanswered,
162
+ async runHook(name, payload) {
163
+ if (name !== 'afterResponse') {
164
+ return identityHookRunner(name, payload);
165
+ }
166
+ const { request, thymianTransaction } = payload.ctx;
167
+ if (thymianTransaction === undefined) {
168
+ throw new Error(`The request ${request.method.toUpperCase()} ${request.path} is aimed at no transaction of the format, so a rule test cannot answer it.`);
169
+ }
170
+ const result = await respond({
171
+ transaction: thymianTransaction.transactionId,
172
+ conforming: conformingResponse(thymianTransaction.thymianRes),
173
+ request,
174
+ });
175
+ return { result };
176
+ },
177
+ };
178
+ }
179
+ /**
180
+ * Where the tester located an execution: on the transaction of its first
181
+ * step, as the engine reports it, or on whatever the rule returned when the
182
+ * tester sent no request for it.
183
+ */
184
+ function locationOf({ name, steps }, returnedLocations) {
185
+ const location = steps[0]?.location;
186
+ if (location?.type === 'thymianFormat') {
187
+ const { elementType, elementId, pointer } = location;
188
+ return { elementType, elementId, pointer };
189
+ }
190
+ if (location?.type === 'custom') {
191
+ return location.value;
192
+ }
193
+ return returnedLocations.get(name) ?? name;
194
+ }
195
+ //# sourceMappingURL=run-test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"run-test.js","sourceRoot":"","sources":["../src/run-test.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EAMrB,yBAAyB,EACzB,wBAAwB,EACxB,4BAA4B,EAC5B,uBAAuB,EACvB,UAAU,EACV,wBAAwB,EAKxB,QAAQ,GAIT,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,qBAAqB,EACrB,kBAAkB,GACnB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACL,UAAU,EACV,kBAAkB,GAEnB,MAAM,6BAA6B,CAAC;AAErC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AC;QACvC,MAAM,EAAE,WAAW,EAAE,SAAS,EAAE,OAAO,EAAE,GAAG,QAAQ,CAAC;QACrD,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC;IAC7C,CAAC;IAED,IAAI,QAAQ,EAAE,IAAI,KAAK,QAAQ,EAAE,CAAC;QAChC,OAAO,QAAQ,CAAC,KAAK,CAAC;IACxB,CAAC;IAED,OAAO,iBAAiB,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC;AAC7C,CAAC"}
@@ -0,0 +1,46 @@
1
+ import { type CommonHttpRuleCriteria, type HttpFilterExpression, type RuleFnResult } from '@thymian/core';
2
+ /**
3
+ * How the rule-context calls of one validation context take the rule's
4
+ * Applicability. The contexts differ: `validateHttpTransactions` takes a plain
5
+ * callback in `static` and a filter expression in `analytics`.
6
+ */
7
+ export type ApplicabilityCalls = {
8
+ /**
9
+ * The calls whose first argument states the Applicability as a filter
10
+ * expression, readable from outside the rule.
11
+ */
12
+ stating: ReadonlySet<PropertyKey>;
13
+ /**
14
+ * The calls that say nothing about a transaction the rule did not flag:
15
+ * they take the Applicability as a plain callback, or take none.
16
+ */
17
+ notStating: ReadonlySet<PropertyKey>;
18
+ };
19
+ /**
20
+ * What a rule stated about its Applicability while it ran: the `appliesTo`
21
+ * expression of every rule-context call it made, or `undefined` when at least
22
+ * one call stated none (it decided in a callback, took no applicability, or
23
+ * gave a lone positional expression, which is its violation condition), or the
24
+ * rule made no call.
25
+ */
26
+ export type StatedApplicability = HttpFilterExpression[] | undefined;
27
+ /**
28
+ * Wraps a rule context so that the Applicability the rule states to it can be
29
+ * read after the rule ran. The context itself evaluates the rule untouched.
30
+ */
31
+ export declare function observeApplicability<Context extends object>(context: Context, { stating, notStating }: ApplicabilityCalls): {
32
+ context: Context;
33
+ stated(): StatedApplicability;
34
+ };
35
+ /**
36
+ * The transactions the stated Applicability accepts, asked of the engine's
37
+ * context in a call of its own; `undefined` when the rule stated none, so that
38
+ * every transaction counts as applicable.
39
+ *
40
+ * The context is one the rule did not run on. Whatever it does not locate on
41
+ * a transaction is left out.
42
+ */
43
+ export declare function applicableTransactions(stated: StatedApplicability, context: {
44
+ validateCommonHttpTransactions(criteria: CommonHttpRuleCriteria): RuleFnResult[];
45
+ }): Set<string> | undefined;
46
+ //# sourceMappingURL=stated-applicability.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stated-applicability.d.ts","sourceRoot":"","sources":["../src/stated-applicability.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,sBAAsB,EAC3B,KAAK,oBAAoB,EAGzB,KAAK,YAAY,EAClB,MAAM,eAAe,CAAC;AAEvB;;;;GAIG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAC/B;;;OAGG;IACH,OAAO,EAAE,WAAW,CAAC,WAAW,CAAC,CAAC;IAClC;;;OAGG;IACH,UAAU,EAAE,WAAW,CAAC,WAAW,CAAC,CAAC;CACtC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,MAAM,mBAAmB,GAAG,oBAAoB,EAAE,GAAG,SAAS,CAAC;AAErE;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,SAAS,MAAM,EACzD,OAAO,EAAE,OAAO,EAChB,EAAE,OAAO,EAAE,UAAU,EAAE,EAAE,kBAAkB,GAC1C;IAAE,OAAO,EAAE,OAAO,CAAC;IAAC,MAAM,IAAI,mBAAmB,CAAA;CAAE,CAyCrD;AAED;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CACpC,MAAM,EAAE,mBAAmB,EAC3B,OAAO,EAAE;IACP,8BAA8B,CAC5B,QAAQ,EAAE,sBAAsB,GAC/B,YAAY,EAAE,CAAC;CACnB,GACA,GAAG,CAAC,MAAM,CAAC,GAAG,SAAS,CAqBzB"}
@@ -0,0 +1,66 @@
1
+ import { isRuleCriteria, isRuleViolationGroupLocation, } from '@thymian/core';
2
+ /**
3
+ * Wraps a rule context so that the Applicability the rule states to it can be
4
+ * read after the rule ran. The context itself evaluates the rule untouched.
5
+ */
6
+ export function observeApplicability(context, { stating, notStating }) {
7
+ const expressions = [];
8
+ let unstated = false;
9
+ const observed = new Proxy(context, {
10
+ get(target, property) {
11
+ const value = Reflect.get(target, property, target);
12
+ if (typeof value !== 'function') {
13
+ return value;
14
+ }
15
+ return (...args) => {
16
+ if (stating.has(property)) {
17
+ const [first] = args;
18
+ if (isRuleCriteria(first)) {
19
+ expressions.push(first.appliesTo);
20
+ }
21
+ else if (args.length > 1) {
22
+ expressions.push(first);
23
+ }
24
+ else {
25
+ // A lone positional expression is the violation condition, not
26
+ // an applicability: every transaction it does not match is one
27
+ // the rule accepted.
28
+ unstated = true;
29
+ }
30
+ }
31
+ else if (notStating.has(property)) {
32
+ unstated = true;
33
+ }
34
+ return value.apply(target, args);
35
+ };
36
+ },
37
+ });
38
+ return {
39
+ context: observed,
40
+ stated: () => unstated || expressions.length === 0 ? undefined : expressions,
41
+ };
42
+ }
43
+ /**
44
+ * The transactions the stated Applicability accepts, asked of the engine's
45
+ * context in a call of its own; `undefined` when the rule stated none, so that
46
+ * every transaction counts as applicable.
47
+ *
48
+ * The context is one the rule did not run on. Whatever it does not locate on
49
+ * a transaction is left out.
50
+ */
51
+ export function applicableTransactions(stated, context) {
52
+ if (stated === undefined) {
53
+ return undefined;
54
+ }
55
+ return new Set(stated
56
+ .flatMap((appliesTo) => context.validateCommonHttpTransactions({
57
+ appliesTo,
58
+ violatedWhen: (_req, _res, location) => [
59
+ { location, findings: [] },
60
+ ],
61
+ }))
62
+ .flatMap(({ location }) => typeof location === 'string' || isRuleViolationGroupLocation(location)
63
+ ? []
64
+ : [location.elementId]));
65
+ }
66
+ //# sourceMappingURL=stated-applicability.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stated-applicability.js","sourceRoot":"","sources":["../src/stated-applicability.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,cAAc,EACd,4BAA4B,GAE7B,MAAM,eAAe,CAAC;AA6BvB;;;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E,CACX,QAAQ,IAAI,WAAW,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW;KACjE,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,sBAAsB,CACpC,MAA2B,EAC3B,OAIC;IAED,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACzB,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,OAAO,IAAI,GAAG,CACZ,MAAM;SACH,OAAO,CAAC,CAAC,SAAS,EAAE,EAAE,CACrB,OAAO,CAAC,8BAA8B,CAAC;QACrC,SAAS;QACT,YAAY,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAkB,EAAE,CAAC;YACtD,EAAE,QAAQ,EAAE,QAAQ,EAAE,EAAE,EAAE;SAC3B;KACF,CAAC,CACH;SACA,OAAO,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE,CACxB,OAAO,QAAQ,KAAK,QAAQ,IAAI,4BAA4B,CAAC,QAAQ,CAAC;QACpE,CAAC,CAAC,EAAE;QACJ,CAAC,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC,CACzB,CACJ,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,52 @@
1
1
  {
2
2
  "name": "@thymian/rules-testing",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.4.1",
4
+ "description": "Test a Thymian rule through the real engine of each validation context it declares",
5
+ "license": "AGPL-3.0-only",
6
+ "bugs": {
7
+ "url": "https://github.com/thymianofficial/thymian/issues"
8
+ },
9
+ "homepage": "https://thymian.dev",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/thymianofficial/thymian.git",
13
+ "directory": "packages/rules-testing"
14
+ },
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "engines": {
19
+ "node": ">=22"
20
+ },
21
+ "keywords": [
22
+ "thymian",
23
+ "testing",
24
+ "rule-testing",
25
+ "http-rules",
26
+ "api-testing"
27
+ ],
28
+ "main": "./dist/index.js",
29
+ "module": "./dist/index.js",
30
+ "types": "./dist/index.d.ts",
31
+ "type": "module",
32
+ "exports": {
33
+ "./package.json": "./package.json",
34
+ ".": {
35
+ "types": "./dist/index.d.ts",
36
+ "import": "./dist/index.js",
37
+ "default": "./dist/index.js"
38
+ }
39
+ },
40
+ "files": [
41
+ "dist",
42
+ "!**/*.tsbuildinfo"
43
+ ],
44
+ "dependencies": {
45
+ "@thymian/core": "0.4.1",
46
+ "@thymian/core-testing": "0.4.1",
47
+ "@thymian/plugin-http-analyzer": "0.4.1",
48
+ "@thymian/plugin-http-linter": "0.4.1",
49
+ "@thymian/plugin-http-tester": "0.4.1",
50
+ "tslib": "^2.3.0"
51
+ }
52
+ }