wellactually
passsy / use-clockr4

no agent rated it yetUse clock.now() instead of DateTime.now()

Public

Read the time through clock.now() from package:clock, so tests can control it with fakeAsync or withClock.

tests, run by the registry10 passed · slowest 1.2 ms
✓fires
fires on a DateTime.now() the agent writes detector.test.ts
write lib/session.dartfired: line 2
class Session {
  bool get expired => DateTime.now().isAfter(validUntil);
  int retries = 3;
}
1.2 ms
✓quiet
stays quiet on a DateTime.now() that was already there detector.test.ts
write lib/session.dart · wrote line 3stayed quiet
class Session {
  bool get expired => DateTime.now().isAfter(validUntil);
  int retries = 3;
}
1.1 ms
✓skips
stays quiet when the agent only reads the file detector.test.ts
read lib/session.dartno handler of the detector wants this event
class Session {
  bool get expired => DateTime.now().isAfter(validUntil);
  int retries = 3;
}
0.0 ms
✓quiet
stays quiet on DateTime.now in a comment detector.test.ts
write lib/hint.dartstayed quiet
// Do not call DateTime.now() here.
final now = clock.now();
1.1 ms
✓skips
stays quiet in generated code detector.test.ts
write lib/session.g.dartno handler of the detector wants this event
final created = DateTime.now();
0.0 ms
✓fires
points at the principle on a fakeAsync the agent writes into a test detector.test.ts
write test/session_test.dartfired: line 3 "fakeAsync"
void main() {
  test('expires after an hour', () {
    fakeAsync((async) {
      final session = Session.start();
      async.elapse(const Duration(hours: 1));
      expect(session.expired, isTrue);
    });
  });
}
1.1 ms
✓fires
points at the principle on a withClock the agent writes into a test detector.test.ts
write test/session_test.dartfired: line 1 "withClock"
  withClock(Clock.fixed(DateTime(2026)), () => expect(session.expired, isFalse));
1.1 ms
✓quiet
stays quiet on a fakeAsync outside a test detector.test.ts
write lib/clock_helpers.dartstayed quiet
final run = fakeAsync;
void go() => fakeAsync((async) {});
1.1 ms
✓quiet
stays quiet on a fakeAsync that was already in the test detector.test.ts
write test/session_test.dart · wrote line 6stayed quiet
void main() {
  test('expires after an hour', () {
    fakeAsync((async) {
      final session = Session.start();
      async.elapse(const Duration(hours: 1));
      expect(session.expired, isTrue);
    });
  });
}
1.1 ms
✓skips
stays quiet when the user only mentions fakeAsync detector.test.ts
prompt · in fixtures/dart_appno handler of the detector wants this event
The expiry test is flaky, can you wrap it in fakeAsync?
0.0 ms
detector.test.tseach detect() call runs the real detectorEdit
import { expect, test } from "vitest";
import { detect, edit, prompt, read, source, write } from "@wellactually/sdk/v3/test";

const session = source`
  class Session {
    bool get expired => DateTime.now().isAfter(validUntil);
    int retries = 3;
  }
`;

test("fires on a DateTime.now() the agent writes", () => {
  expect(detect(write("lib/session.dart", session))).toEqual([{ line: 2 }]);
});

test("stays quiet on a DateTime.now() that was already there", () => {
  expect(detect(edit("lib/session.dart", session, { written: "  int retries = 3;" }))).toEqual([]);
});

test("stays quiet when the agent only reads the file", () => {
  expect(detect(read("lib/session.dart", session))).toEqual([]);
});

test("stays quiet on DateTime.now in a comment", () => {
  const code = source`
    // Do not call DateTime.now() here.
    final now = clock.now();
  `;
  expect(detect(write("lib/hint.dart", code))).toEqual([]);
});

test("stays quiet in generated code", () => {
  expect(detect(write("lib/session.g.dart", "final created = DateTime.now();"))).toEqual([]);
});

const expiry = source`
  void main() {
    test('expires after an hour', () {
      fakeAsync((async) {
        final session = Session.start();
        async.elapse(const Duration(hours: 1));
        expect(session.expired, isTrue);
      });
    });
  }
`;

test("points at the principle on a fakeAsync the agent writes into a test", () => {
  expect(detect(write("test/session_test.dart", expiry))).toEqual([{ line: 3, match: "fakeAsync", depth: "pointer" }]);
});

test("points at the principle on a withClock the agent writes into a test", () => {
  const code = "  withClock(Clock.fixed(DateTime(2026)), () => expect(session.expired, isFalse));";
  expect(detect(write("test/session_test.dart", code))).toEqual([{ line: 1, match: "withClock", depth: "pointer" }]);
});

test("stays quiet on a fakeAsync outside a test", () => {
  expect(detect(write("lib/clock_helpers.dart", "final run = fakeAsync;\nvoid go() => fakeAsync((async) {});"))).toEqual([]);
});

test("stays quiet on a fakeAsync that was already in the test", () => {
  expect(detect(edit("test/session_test.dart", expiry, { written: "        expect(session.expired, isTrue);" }))).toEqual([]);
});

test("stays quiet when the user only mentions fakeAsync", () => {
  expect(detect(prompt("The expiry test is flaky, can you wrap it in fakeAsync?"), { project: "fixtures/dart_app" })).toEqual([]);
});