@shivaedev/quality 0.58.0 → 0.59.0

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 (66) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +8 -2
  3. package/dist/adapters/bodyReferences.d.ts +10 -0
  4. package/dist/adapters/bodyReferences.d.ts.map +1 -0
  5. package/dist/adapters/bodyReferences.js +125 -0
  6. package/dist/adapters/bodyReferences.js.map +1 -0
  7. package/dist/adapters/engineEscapes.d.ts +10 -0
  8. package/dist/adapters/engineEscapes.d.ts.map +1 -0
  9. package/dist/adapters/engineEscapes.js +103 -0
  10. package/dist/adapters/engineEscapes.js.map +1 -0
  11. package/dist/adapters/outcomeAssertions.d.ts +9 -0
  12. package/dist/adapters/outcomeAssertions.d.ts.map +1 -0
  13. package/dist/adapters/outcomeAssertions.js +94 -0
  14. package/dist/adapters/outcomeAssertions.js.map +1 -0
  15. package/dist/adapters/positiveRefusalFindings.d.ts +4 -0
  16. package/dist/adapters/positiveRefusalFindings.d.ts.map +1 -0
  17. package/dist/adapters/positiveRefusalFindings.js +41 -0
  18. package/dist/adapters/positiveRefusalFindings.js.map +1 -0
  19. package/dist/adapters/rpcBoundaryFindings.d.ts +4 -0
  20. package/dist/adapters/rpcBoundaryFindings.d.ts.map +1 -0
  21. package/dist/adapters/rpcBoundaryFindings.js +90 -0
  22. package/dist/adapters/rpcBoundaryFindings.js.map +1 -0
  23. package/dist/adapters/serviceAccess.d.ts +4 -0
  24. package/dist/adapters/serviceAccess.d.ts.map +1 -0
  25. package/dist/adapters/serviceAccess.js +33 -0
  26. package/dist/adapters/serviceAccess.js.map +1 -0
  27. package/dist/adapters/vitePort.d.ts +2 -0
  28. package/dist/adapters/vitePort.d.ts.map +1 -0
  29. package/dist/adapters/vitePort.js +5 -0
  30. package/dist/adapters/vitePort.js.map +1 -0
  31. package/dist/appViteConfig.d.ts +4 -1
  32. package/dist/appViteConfig.d.ts.map +1 -1
  33. package/dist/appViteConfig.js +7 -3
  34. package/dist/appViteConfig.js.map +1 -1
  35. package/dist/rules/builtIn.d.ts +2 -2
  36. package/dist/rules/builtIn.d.ts.map +1 -1
  37. package/dist/rules/builtIn.js +4 -0
  38. package/dist/rules/builtIn.js.map +1 -1
  39. package/dist/rules/effect/rpcBoundary.d.ts +2 -0
  40. package/dist/rules/effect/rpcBoundary.d.ts.map +1 -0
  41. package/dist/rules/effect/rpcBoundary.js +9 -0
  42. package/dist/rules/effect/rpcBoundary.js.map +1 -0
  43. package/dist/rules/stories/engine.d.ts.map +1 -1
  44. package/dist/rules/stories/engine.js +24 -7
  45. package/dist/rules/stories/engine.js.map +1 -1
  46. package/dist/rules/stories/positiveRefusals.d.ts +2 -0
  47. package/dist/rules/stories/positiveRefusals.d.ts.map +1 -0
  48. package/dist/rules/stories/positiveRefusals.js +17 -0
  49. package/dist/rules/stories/positiveRefusals.js.map +1 -0
  50. package/dist/rules/stories/usesInputs.d.ts.map +1 -1
  51. package/dist/rules/stories/usesInputs.js +3 -2
  52. package/dist/rules/stories/usesInputs.js.map +1 -1
  53. package/package.json +4 -4
  54. package/src/adapters/bodyReferences.ts +141 -0
  55. package/src/adapters/engineEscapes.ts +119 -0
  56. package/src/adapters/outcomeAssertions.ts +110 -0
  57. package/src/adapters/positiveRefusalFindings.ts +52 -0
  58. package/src/adapters/rpcBoundaryFindings.ts +98 -0
  59. package/src/adapters/serviceAccess.ts +38 -0
  60. package/src/adapters/vitePort.ts +5 -0
  61. package/src/appViteConfig.ts +13 -3
  62. package/src/rules/builtIn.ts +4 -0
  63. package/src/rules/effect/rpcBoundary.ts +10 -0
  64. package/src/rules/stories/engine.ts +25 -9
  65. package/src/rules/stories/positiveRefusals.ts +17 -0
  66. package/src/rules/stories/usesInputs.ts +3 -2
