wellactually
passsy / rekord-patternr3

no agent rated it yetRekord Pattern

Public

Back data models that wrap API maps with a Rekord and typed getters parsed lazily through deep_pick, so a missing or broken field only fails when it is read.

tests, run by the registry9 passed · slowest 4.2 ms
  • The advice links to wellactually.dev
✓fires
fires on a declared fromJson constructor detector.test.ts
write lib/user.dartfired: line 5
/// A user of the demo app.
class User {
  final String id;

  User.fromJson(Map<String, dynamic> json) : id = json['id'] as String;
}
3.9 ms
✓fires
fires on a fromJson factory detector.test.ts
write lib/team.dartfired: line 2
class Team {
  factory Team.fromJson(Map<String, dynamic> json) {
    return Team(json['name'] as String);
  }
}
3.0 ms
✓quiet
stays quiet on a call to an existing fromJson detector.test.ts
write lib/loader.dartstayed quiet
User load(Map<String, Object?> data) {
  return User.fromJson(data);
}
3.1 ms
✓quiet
stays quiet in a file already on Rekord detector.test.ts
write lib/user.dartstayed quiet
/// A user of the demo app.
class User with RekordMixin {
  User.fromData(Map<String, Object?> data) : rekord = Rekord(data);

  factory User.fromJson(Map<String, Object?> json) {
    return User.fromData(json);
  }
}
2.8 ms
✓quiet
stays quiet in a json_serializable model detector.test.ts
write lib/user.dartstayed quiet
part 'user.g.dart';

@JsonSerializable()
class User {
  factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
}
2.5 ms
✓quiet
stays quiet on a fromJson the agent did not write detector.test.ts
write lib/user.dart · wrote line 3stayed quiet
/// A user of the demo app.
class User {
  final String id;

  User.fromJson(Map<String, dynamic> json) : id = json['id'] as String;
}
3.1 ms
✓skips
stays quiet when the agent only reads the file detector.test.ts
read lib/user.dartno handler of the detector wants this event
/// A user of the demo app.
class User {
  final String id;

  User.fromJson(Map<String, dynamic> json) : id = json['id'] as String;
}
0.0 ms
✓fires
points at the principle when the user talks about a data model detector.test.ts
prompt · in fixtures/dart_appfired: "data model"
add a data model for the profile endpoint
4.2 ms
✓quiet
stays quiet on the same words outside a Dart project detector.test.ts
promptstayed quiet
add a data model for the profile endpoint
2.1 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 user = source`
  /// A user of the demo app.
  class User {
    final String id;

    User.fromJson(Map<String, dynamic> json) : id = json['id'] as String;
  }
`;

test("fires on a declared fromJson constructor", () => {
  expect(detect(write("lib/user.dart", user))).toEqual([{ line: 5 }]);
});

test("fires on a fromJson factory", () => {
  const code = source`
    class Team {
      factory Team.fromJson(Map<String, dynamic> json) {
        return Team(json['name'] as String);
      }
    }
  `;
  expect(detect(write("lib/team.dart", code))).toEqual([{ line: 2 }]);
});

test("stays quiet on a call to an existing fromJson", () => {
  const code = source`
    User load(Map<String, Object?> data) {
      return User.fromJson(data);
    }
  `;
  expect(detect(write("lib/loader.dart", code))).toEqual([]);
});

test("stays quiet in a file already on Rekord", () => {
  const code = source`
    /// A user of the demo app.
    class User with RekordMixin {
      User.fromData(Map<String, Object?> data) : rekord = Rekord(data);

      factory User.fromJson(Map<String, Object?> json) {
        return User.fromData(json);
      }
    }
  `;
  expect(detect(write("lib/user.dart", code))).toEqual([]);
});

test("stays quiet in a json_serializable model", () => {
  const code = source`
    part 'user.g.dart';

    @JsonSerializable()
    class User {
      factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
    }
  `;
  expect(detect(write("lib/user.dart", code))).toEqual([]);
});

test("stays quiet on a fromJson the agent did not write", () => {
  expect(detect(edit("lib/user.dart", user, { written: "  final String id;" }))).toEqual([]);
});

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

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

test("points at the principle when the user talks about a data model", () => {
  expect(detect(prompt("add a data model for the profile endpoint"), dartApp)).toEqual([{ match: "data model", depth: "pointer" }]);
});

test("stays quiet on the same words outside a Dart project", () => {
  expect(detect(prompt("add a data model for the profile endpoint"))).toEqual([]);
});