@@ -6,7 +6,13 @@ import * as TestClock from "effect/testing/TestClock";
66
77import * as EntirePullRequestProvider from "./EntirePullRequestProvider.ts" ;
88import * as Fixtures from "./entireFixtures.ts" ;
9- import { Answer , fakeEntire , TRAIL_COLLECTION } from "./entireTestServer.ts" ;
9+ import {
10+ Answer ,
11+ fakeEntire ,
12+ PROJECT_CELL_ORIGIN ,
13+ PROJECT_TRAILS ,
14+ TRAIL_COLLECTION ,
15+ } from "./entireTestServer.ts" ;
1016
1117const target = { cwd : "/repo" , host : "entire.io" , repository : "gh/acme/web" , number : 7 } ;
1218
@@ -150,71 +156,129 @@ describe("EntirePullRequestProvider", () => {
150156 } ,
151157 ) ;
152158
153- it . effect ( "renames a trail, and rewrites its description against the version read" , ( ) => {
154- const parent = { number : 1070 , url : "https://entire.com/et/acme/-/trails/1070" } ;
155- const reads : Array < unknown > = [
156- { ...Fixtures . detail , parent, body_document : { text_snapshot : "Old" , etag : 'W/"d1"' } } ,
157- // No document yet: there is no version to name.
158- { ...Fixtures . detail , body_document : { text_snapshot : "" , etag : "" } } ,
159- ] ;
159+ it . effect ( "renames a trail, and refuses a description it can't route to a project trail" , ( ) => {
160160 const server = fakeEntire ( {
161161 "PATCH /trails/7" : { trail : { } } ,
162- "GET /trails/7" : ( ) => reads . shift ( ) ,
163- "PUT /trails/7/body" : { etag : 'W/"d2"' } ,
162+ "GET /trails/7" : {
163+ ...Fixtures . detail ,
164+ body_document : { text_snapshot : "Old" , etag : 'W/"d1"' } ,
165+ } ,
164166 } ) ;
165167 return Effect . gen ( function * ( ) {
166168 const provider = yield * EntirePullRequestProvider . make ;
167169 yield * provider . updateChangeRequest ! ( { ...target , title : "Add the other thing" } ) ;
168- yield * provider . updateChangeRequest ! ( {
170+ const error = yield * provider . updateChangeRequest ! ( {
169171 ...target ,
170- body : `Part of trail [#1070](${ parent . url } ) on Entire.\n\nNew` ,
171- } ) ;
172- yield * provider . updateChangeRequest ! ( { ...target , body : "First" } ) ;
173- const writes = server . sent . filter ( ( request ) => request . method !== "GET" ) ;
172+ title : "Renamed" ,
173+ body : "New" ,
174+ } ) . pipe ( Effect . flip ) ;
175+ assert . strictEqual (
176+ error . detail ,
177+ "T3 Code couldn't reach this change's project trail to save the description. Try again, or edit it on entire.com." ,
178+ ) ;
179+ // Neither the description nor the title was written.
174180 assert . deepStrictEqual (
175- writes . map ( ( request ) => [
176- `${ request . method } ${ request . path } ` ,
177- request . body ,
178- request . headers [ "if-match" ] ,
179- ] ) ,
180- [
181- [ "PATCH /trails/7" , { title : "Add the other thing" } , undefined ] ,
182- [ "PUT /trails/7/body" , { markdown : "New" , overwrite : false } , 'W/"d1"' ] ,
183- [ "PUT /trails/7/body" , { markdown : "First" , overwrite : false } , undefined ] ,
184- ] ,
181+ server . sent
182+ . filter ( ( request ) => request . method !== "GET" )
183+ . map ( ( request ) => [ `${ request . method } ${ request . path } ` , request . body ] ) ,
184+ [ [ "PATCH /trails/7" , { title : "Add the other thing" } ] ] ,
185185 ) ;
186186 } ) . pipe ( Effect . provide ( server . layer ) ) ;
187187 } ) ;
188188
189- it . effect . each ( [ 412 , 409 ] ) (
190- "asks for a refresh when the description changed elsewhere (%s)" ,
191- ( status ) => {
189+ describe ( "a change's project trail" , ( ) => {
190+ const trailPath = `${ PROJECT_TRAILS } /01J0PARENT0000000000000000` ;
191+ const parent = {
192+ number : 1070 ,
193+ url : "https://entire.com/gh/acme/-/trails/1070" ,
194+ path : trailPath ,
195+ primary_processing_cell : "aws-eu-central-1" ,
196+ jurisdiction : "eu" ,
197+ } ;
198+ const link = `Part of trail [#1070](${ parent . url } ) on Entire.` ;
199+ const change = {
200+ ...Fixtures . detail ,
201+ parent,
202+ body_document : { text_snapshot : "The copy taken at linking" , etag : 'W/"c1"' } ,
203+ } ;
204+
205+ it . effect ( "shows the project trail's description, read from its project's cell" , ( ) => {
192206 const server = fakeEntire ( {
193- "GET /trails/7" : Fixtures . detail ,
194- "PUT /trails/7/body" : new Answer ( status , { detail : "If-Match does not match" } ) ,
195- "PATCH /trails/7" : { trail : { } } ,
207+ "GET /trails/7" : change ,
208+ [ `GET ${ trailPath } ` ] : { bodyDocument : { text_snapshot : "The intent, as edited" } } ,
196209 } ) ;
210+ return Effect . gen ( function * ( ) {
211+ const provider = yield * EntirePullRequestProvider . make ;
212+ const detail = yield * provider . getChangeRequest ( target ) ;
213+ assert . strictEqual ( detail . body , `${ link } \n\nThe intent, as edited` ) ;
214+ assert . strictEqual (
215+ server . sent . find ( ( request ) => request . path === trailPath ) ?. origin ,
216+ PROJECT_CELL_ORIGIN ,
217+ ) ;
218+ } ) . pipe ( Effect . provide ( server . layer ) ) ;
219+ } ) ;
220+
221+ it . effect ( "falls back to the change's copy when the project trail can't be read" , ( ) => {
222+ const server = fakeEntire ( { "GET /trails/7" : change } ) ;
223+ return Effect . gen ( function * ( ) {
224+ const provider = yield * EntirePullRequestProvider . make ;
225+ const detail = yield * provider . getChangeRequest ( target ) ;
226+ assert . strictEqual ( detail . body , `${ link } \n\nThe copy taken at linking` ) ;
227+ } ) . pipe ( Effect . provide ( server . layer ) ) ;
228+ } ) ;
229+
230+ it . effect ( "saves a description edit to the project trail, against the version read" , ( ) => {
231+ const server = fakeEntire (
232+ {
233+ "GET /trails/7" : change ,
234+ [ `GET ${ trailPath } ` ] : { bodyDocument : { text_snapshot : "Old intent" } } ,
235+ [ `PATCH ${ trailPath } ` ] : { } ,
236+ } ,
237+ { etag : 'W/"p1"' } ,
238+ ) ;
239+ return Effect . gen ( function * ( ) {
240+ const provider = yield * EntirePullRequestProvider . make ;
241+ yield * provider . updateChangeRequest ! ( { ...target , body : `${ link } \n\nNew intent` } ) ;
242+ assert . deepStrictEqual ( server . writes ( ) , [ `PATCH ${ trailPath } ` ] ) ;
243+ const write = server . sent . at ( - 1 ) ;
244+ assert . deepStrictEqual ( write ?. body , { body : "New intent" } ) ;
245+ assert . strictEqual ( write ?. headers [ "if-match" ] , 'W/"p1"' ) ;
246+ assert . strictEqual ( write ?. origin , PROJECT_CELL_ORIGIN ) ;
247+ } ) . pipe ( Effect . provide ( server . layer ) ) ;
248+ } ) ;
249+
250+ it . effect ( "asks for a refresh when the project trail changed since it was read" , ( ) => {
251+ const server = fakeEntire (
252+ {
253+ "GET /trails/7" : change ,
254+ [ `GET ${ trailPath } ` ] : { bodyDocument : { text_snapshot : "Old intent" } } ,
255+ [ `PATCH ${ trailPath } ` ] : new Answer ( 412 , { title : "Precondition Failed" } ) ,
256+ } ,
257+ { etag : 'W/"p1"' } ,
258+ ) ;
197259 return Effect . gen ( function * ( ) {
198260 const provider = yield * EntirePullRequestProvider . make ;
199261 const error = yield * provider . updateChangeRequest ! ( {
200262 ...target ,
201263 title : "Renamed" ,
202- body : "Mine " ,
264+ body : "New intent " ,
203265 } ) . pipe ( Effect . flip ) ;
204- assert . include ( error . detail , "changed elsewhere" ) ;
266+ assert . strictEqual (
267+ error . detail ,
268+ "The description changed elsewhere. Refresh and try again." ,
269+ ) ;
205270 // The title is left as it was.
206- assert . deepStrictEqual ( server . writes ( ) , [ "PUT /trails/7/body" ] ) ;
271+ assert . deepStrictEqual ( server . writes ( ) , [ `PATCH ${ trailPath } ` ] ) ;
207272 } ) . pipe ( Effect . provide ( server . layer ) ) ;
208- } ,
209- ) ;
273+ } ) ;
274+ } ) ;
210275
211276 it . effect ( "refuses to rewrite formatting Entire's markdown would lose, title and all" , ( ) => {
212277 const server = fakeEntire ( {
213278 "GET /trails/7" : {
214279 ...Fixtures . detail ,
215280 body_document : { text_snapshot : "Plain" , etag : 'W/"d1"' } ,
216281 } ,
217- "PUT /trails/7/body" : { etag : 'W/"d2"' } ,
218282 } ) ;
219283 return Effect . gen ( function * ( ) {
220284 const provider = yield * EntirePullRequestProvider . make ;
0 commit comments