@@ -0,0 +1,119 @@
1
+ import { posix } from "node:path";
2
+ import ts from "typescript";
3
+ import { isTestSupport, testName } from "#naming/testName.ts";
4
+ import { engineOf } from "#rules/stories/engineOf.ts";
5
+ import { memberName, unwrapped } from "#rules/stories/syntax.ts";
6
+ import { type BodyReference, bodyReferences, storyBody } from "./bodyReferences.ts";
7
+ import { serviceAccess } from "./serviceAccess.ts";
8
+
9
+ export interface EngineEscape {
10
+ readonly node: ts.Node;
11
+ readonly reason: string;
12
+ }
13
+
14
+ function production({ file, module }: BodyReference): boolean {
15
+ return (
16
+ !(isTestSupport(file ?? module) || module.startsWith("@shivaedev/test"))
17
+ && (/^[.#/]/u.test(module)
18
+ || module.startsWith("@shivaedev/")
19
+ || engineOf({ kind: "external", package: module, specifier: module }) !== undefined)
20
+ );
21
+ }
22
+
23
+ function constructorTest(file: string, reference: BodyReference): boolean {
24
+ const name = testName(file);
25
+ const target = reference.file ?? posix.join(posix.dirname(file), reference.module);
26
+ return name !== undefined && target.endsWith(`/${posix.join(name.folder, name.stem)}${name.extension}`);
27
+ }
28
+
29
+ function constructorCall(node: ts.CallExpression | ts.NewExpression, called: BodyReference): boolean {
30
+ const name = called.path.at(-1) ?? "";
31
+ return (
32
+ called.kind === "import"
33
+ && production(called)
34
+ && (ts.isNewExpression(node) || /^(?:(?:make|create|build)[A-Z]|make$|create$|build$|defineService$|layer(?:$|[A-Z]))/u.test(name))
35
+ );
36
+ }
37
+
38
+ export function engineEscapes(body: ts.Expression, checker: ts.TypeChecker, file: string) {
39
+ const declaration = storyBody(body, checker);
40
+ if (declaration?.body === undefined) {
41
+ return { drivesSubject: false, escapes: [] };
42
+ }
43
+ const reference = bodyReferences(declaration, checker);
44
+ const block = declaration.body;
45
+ const escapes: EngineEscape[] = [];
46
+ let drivesSubject = false;
47
+ function transactionHandle(node: ts.Node, value: BodyReference): boolean {
48
+ if (!((value.module === "effect/sql/SqlClient" || value.module === "effect/sql") && value.path.at(-1) === "SqlClient")) {
49
+ return false;
50
+ }
51
+ const yielded = node.parent;
52
+ const binding = yielded.parent;
53
+ if (!(ts.isYieldExpression(yielded) && ts.isVariableDeclaration(binding) && ts.isIdentifier(binding.name))) {
54
+ return false;
55
+ }
56
+ const name = binding.name;
57
+ const symbol = checker.getSymbolAtLocation(name);
58
+ const uses: ts.Identifier[] = [];
59
+ function collect(current: ts.Node): void {
60
+ if (ts.isIdentifier(current) && current !== name && checker.getSymbolAtLocation(current) === symbol) {
61
+ uses.push(current);
62
+ }
63
+ ts.forEachChild(current, collect);
64
+ }
65
+ collect(block);
66
+ return (
67
+ uses.length > 0
68
+ && uses.every(
69
+ (use) =>
70
+ (ts.isPropertyAccessExpression(use.parent) || ts.isElementAccessExpression(use.parent))
71
+ && use.parent.expression === use
72
+ && memberName(use.parent) === "withTransaction",
73
+ )
74
+ );
75
+ }
76
+ function accessReason(node: ts.Node): string | undefined {
77
+ const value = reference(node);
78
+ if (value?.kind === "told" && value.path[0] === "story" && value.path[1] === "engine") {
79
+ return "Reads story.engine";
80
+ }
81
+ if (value === undefined || !serviceAccess(node, value, reference)) {
82
+ return undefined;
83
+ }
84
+ if (transactionHandle(node, value)) {
85
+ drivesSubject = true;
86
+ return undefined;
87
+ }
88
+ return "Reads a raw service handle";
89
+ }
90
+ function checkConstructor(node: ts.CallExpression | ts.NewExpression): void {
91
+ const called = reference(unwrapped(node.expression));
92
+ if (called === undefined || !constructorCall(node, called)) {
93
+ return;
94
+ }
95
+ if (constructorTest(file, called)) {
96
+ drivesSubject = true;
97
+ } else {
98
+ escapes.push({ node, reason: "Calls a production constructor" });
99
+ }
100
+ }
101
+ function visit(node: ts.Node): void {
102
+ if (ts.isTypeNode(node)) {
103
+ return;
104
+ }
105
+ if (ts.isIdentifier(node) || ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node)) {
106
+ const reason = accessReason(node);
107
+ if (reason !== undefined) {
108
+ escapes.push({ node, reason });
109
+ return;
110
+ }
111
+ }
112
+ if (ts.isCallExpression(node) || ts.isNewExpression(node)) {
113
+ checkConstructor(node);
114
+ }
115
+ ts.forEachChild(node, visit);
116
+ }
117
+ visit(declaration.body);
118
+ return { drivesSubject, escapes: escapes.filter((found, index) => escapes.findIndex((other) => other.reason === found.reason) === index) };
119
+ }
@@ -0,0 +1,110 @@
1
+ import ts from "typescript";
2
+ import { type ImportBinding, importBindings } from "#rules/stories/importBindings.ts";
3
+ import { memberName, unwrapped } from "#rules/stories/syntax.ts";
4
+
5
+ export interface OutcomeAssertion {
6
+ readonly expected: ts.Expression;
7
+ readonly negative: boolean;
8
+ readonly node: ts.Node;
9
+ readonly subject: string;
10
+ }
11
+
12
+ function receiver(node: ts.Expression): ts.Expression | undefined {
13
+ const value = unwrapped(node);
14
+ return ts.isPropertyAccessExpression(value) || ts.isElementAccessExpression(value) ? unwrapped(value.expression) : undefined;
15
+ }
16
+
17
+ function imported(node: ts.Expression, name: string, modules: readonly string[], bindings: ReadonlyMap<string, ImportBinding>): boolean {
18
+ const value = unwrapped(node);
19
+ if (!ts.isIdentifier(value)) {
20
+ return false;
21
+ }
22
+ const binding = bindings.get(value.text);
23
+ return binding === undefined ? value.text === name : binding.imported === name && modules.includes(binding.module);
24
+ }
25
+
26
+ function subject(node: ts.Expression, checker: ts.TypeChecker): string | undefined {
27
+ const value = unwrapped(node);
28
+ if (ts.isYieldExpression(value)) {
29
+ return value.expression === undefined ? undefined : subject(value.expression, checker);
30
+ }
31
+ if (ts.isIdentifier(value)) {
32
+ const declaration = checker.getSymbolAtLocation(value)?.valueDeclaration;
33
+ if (declaration !== undefined && ts.isVariableDeclaration(declaration) && declaration.initializer !== undefined) {
34
+ return subject(declaration.initializer, checker);
35
+ }
36
+ }
37
+ const named = ts.isCallExpression(value) ? unwrapped(value.expression) : value;
38
+ const name = ts.isIdentifier(named) ? named.text : memberName(named);
39
+ return name !== undefined && /outcome$/iu.test(name) ? value.getText() : undefined;
40
+ }
41
+
42
+ function assertion(node: ts.CallExpression, checker: ts.TypeChecker, bindings: ReadonlyMap<string, ImportBinding>): OutcomeAssertion | undefined {
43
+ const matcher = memberName(node.expression);
44
+ if (matcher === undefined || !["toBe", "toEqual", "toMatchObject"].includes(matcher)) {
45
+ return;
46
+ }
47
+ let root = receiver(node.expression);
48
+ const negative = root !== undefined && memberName(root) === "not";
49
+ if (negative && root !== undefined) {
50
+ root = receiver(root);
51
+ }
52
+ if (root === undefined || !ts.isCallExpression(root)) {
53
+ return;
54
+ }
55
+ const callee = memberName(root.expression) === "soft" ? receiver(root.expression) : root.expression;
56
+ const actual = root.arguments[0];
57
+ const expected = node.arguments[0];
58
+ const key = actual === undefined ? undefined : subject(actual, checker);
59
+ if (
60
+ callee !== undefined
61
+ && imported(callee, "expect", ["vitest", "@effect/vitest"], bindings)
62
+ && key !== undefined
63
+ && expected !== undefined
64
+ && (!negative || matcher !== "toMatchObject")
65
+ ) {
66
+ return { expected, negative, node, subject: key };
67
+ }
68
+ return undefined;
69
+ }
70
+
71
+ function grouped(node: ts.CallExpression, checker: ts.TypeChecker, bindings: ReadonlyMap<string, ImportBinding>): readonly OutcomeAssertion[] {
72
+ const rows = node.arguments[0];
73
+ if (
74
+ !imported(node.expression, "outcomes", ["@shivaedev/test-story/server/outcomes.ts"], bindings)
75
+ || rows === undefined
76
+ || !ts.isArrayLiteralExpression(rows)
77
+ ) {
78
+ return [];
79
+ }
80
+ const assertions: OutcomeAssertion[] = [];
81
+ for (const row of rows.elements) {
82
+ if (!ts.isArrayLiteralExpression(row)) {
83
+ continue;
84
+ }
85
+ const read = row.elements[1];
86
+ const expected = row.elements[2];
87
+ const key = read !== undefined && ts.isArrowFunction(read) && !ts.isBlock(read.body) ? subject(read.body, checker) : undefined;
88
+ if (key !== undefined && expected !== undefined) {
89
+ assertions.push({ expected, negative: false, node: row, subject: key });
90
+ }
91
+ }
92
+ return assertions;
93
+ }
94
+
95
+ export function outcomeAssertions(body: ts.Node, checker: ts.TypeChecker): readonly OutcomeAssertion[] {
96
+ const bindings = importBindings(body.getSourceFile());
97
+ const assertions: OutcomeAssertion[] = [];
98
+ function visit(node: ts.Node): void {
99
+ if (ts.isFunctionLike(node) && node !== body) {
100
+ return;
101
+ }
102
+ if (ts.isCallExpression(node)) {
103
+ const direct = assertion(node, checker, bindings);
104
+ assertions.push(...(direct === undefined ? [] : [direct]), ...grouped(node, checker, bindings));
105
+ }
106
+ ts.forEachChild(node, visit);
107
+ }
108
+ visit(body);
109
+ return assertions;
110
+ }
@@ -0,0 +1,52 @@
1
+ import ts from "typescript";
2
+ import type { Finding, SourceFile } from "#rule.ts";
3
+ import type { storyOrigins } from "#rules/stories/origins.ts";
4
+ import { nodesWhere, unwrapped } from "#rules/stories/syntax.ts";
5
+ import { type OutcomeAssertion, outcomeAssertions } from "./outcomeAssertions.ts";
6
+
7
+ const MESSAGE =
8
+ "Only rules out an outcome. Assert the expected refusal's domain reason or public response positively on the same outcome in this story; an unrelated failure must fail the story too.";
9
+
10
+ function concrete(node: ts.Expression): boolean {
11
+ const value = unwrapped(node);
12
+ return (
13
+ (ts.isStringLiteralLike(value) && value.text.length > 0)
14
+ || (ts.isObjectLiteralExpression(value)
15
+ && value.properties.some((property) => ts.isPropertyAssignment(property) && concrete(property.initializer)))
16
+ );
17
+ }
18
+
19
+ function sameExpected(left: ts.Expression, right: ts.Expression): boolean {
20
+ const a = unwrapped(left);
21
+ const b = unwrapped(right);
22
+ return ts.isStringLiteralLike(a) && ts.isStringLiteralLike(b) ? a.text === b.text : a.getText() === b.getText();
23
+ }
24
+
25
+ function confirmsRefusal(positive: OutcomeAssertion, negative: OutcomeAssertion): boolean {
26
+ return (
27
+ !positive.negative && positive.subject === negative.subject && concrete(positive.expected) && !sameExpected(positive.expected, negative.expected)
28
+ );
29
+ }
30
+
31
+ export function positiveRefusalFindings(file: SourceFile, origins: ReturnType<typeof storyOrigins>): readonly Finding[] {
32
+ const source = origins.source(file.path);
33
+ if (source === undefined) {
34
+ return [];
35
+ }
36
+ return nodesWhere(source, (node) => ts.isCallExpression(node) && origins.origin(node.expression) === "story")
37
+ .filter(ts.isCallExpression)
38
+ .flatMap((story) => {
39
+ const body = story.arguments[2];
40
+ if (body === undefined) {
41
+ return [];
42
+ }
43
+ const assertions = outcomeAssertions(body, origins.checker);
44
+ return assertions
45
+ .filter((negative) => negative.negative && !assertions.some((positive) => confirmsRefusal(positive, negative)))
46
+ .map(({ node }) => ({
47
+ file: file.path,
48
+ line: source.getLineAndCharacterOfPosition(node.getStart(source)).line + 1,
49
+ message: MESSAGE,
50
+ }));
51
+ });
52
+ }
@@ -0,0 +1,98 @@
1
+ import { join } from "node:path";
2
+ import { Effect } from "effect";
3
+ import ts from "typescript";
4
+ import { importGraph } from "#imports/graph.ts";
5
+ import { isTestSupport } from "#naming/testName.ts";
6
+ import type { Finding, RuleInputs } from "#rule.ts";
7
+ import { effectProgram } from "#rules/effect/program.ts";
8
+ import { nodesWhere } from "#rules/stories/syntax.ts";
9
+
10
+ function boundaryName(declaration: ts.Declaration | undefined): string | undefined {
11
+ if (declaration === undefined || !ts.isFunctionDeclaration(declaration)) {
12
+ return undefined;
13
+ }
14
+ const name = declaration.name?.text;
15
+ const path = declaration.getSourceFile().fileName.replaceAll("\\", "/");
16
+ return /\/(?:@shivaedev\/)?errors\/(?:src|dist)\/(?:boundary|rpcBoundaryHandlers)\.(?:d\.)?ts$/u.test(path)
17
+ && (name === "rpcBoundary" || name === "rpcBoundaryHandlers")
18
+ ? name
19
+ : undefined;
20
+ }
21
+
22
+ function handlerBuilder(declaration: ts.Declaration | undefined): boolean {
23
+ if (declaration === undefined) {
24
+ return false;
25
+ }
26
+ const path = declaration.getSourceFile().fileName.replaceAll("\\", "/");
27
+ return (
28
+ ts.isMethodSignature(declaration)
29
+ && ["toLayer", "toLayerHandler", "toHandlers", "of"].includes(declaration.name.getText())
30
+ && /\/effect\/(?:src|dist(?:\/dts)?)\/(?:unstable\/)?rpc\/RpcGroup\.(?:d\.)?ts$/u.test(path)
31
+ );
32
+ }
33
+
34
+ function checkSources(inputs: RuleInputs): readonly Finding[] {
35
+ const program = effectProgram(inputs);
36
+ const checker = program.getTypeChecker();
37
+ return inputs.sources.flatMap((file) => {
38
+ const source = program.getSourceFile(join(inputs.root, file.path));
39
+ if (source === undefined) {
40
+ return [];
41
+ }
42
+ const calls = nodesWhere(source, ts.isCallExpression)
43
+ .filter(ts.isCallExpression)
44
+ .map((call) => ({ call, declaration: checker.getTypeAtLocation(call.expression).getCallSignatures()[0]?.getDeclaration() }));
45
+ if (
46
+ calls.some(({ declaration }) => handlerBuilder(declaration))
47
+ || source.statements.some((statement) => ts.isFunctionDeclaration(statement) && boundaryName(statement) === "rpcBoundaryHandlers")
48
+ ) {
49
+ return [];
50
+ }
51
+ return calls.flatMap(({ call, declaration }): readonly Finding[] => {
52
+ const name = boundaryName(declaration);
53
+ return name === undefined
54
+ ? []
55
+ : [
56
+ {
57
+ file: file.path,
58
+ line: source.getLineAndCharacterOfPosition(call.getStart(source)).line + 1,
59
+ message: `Uses ${name} outside a module that builds RPC handlers. Move the boundary into the module calling the RPC group's toLayer, toLayerHandler, toHandlers or of; services must keep their own typed failures.`,
60
+ },
61
+ ];
62
+ });
63
+ });
64
+ }
65
+
66
+ export const rpcBoundaryFindings = Effect.fn("RpcBoundary.rpcBoundaryFindings")(function* (inputs: RuleInputs) {
67
+ const candidates = new Set(inputs.sources.filter((source) => source.text.includes("rpcBoundary")).map((source) => source.path));
68
+ if (candidates.size === 0) {
69
+ return [];
70
+ }
71
+ const graph = yield* importGraph(inputs);
72
+ const importers = new Map<string, string[]>();
73
+ for (const edge of graph.edges) {
74
+ if (edge.to.kind === "file" && !edge.type) {
75
+ const paths = importers.get(edge.to.path) ?? [];
76
+ paths.push(edge.from);
77
+ importers.set(edge.to.path, paths);
78
+ }
79
+ }
80
+ for (const path of candidates) {
81
+ for (const importer of importers.get(path) ?? []) {
82
+ candidates.add(importer);
83
+ }
84
+ }
85
+ return yield* Effect.sync(() => {
86
+ const groups = new Map<string, RuleInputs["sources"][number][]>();
87
+ for (const source of inputs.sources) {
88
+ if (isTestSupport(source.path) || !/\.[cm]?[jt]sx?$/u.test(source.path) || !candidates.has(source.path)) {
89
+ continue;
90
+ }
91
+ const group = /^(?:packages|examples)\/[^/]+/u.exec(source.path)?.[0] ?? ".";
92
+ const sources = groups.get(group) ?? [];
93
+ sources.push(source);
94
+ groups.set(group, sources);
95
+ }
96
+ return [...groups.values()].flatMap((sources) => checkSources({ ...inputs, sources }));
97
+ });
98
+ });
@@ -0,0 +1,38 @@
1
+ import { basename } from "node:path";
2
+ import ts from "typescript";
3
+ import type { BodyReference } from "./bodyReferences.ts";
4
+
5
+ function nativeService({ module, path }: BodyReference): boolean {
6
+ const name = path[0];
7
+ return name !== undefined && (module === "effect" || module.startsWith("effect/")) && (basename(module) === name || path[1] === name);
8
+ }
9
+
10
+ function member(value: BodyReference): boolean {
11
+ return value.kind === "service" ? value.path.length > 0 : nativeService(value) && value.path.length > (value.path[0] === value.path[1] ? 2 : 1);
12
+ }
13
+
14
+ function outerExpression(node: ts.Node): ts.Node {
15
+ let use = node;
16
+ while (ts.isParenthesizedExpression(use.parent) || ts.isAsExpression(use.parent) || ts.isNonNullExpression(use.parent)) {
17
+ use = use.parent;
18
+ }
19
+ return use;
20
+ }
21
+
22
+ function readsService(called: BodyReference | undefined): boolean {
23
+ return called !== undefined && !called.path.at(-1)?.startsWith("provide") && ["effect", "effect/Effect", "effect/Context"].includes(called.module);
24
+ }
25
+
26
+ export function serviceAccess(node: ts.Node, value: BodyReference, reference: (node: ts.Node) => BodyReference | undefined): boolean {
27
+ if (!(value.kind === "service" || (value.kind === "import" && nativeService(value)))) {
28
+ return false;
29
+ }
30
+ if (value.kind === "service" && value.path.at(-1) === "of") {
31
+ return false;
32
+ }
33
+ if (member(value)) {
34
+ return true;
35
+ }
36
+ const use = outerExpression(node);
37
+ return ts.isYieldExpression(use.parent) || (ts.isCallExpression(use.parent) && readsService(reference(use.parent.expression)));
38
+ }
@@ -0,0 +1,5 @@
1
+ import { Config, ConfigProvider, Effect } from "effect";
2
+
3
+ export function vitePort(name: string, fallback: number): number {
4
+ return Effect.runSync(Config.Int(name).pipe(Config.withDefault(fallback)).parse(ConfigProvider.fromEnv()));
5
+ }
@@ -1,12 +1,22 @@
1
- export function appViteConfig<TPlugin>(plugins: TPlugin[], conditions: string[] = [], proxyPaths: string[] = []) {
1
+ import { vitePort } from "#adapters/vitePort.ts";
2
+
3
+ export function appViteConfig<TPlugin>(
4
+ plugins: TPlugin[],
5
+ conditions: string[] = [],
6
+ proxyPaths: string[] = [],
7
+ { webPort = vitePort("VITE_PORT", 5173), apiPort = vitePort("PORT", 3000) }: { webPort?: number; apiPort?: number } = {},
8
+ ) {
2
9
  return {
3
10
  plugins,
4
11
  resolve: { conditions, tsconfigPaths: true },
5
12
  server: {
6
13
  host: "127.0.0.1",
7
- port: 5173,
14
+ port: webPort,
8
15
  proxy: Object.fromEntries(
9
- ["/api/auth", "/rpc", ...proxyPaths].map((path) => [path, { changeOrigin: false, target: "http://127.0.0.1:3000", ws: path === "/rpc" }]),
16
+ ["/api/auth", "/rpc", ...proxyPaths].map((path) => [
17
+ path,
18
+ { changeOrigin: false, target: `http://127.0.0.1:${apiPort}`, ws: path === "/rpc" },
19
+ ]),
10
20
  ),
11
21
  strictPort: true,
12
22
  },
@@ -10,6 +10,7 @@ import { docsUnversionedInstalls } from "#rules/docs/unversionedInstalls.ts";
10
10
  import { adapterImports } from "#rules/effect/adapterImports.ts";
11
11
  import { effectBoundary } from "#rules/effect/boundary.ts";
12
12
  import { failureHonesty } from "#rules/effect/failureHonesty.ts";
13
+ import { rpcBoundary } from "#rules/effect/rpcBoundary.ts";
13
14
  import { serviceDefinitionAssembly } from "#rules/effect/serviceDefinitionAssembly.ts";
14
15
  import { serviceIds } from "#rules/effect/serviceIds.ts";
15
16
  import { serviceParameters } from "#rules/effect/serviceParameters.ts";
@@ -39,6 +40,7 @@ import { importedChangeKeys } from "#rules/stories/importedChangeKeys.ts";
39
40
  import { namedValues } from "#rules/stories/namedValues.ts";
40
41
  import { noExpect } from "#rules/stories/noExpect.ts";
41
42
  import { noLimitImports } from "#rules/stories/noLimitImports.ts";
43
+ import { positiveRefusals } from "#rules/stories/positiveRefusals.ts";
42
44
  import { queueReadouts } from "#rules/stories/queueReadouts.ts";
43
45
  import { queueSql } from "#rules/stories/queueSql.ts";
44
46
  import { storySetup } from "#rules/stories/setup.ts";
@@ -68,6 +70,7 @@ export const builtInRules = [
68
70
  adapterImports,
69
71
  effectBoundary,
70
72
  failureHonesty,
73
+ rpcBoundary,
71
74
  serviceDefinitionAssembly,
72
75
  serviceIds,
73
76
  serviceParameters,
@@ -126,6 +129,7 @@ export const builtInRules = [
126
129
  namedValues,
127
130
  noLimitImports,
128
131
  domainOutcomes,
132
+ positiveRefusals,
129
133
  groupedOutcomes,
130
134
  waitOnEvents,
131
135
  ] as const;
@@ -0,0 +1,10 @@
1
+ import { rpcBoundaryFindings } from "#adapters/rpcBoundaryFindings.ts";
2
+ import { defineRule } from "#rule.ts";
3
+
4
+ export const rpcBoundary = defineRule({
5
+ check: rpcBoundaryFindings,
6
+ description:
7
+ "Apply rpcBoundary and rpcBoundaryHandlers only in modules building native RPC handlers; services keep their typed failures. Test-support kits and the handler boundary implementation are accepted. No config exceptions or inline suppressions; baseline existing debt.",
8
+ id: "effect/rpc-boundary",
9
+ registrable: false,
10
+ });
@@ -1,5 +1,6 @@
1
1
  import { Effect } from "effect";
2
2
  import ts from "typescript";
3
+ import { engineEscapes } from "#adapters/engineEscapes.ts";
3
4
  import { importGraph } from "#imports/graph.ts";
4
5
  import { packageOf } from "#imports/workspace.ts";
5
6
  import { testName } from "#naming/testName.ts";
@@ -40,18 +41,33 @@ export const storyEngine = defineRule({
40
41
  if (source === undefined) {
41
42
  return [];
42
43
  }
43
- const stories = nodesWhere(
44
- source,
45
- (node) => ts.isCallExpression(node) && origins.origin(node.expression) === "story" && ignoresEngine(node, origins.checker),
44
+ const stories = nodesWhere(source, (node) => ts.isCallExpression(node) && origins.origin(node.expression) === "story").filter(
45
+ ts.isCallExpression,
46
46
  );
47
- return stories.map((story) => ({
48
- file: file.path,
49
- line: source.getLineAndCharacterOfPosition(story.getStart(source)).line + 1,
50
- message: `Declares a story without traits whose body never references its verbs, its story or its test context. Drive the engine through one of the kit's verbs or traits. If the code under test is pure, move the test to ${name.stem}.unit.${name.kind}${name.extension}, which reaches no engine and mocks nothing. See https://github.com/ShivaeDev/platform/tree/main/packages/quality#story-tests.`,
51
- }));
47
+ return stories.flatMap((story) => {
48
+ const body = story.arguments[2];
49
+ const checked = body === undefined ? { drivesSubject: false, escapes: [] } : engineEscapes(body, origins.checker, file.path);
50
+ if (checked.escapes.length > 0) {
51
+ return checked.escapes.map(({ node, reason }) => ({
52
+ file: file.path,
53
+ line: source.getLineAndCharacterOfPosition(node.getStart(source)).line + 1,
54
+ message: `${reason} in a spec body. Move engine access and wiring into the story kit's verbs or traits in test-support/. Keep expectations in the spec. Constructor tests may call the constructor in their colocated module; native SQL transaction blocks may wrap kit verbs. See https://github.com/ShivaeDev/platform/tree/main/packages/quality#story-tests.`,
55
+ }));
56
+ }
57
+ if (checked.drivesSubject || !ignoresEngine(story, origins.checker)) {
58
+ return [];
59
+ }
60
+ return [
61
+ {
62
+ file: file.path,
63
+ line: source.getLineAndCharacterOfPosition(story.getStart(source)).line + 1,
64
+ message: `Declares a story without traits whose body never references its verbs, its story or its test context. Drive the engine through one of the kit's verbs or traits. If the code under test is pure, move the test to ${name.stem}.unit.${name.kind}${name.extension}, which reaches no engine and mocks nothing. See https://github.com/ShivaeDev/platform/tree/main/packages/quality#story-tests.`,
65
+ },
66
+ ];
67
+ });
52
68
  });
53
69
  }),
54
70
  description:
55
- "A story without traits references its kit's verbs, its story or its test context; pure tests belong in .unit files that reach no engine and mock nothing.",
71
+ "Story bodies use their kit instead of raw services, story.engine or production wiring; constructor subjects and native transaction blocks remain explicit.",
56
72
  id: "tests/story-engine",
57
73
  });
@@ -0,0 +1,17 @@
1
+ import { Effect } from "effect";
2
+ import { positiveRefusalFindings } from "#adapters/positiveRefusalFindings.ts";
3
+ import { importGraph } from "#imports/graph.ts";
4
+ import { defineRule } from "#rule.ts";
5
+ import { storyOrigins } from "./origins.ts";
6
+ import { storySources } from "./sources.ts";
7
+
8
+ export const positiveRefusals = defineRule({
9
+ check: Effect.fn("Stories.check")(function* (inputs) {
10
+ const files = yield* storySources(inputs);
11
+ const graph = yield* importGraph(inputs);
12
+ const origins = storyOrigins(inputs, graph);
13
+ return files.flatMap((file) => positiveRefusalFindings(file, origins));
14
+ }),
15
+ description: "Negative outcome comparisons in a story need a positive assertion of the expected refusal on the same outcome.",
16
+ id: "tests/positive-refusals",
17
+ });
@@ -1,8 +1,9 @@
1
1
  import ts from "typescript";
2
+ import { storyBody } from "#adapters/bodyReferences.ts";
2
3
 
3
4
  export function storyUsesInputs(body: ts.Expression, checker: ts.TypeChecker): boolean | undefined {
4
- const declaration = checker.getTypeAtLocation(body).getCallSignatures()[0]?.declaration;
5
- if (declaration === undefined || !("body" in declaration) || declaration.body === undefined) {
5
+ const declaration = storyBody(body, checker);
6
+ if (declaration?.body === undefined) {
6
7
  return undefined;
7
8
  }
8
9
  const inputs = new Set<ts.Symbol>();