passsy / minimize-global-stater3
Minimize Global State
PublicAvoid 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
✓fires3.1 ms
a reassignable top-level variable fires
write lib/session_state.dartfired: line 1
int activeSessionCount = 0;
/// Resets the counter between runs.
void resetSessions() {
activeSessionCount = 0;
}✓quiet2.9 ms
a global the agent did not write stays quiet
write lib/session_state.dart · wrote line 5stayed quiet
int activeSessionCount = 0;
/// Resets the counter between runs.
void resetSessions() {
activeSessionCount = 0;
}✓quiet3.1 ms
top-level const and final stay quiet
write lib/session_limits.dartstayed quiet
const int maxSessions = 5;
final List<int> ids = [];✓quiet2.4 ms
mutable state inside a function is local, not global
write lib/session_counter.dartstayed quiet
int countSessions(List<int> sessions) {
var total = 0;
return total;
}✓skips0.0 ms
a top-level variable in a test stays quiet
write test/session_test.dartno handler of the detector wants this event
var calls = 0;✓fires4.8 ms
points at the principle when the user mentions global state in a Dart project
prompt · in fixtures/dart_appfired: "global variable"
Keep the token in a global variable✓quiet3.1 ms
stays quiet on the same words outside a Dart project
promptstayed quiet
Keep the token in a global variabledetector.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([]);
});