code-store.test.ts
This file
Path
packages/project/test/code-store.test.ts
146 lines · 4.1 KiB · TypeScript
Last changed by
Merge build/comments-project into mainoscabrielbefore adoption49efd2a
History of this fileTake it
Name the ask an opening opened, say why a policy has not evaluated, and give code history a durable storeoscabrielbefore adoption51f046c
lines 1–146why, one step back1import { describe, expect, it } from "@effect/vitest";2import { Effect, Exit, Layer, Logger, Option } from "effect";34import { CodeStore } from "../src/code-store.ts";5import { CodeStoreR2 } from "../src/code-store/binding.ts";6import type { BucketLike } from "../src/code-store/binding.ts";7import { CodeStoreCounts, CodeStoreMemory } from "../src/code-store/memory.ts";89const key = "datekit/why/v1/1111111111111111111111111111111111111111/main/ab";1011/** A key one byte past R2's limit of 1,024. */12const tooLong = "k".repeat(1025);1314const putThenGet = Effect.gen(function* () {15 const store = yield* CodeStore;1617 yield* store.put(key, '{"lines":[]}');1819 return yield* store.get(key);20});2122const bucketOf = (fails: {23 readonly get?: boolean;24 readonly put?: boolean;25}) => {26 const objects = new Map<string, string>();2728 const bucket: BucketLike = {29 get: (name) =>30 fails.get === true31 ? Promise.reject(new Error("R2 is down"))32 : Promise.resolve(33 Option.match(Option.fromUndefinedOr(objects.get(name)), {34 onNone: () => null,35 onSome: (text) => ({ text: () => Promise.resolve(text) }),36 })37 ),38 put: (name, value) => {39 if (fails.put === true) {40 return Promise.reject(new Error("R2 is down"));41 }4243 objects.set(name, value);4445 return Promise.resolve({ key: name });46 },47 };4849 return bucket;50};5152const logged = <A, E>(effect: Effect.Effect<A, E>) =>53 Effect.gen(function* () {54 const messages: unknown[] = [];5556 const capture = Logger.make(({ message }) => {57 messages.push(message);58 });5960 const exit = yield* Effect.exit(61 effect.pipe(Effect.provide(Logger.layer([capture])))62 );6364 return { exit, messages };65 });6667describe("the code store", () => {68 it.effect("stores nothing until a host gives it a store", () =>69 Effect.map(putThenGet, (got) => {70 expect(got).toStrictEqual(Option.none());71 })72 );7374 it.effect("in memory, gives back what was put, and counts each call", () =>75 Effect.gen(function* () {76 const got = yield* putThenGet;7778 const missed = yield* Effect.flatMap(CodeStore, (store) =>79 store.get(`${key}/other`)80 );8182 expect({83 counts: yield* CodeStoreCounts.use(({ counts }) => counts),84 got,85 missed,86 }).toStrictEqual({87 counts: { gets: 2, puts: 1 },88 got: Option.some('{"lines":[]}'),89 missed: Option.none(),90 });91 }).pipe(Effect.provide(CodeStoreMemory))92 );9394 it.effect("over R2, gives back what was put", () =>95 Effect.map(96 putThenGet.pipe(Effect.provide(CodeStoreR2(bucketOf({})))),97 (got) => {98 expect(got).toStrictEqual(Option.some('{"lines":[]}'));99 }100 )101 );102103 it.effect("over R2, reads a failed get as a miss and logs it", () =>104 Effect.map(105 logged(106 putThenGet.pipe(Effect.provide(CodeStoreR2(bucketOf({ get: true }))))107 ),108 ({ exit, messages }) => {109 expect(exit).toStrictEqual(Exit.succeed(Option.none()));110 expect(messages.length).toBeGreaterThan(0);111 }112 )113 );114115 it.effect("over R2, never fails its caller on a failed put", () =>116 Effect.map(117 logged(118 Effect.flatMap(CodeStore, (store) => store.put(key, "{}")).pipe(119 Effect.provide(CodeStoreR2(bucketOf({ put: true })))120 )121 ),122 ({ exit, messages }) => {123 expect(exit).toStrictEqual(Exit.void);124 expect(messages.length).toBeGreaterThan(0);125 }126 )127 );128129 it.effect.each([130 ["the default", Layer.empty],131 ["memory", CodeStoreMemory],132 ["R2", CodeStoreR2(bucketOf({}))],133 ] as const)(134 "%s refuses a key over 1,024 bytes as a defect, so callers hash paths",135 ([, layer]) =>136 Effect.gen(function* () {137 const store = yield* CodeStore;138139 expect([140 Exit.hasDies(yield* Effect.exit(store.get(tooLong))),141 Exit.hasDies(yield* Effect.exit(store.put(tooLong, "{}"))),142 Exit.hasDies(yield* Effect.exit(store.get("k".repeat(1024)))),143 ]).toStrictEqual([true, true, false]);144 }).pipe(Effect.provide(layer))145 );146});