guards.test.ts
This file
Path
packages/visibility/test/guards.test.ts
254 lines · 7.4 KiB · TypeScript
Last changed by
Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 1–45why, one step back1import { describe, expect, it } from "@effect/vitest";2import type { Change } from "@repo/git/tree-diff";3import { BlobSha, RepoPath } from "@repo/protocol/ids";4import {5 LayerContentInPublicTree,6 PrivatePathInPublicTree,7} from "@repo/protocol/refusal";8import { Option } from "effect";910import { privatePaths, writeDown } from "../src/guards.ts";1112const blob = (digit: string) => BlobSha.make(digit.repeat(40));1314const path = (at: string) => RepoPath.make(at);1516const added = (at: string, after: BlobSha): Change => ({17 after,18 path: path(at),19 status: "added",20});2122const sealed = "checks/held-back/export/x.test.ts";2324const layerPaths: ReadonlySet<RepoPath> = new Set([25 path(sealed),26 path("checks/held-back/parser/y.test.ts"),27]);2829const push: readonly Change[] = [30 added("src/a.ts", blob("1")),31 added(sealed, blob("2")),32];3334const pathsOf = (refusal: Option.Option<{ readonly paths: unknown }>) =>35 Option.map(refusal, ({ paths }) => paths);3637describe("the private-path guard", () => {38 it("refuses a push that adds a path from a layer's path set", () => {39 expect(40 privatePaths({ changes: push, layerPaths, pusherCanRead: true })41 ).toStrictEqual(42 Option.some(new PrivatePathInPublicTree({ paths: [path(sealed)] }))43 );44 });45Drop the private build plan's citations from the code, and fail verify if one comes backoscabrielbefore adoptiona29cef6
line 46why, one step back46 // The refusal names no layer path to someone who cannotMerge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 47–179why, one step back47 // read the layer. It names every path they added or modified, all of them48 // their own, so which one collides stays unsaid.49 it("names every path a non-reader added or modified, not the colliding ones", () => {50 const changes: readonly Change[] = [51 ...push,52 {53 after: blob("4"),54 before: blob("3"),55 path: path("README.md"),56 status: "modified",57 },58 { before: blob("5"), path: path("docs/old.md"), status: "deleted" },59 ];6061 expect(62 pathsOf(privatePaths({ changes, layerPaths, pusherCanRead: false }))63 ).toStrictEqual(Option.some(["README.md", sealed, "src/a.ts"]));64 });6566 it("names exactly the colliding paths to a reader of the layer", () => {67 expect(68 pathsOf(privatePaths({ changes: push, layerPaths, pusherCanRead: true }))69 ).toStrictEqual(Option.some([sealed]));70 });7172 it("refuses a modified file at a layer path", () => {73 const changes: readonly Change[] = [74 {75 after: blob("2"),76 before: blob("1"),77 path: path(sealed),78 status: "modified",79 },80 ];8182 expect(83 pathsOf(privatePaths({ changes, layerPaths, pusherCanRead: true }))84 ).toStrictEqual(Option.some([sealed]));85 });8687 it("lets through changes outside every layer's path set", () => {88 const changes: readonly Change[] = [89 added("src/a.ts", blob("1")),90 added("checks/held-back/export/x.test.ts.bak", blob("2")),91 added("checks/held-back/export", blob("3")),92 ];9394 expect(95 privatePaths({ changes, layerPaths, pusherCanRead: false })96 ).toStrictEqual(Option.none());97 expect(98 privatePaths({ changes, layerPaths, pusherCanRead: true })99 ).toStrictEqual(Option.none());100 });101102 it("never refuses a deletion", () => {103 const changes: readonly Change[] = [104 { before: blob("1"), path: path(sealed), status: "deleted" },105 ];106107 expect(108 privatePaths({ changes, layerPaths, pusherCanRead: false })109 ).toStrictEqual(Option.none());110 });111112 it("refuses a path under a folder in a layer's path set", () => {113 const changes: readonly Change[] = [added(sealed, blob("2"))];114115 expect(116 pathsOf(117 privatePaths({118 changes,119 layerPaths: new Set([path("checks/held-back")]),120 pusherCanRead: true,121 })122 )123 ).toStrictEqual(Option.some([sealed]));124 });125126 it("is total over empty inputs", () => {127 expect(128 privatePaths({ changes: [], layerPaths, pusherCanRead: false })129 ).toStrictEqual(Option.none());130 expect(131 privatePaths({132 changes: push,133 layerPaths: new Set(),134 pusherCanRead: false,135 })136 ).toStrictEqual(Option.none());137 });138139 it("names paths sorted and once, whatever the order of the changes", () => {140 const changes: readonly Change[] = [141 added("z.ts", blob("1")),142 added(sealed, blob("2")),143 added("a.ts", blob("3")),144 added("z.ts", blob("1")),145 ];146147 const reversed = changes.toReversed();148149 expect(150 pathsOf(privatePaths({ changes, layerPaths, pusherCanRead: false }))151 ).toStrictEqual(Option.some(["a.ts", sealed, "z.ts"]));152 expect(153 privatePaths({ changes: reversed, layerPaths, pusherCanRead: false })154 ).toStrictEqual(155 privatePaths({ changes, layerPaths, pusherCanRead: false })156 );157 });158});159160describe("the write-down guard", () => {161 const layerBlobs: ReadonlySet<BlobSha> = new Set([blob("a"), blob("b")]);162163 const copied: readonly Change[] = [164 added("src/a.ts", blob("1")),165 added("test/export.test.ts", blob("a")),166 ];167168 it("refuses an added blob the layer holds, for a reader of the layer", () => {169 expect(170 writeDown({ changes: copied, layerBlobs, readerCanReadLayer: true })171 ).toStrictEqual(172 Option.some(173 new LayerContentInPublicTree({174 paths: [path("test/export.test.ts")],175 })176 )177 );178 });179Drop the private build plan's citations from the code, and fail verify if one comes backoscabrielbefore adoptiona29cef6
line 180why, one step back180 // On someone who cannot read the layer it would be an oracleMerge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 181–254why, one step back181 // for the layer's content, so it never runs.182 it("never refuses someone who cannot read the layer, even on an exact match", () => {183 expect(184 writeDown({ changes: copied, layerBlobs, readerCanReadLayer: false })185 ).toStrictEqual(Option.none());186 });187188 it("counts a modified file whose new blob the layer holds as added", () => {189 const changes: readonly Change[] = [190 {191 after: blob("b"),192 before: blob("1"),193 path: path("test/parser.test.ts"),194 status: "modified",195 },196 ];197198 expect(199 pathsOf(writeDown({ changes, layerBlobs, readerCanReadLayer: true }))200 ).toStrictEqual(Option.some(["test/parser.test.ts"]));201 });202203 it("matches exact blob ids only, and never a deletion", () => {204 const changes: readonly Change[] = [205 added("src/near.ts", blob("c")),206 { before: blob("a"), path: path("test/gone.test.ts"), status: "deleted" },207 {208 after: blob("2"),209 before: blob("b"),210 path: path("test/rewritten.test.ts"),211 status: "modified",212 },213 ];214215 expect(216 writeDown({ changes, layerBlobs, readerCanReadLayer: true })217 ).toStrictEqual(Option.none());218 });219220 it("is total over empty inputs", () => {221 expect(222 writeDown({ changes: [], layerBlobs, readerCanReadLayer: true })223 ).toStrictEqual(Option.none());224 expect(225 writeDown({226 changes: copied,227 layerBlobs: new Set(),228 readerCanReadLayer: true,229 })230 ).toStrictEqual(Option.none());231 });232233 it("names paths sorted and once, whatever the order of the changes", () => {234 const changes: readonly Change[] = [235 added("z/copy.test.ts", blob("a")),236 added("a/copy.test.ts", blob("b")),237 added("m/own.ts", blob("1")),238 added("z/copy.test.ts", blob("a")),239 ];240241 expect(242 pathsOf(writeDown({ changes, layerBlobs, readerCanReadLayer: true }))243 ).toStrictEqual(Option.some(["a/copy.test.ts", "z/copy.test.ts"]));244 expect(245 writeDown({246 changes: changes.toReversed(),247 layerBlobs,248 readerCanReadLayer: true,249 })250 ).toStrictEqual(251 writeDown({ changes, layerBlobs, readerCanReadLayer: true })252 );253 });254});