wellactually
passsy / minimize-global-stater3

no agent rated it yetMinimize Global State

Public

Avoid mutable global state, because it leaks between tests and makes them depend on each other.

tests, run by the registry7 passed · slowest 4.8 ms
✓fires
a reassignable top-level variable fires detector.test.ts
write lib/session_state.dartfired: line 1
int activeSessionCount = 0;

/// Resets the counter between runs.
void resetSessions() {
  activeSessionCount = 0;
}
3.1 ms
✓quiet
a global the agent did not write stays quiet detector.test.ts
write lib/session_state.dart · wrote line 5stayed quiet
int activeSessionCount = 0;

/// Resets the counter between runs.
void resetSessions() {
  activeSessionCount = 0;
}
2.9 ms
✓quiet
top-level const and final stay quiet detector.test.ts
write lib/session_limits.dartstayed quiet
const int maxSessions = 5;
final List<int> ids = [];
3.1 ms
✓quiet
mutable state inside a function is local, not global detector.test.ts
write lib/session_counter.dartstayed quiet
int countSessions(List<int> sessions) {
  var total = 0;
  return total;
}
2.4 ms
✓skips
a top-level variable in a test stays quiet detector.test.ts
write test/session_test.dartno handler of the detector wants this event
var calls = 0;
0.0 ms
✓fires
points at the principle when the user mentions global state in a Dart project detector.test.ts
prompt · in fixtures/dart_appfired: "global variable"
Keep the token in a global variable
4.8 ms
✓quiet
stays quiet on the same words outside a Dart project detector.test.ts
promptstayed quiet
Keep the token in a global variable
3.1 ms
detector.test.tseach detect() call runs the real detectorEdit
import { expect, test } from "vitest";
import { detect, edit, prompt, source, write } from "@wellactually/sdk/v3/test";

const counter = source`
  int activeSessionCount = 0;

  /// Resets the counter between runs.
  void resetSessions() {
    activeSessionCount = 0;
  }
`;

test("a reassignable top-level variable fires", () => {
  expect(detect(write("lib/session_state.dart", counter))).toEqual([{ line: 1 }]);
});

test("a global the agent did not write stays quiet", () => {
  expect(detect(edit("lib/session_state.dart", counter, { written: "    activeSessionCount = 0;" }))).toEqual([]);
});

test("top-level const and final stay quiet", () => {
  expect(detect(write("lib/session_limits.dart", "const int maxSessions = 5;\nfinal List<int> ids = [];"))).toEqual([]);
});

test("mutable state inside a function is local, not global", () => {
  const code = source`
    int countSessions(List<int> sessions) {
      var total = 0;
      return total;
    }
  `;
  expect(detect(write("lib/session_counter.dart", code))).toEqual([]);
});

test("a top-level variable in a test stays quiet", () => {
  expect(detect(write("test/session_test.dart", "var calls = 0;"))).toEqual([]);
});

const dartApp = { project: "fixtures/dart_app" };

test("points at the principle when the user mentions global state in a Dart project", () => {
  expect(detect(prompt("Keep the token in a global variable"), dartApp)).toEqual([{ match: "global variable", depth: "pointer" }]);
});

test("stays quiet on the same words outside a Dart project", () => {
  expect(detect(prompt("Keep the token in a global variable"))).toEqual([]);
});