description-d1.test.ts
This file
Path
packages/project/test/description-d1.test.ts
104 lines · 3.2 KiB · TypeScript
Last changed by
Merge redesign into mainoscabrielbefore adoptioncaedd21
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105import { describe, expect, it } from "@effect/vitest";import { ProjectDescription } from "@repo/protocol/domain/front-page";import { ProjectName } from "@repo/protocol/ids";import { DateTime, Effect, Layer, Logger, Schema } from "effect";import { SqlClient } from "effect/sql/SqlClient";
import { ProjectDescriptions } from "../src/description.ts";import type { KeptDescription } from "../src/description.ts";import { ProjectDescriptionsD1 } from "../src/projection-d1/descriptions.ts";import { ProjectDescriptionsDdl } from "../src/projection-d1/tables.ts";import { descriptionsMigration, migrated, migration,} from "./projection-d1.fixtures.ts";
const datekit = ProjectName.make("alice/datekit");
const burrow = ProjectName.make("alice/burrow");
const at = DateTime.makeUnsafe("2026-10-12T12:00:00Z");
const helpers: KeptDescription = { description: Schema.decodeSync(ProjectDescription)("Date helpers."), landedAt: at, landings: 12,};
const keptFor = (projects: readonly ProjectName[]) => Effect.flatMap(Effect.service(ProjectDescriptions), (store) => store.get(projects) );
const keep = (entries: readonly (readonly [ProjectName, KeptDescription])[]) => Effect.flatMap(Effect.service(ProjectDescriptions), (store) => store.put(new Map(entries)) );
describe("the kept descriptions in D1", () => { it.effect("reads the migration the platform's D1 applies", () => Effect.gen(function* () { expect(yield* migration(descriptionsMigration)).toBe( ProjectDescriptionsDdl ); }) );
it.effect( "keeps each project's description and count of landings beside the landing it read, and a README with none", () => Effect.gen(function* () { yield* keep([ [datekit, helpers], [burrow, { landedAt: at, landings: 1 }], ]);
expect(yield* keptFor([datekit, burrow])).toStrictEqual( new Map([ [datekit, helpers], [burrow, { landedAt: at, landings: 1 }], ]) ); }).pipe( Effect.provide( ProjectDescriptionsD1.pipe( Layer.provideMerge(migrated([descriptionsMigration])) ) ) ) );
it.effect("reads a kept row that no longer decodes as a miss", () => Effect.gen(function* () { const sql = yield* SqlClient;
yield* sql`INSERT INTO project_descriptions (project, landed_at, description, landings) VALUES ('alice__datekit', '2026-10-12T12:00:00.000Z', ${"x".repeat(161)}, 3), ('alice__burrow', '2026-10-12T12:00:00.000Z', NULL, -1)`;
expect(yield* keptFor([datekit, burrow])).toStrictEqual(new Map()); }).pipe( Effect.provide( ProjectDescriptionsD1.pipe( Layer.provideMerge(migrated([descriptionsMigration])) ) ) ) );
it.effect("misses, never fails, before the migration has run", () => Effect.gen(function* () { yield* keep([[datekit, helpers]]);
expect(yield* keptFor([datekit])).toStrictEqual(new Map()); }).pipe( Effect.provide( Layer.merge( ProjectDescriptionsD1.pipe(Layer.provideMerge(migrated([]))), Logger.layer([]) ) ) ) );});