@@ -5,38 +5,88 @@ import { commitFilesFromBuffers } from "../../node.js";
5
5
import { deleteBranches } from "./util.js" ;
6
6
import {
7
7
createRefMutation ,
8
+ getRefTreeQuery ,
8
9
getRepositoryMetadata ,
9
10
} from "../../github/graphql/queries.js" ;
10
11
11
12
const octokit = getOctokit ( ENV . GITHUB_TOKEN ) ;
12
13
13
14
const TEST_BRANCH_PREFIX = `${ ROOT_TEST_BRANCH_PREFIX } -node` ;
14
15
16
+ const TEST_TARGET_COMMIT = "fce2760017eab6d85388ed5cfdfac171559d80b3" ;
17
+ /**
18
+ * For tests, important that this commit is not an ancestor of TEST_TARGET_COMMIT,
19
+ * to ensure that non-fast-forward pushes are tested
20
+ */
21
+ const TEST_TARGET_COMMIT_2 = "7ba8473f02849de3b5449b25fc83c5245d338d94" ;
22
+ const TEST_TARGET_TREE_2 = "95c9ea756f3686614dcdc1c42f7f654b684cdac2" ;
23
+
24
+ const BASIC_FILE_CHANGES_PATH = "foo.txt" ;
25
+ const BASIC_FILE_CHANGES_OID = "0e23339619d605319ec4b49a0ac9dd94598eff8e" ;
26
+ const BASIC_FILE_CONTENTS = {
27
+ message : {
28
+ headline : "Test commit" ,
29
+ body : "This is a test commit" ,
30
+ } ,
31
+ fileChanges : {
32
+ additions : [
33
+ {
34
+ path : BASIC_FILE_CHANGES_PATH ,
35
+ contents : Buffer . alloc ( 1024 , "Hello, world!" ) ,
36
+ } ,
37
+ ] ,
38
+ } ,
39
+ log,
40
+ } ;
41
+
42
+ const TEST_TARGET_TREE_WITH_BASIC_CHANGES =
43
+ "a3431c9b42b71115c52bc6fbf9da3682cf0ed5e8" ;
44
+
15
45
describe ( "node" , ( ) => {
16
46
const branches : string [ ] = [ ] ;
17
47
18
48
// Set timeout to 1 minute
19
49
jest . setTimeout ( 60 * 1000 ) ;
20
50
21
- const contents = Buffer . alloc ( 1024 , "Hello, world!" ) ;
22
- const BASIC_FILE_CONTENTS = {
23
- message : {
24
- headline : "Test commit" ,
25
- body : "This is a test commit" ,
26
- } ,
27
- fileChanges : {
28
- additions : [
29
- {
30
- path : `foo.txt` ,
31
- contents,
32
- } ,
33
- ] ,
34
- } ,
35
- log,
36
- } ;
37
-
38
51
let repositoryId : string ;
39
52
53
+ const expectBranchHasTree = async ( {
54
+ branch,
55
+ treeOid,
56
+ file,
57
+ } : {
58
+ branch : string ;
59
+ treeOid ?: string ;
60
+ file ?: {
61
+ path : string ;
62
+ oid : string ;
63
+ } ;
64
+ } ) => {
65
+ const ref = (
66
+ await getRefTreeQuery ( octokit , {
67
+ owner : REPO . owner ,
68
+ name : REPO . repository ,
69
+ ref : `refs/heads/${ branch } ` ,
70
+ path : file ?. path ?? "package.json" ,
71
+ } )
72
+ ) . repository ?. ref ?. target ;
73
+
74
+ if ( ! ref ) {
75
+ throw new Error ( "Unexpected missing ref" ) ;
76
+ }
77
+
78
+ if ( "tree" in ref ) {
79
+ if ( treeOid ) {
80
+ expect ( ref . tree . oid ) . toEqual ( treeOid ) ;
81
+ }
82
+ if ( file ) {
83
+ expect ( ref . file ?. oid ) . toEqual ( file . oid ) ;
84
+ }
85
+ } else {
86
+ throw new Error ( "Expected ref to have a tree" ) ;
87
+ }
88
+ } ;
89
+
40
90
beforeAll ( async ( ) => {
41
91
const response = await getRepositoryMetadata ( octokit , {
42
92
owner : REPO . owner ,
@@ -53,12 +103,26 @@ describe("node", () => {
53
103
describe ( "commitFilesFromBuffers" , ( ) => {
54
104
describe ( "can commit single file of various sizes" , ( ) => {
55
105
const SIZES_BYTES = {
56
- "1KiB" : 1024 ,
57
- "1MiB" : 1024 * 1024 ,
58
- "10MiB" : 1024 * 1024 * 10 ,
106
+ "1KiB" : {
107
+ sizeBytes : 1024 ,
108
+ treeOid : "547dfe4079b53c3b45a6717ac1ed6d98512f0a1c" ,
109
+ fileOid : "0e23339619d605319ec4b49a0ac9dd94598eff8e" ,
110
+ } ,
111
+ "1MiB" : {
112
+ sizeBytes : 1024 * 1024 ,
113
+ treeOid : "a6dca57388cf08de146bcc01a2113b218d6c2858" ,
114
+ fileOid : "a1d7fed1b4a8de1b665dc4f604015b2d87ef978f" ,
115
+ } ,
116
+ "10MiB" : {
117
+ sizeBytes : 1024 * 1024 * 10 ,
118
+ treeOid : "c4788256a2c1e3ea4267cff0502a656d992248ec" ,
119
+ fileOid : "e36e74edbb6d3fc181ef584a50f8ee55585d27cc" ,
120
+ } ,
59
121
} ;
60
122
61
- for ( const [ sizeName , sizeBytes ] of Object . entries ( SIZES_BYTES ) ) {
123
+ for ( const [ sizeName , { sizeBytes, treeOid, fileOid } ] of Object . entries (
124
+ SIZES_BYTES ,
125
+ ) ) {
62
126
it ( `Can commit a ${ sizeName } ` , async ( ) => {
63
127
const branch = `${ TEST_BRANCH_PREFIX } -${ sizeName } ` ;
64
128
branches . push ( branch ) ;
@@ -69,7 +133,7 @@ describe("node", () => {
69
133
...REPO ,
70
134
branch,
71
135
base : {
72
- branch : "main" ,
136
+ commit : TEST_TARGET_COMMIT ,
73
137
} ,
74
138
message : {
75
139
headline : "Test commit" ,
@@ -85,10 +149,43 @@ describe("node", () => {
85
149
} ,
86
150
log,
87
151
} ) ;
152
+
153
+ await expectBranchHasTree ( {
154
+ branch,
155
+ treeOid,
156
+ file : {
157
+ path : `${ sizeName } .txt` ,
158
+ oid : fileOid ,
159
+ } ,
160
+ } ) ;
88
161
} ) ;
89
162
}
90
163
} ) ;
91
164
165
+ it ( "can commit using branch as a base" , async ( ) => {
166
+ const branch = `${ TEST_BRANCH_PREFIX } -branch-base` ;
167
+ branches . push ( branch ) ;
168
+
169
+ await commitFilesFromBuffers ( {
170
+ octokit,
171
+ ...REPO ,
172
+ branch,
173
+ base : {
174
+ branch : "main" ,
175
+ } ,
176
+ ...BASIC_FILE_CONTENTS ,
177
+ } ) ;
178
+
179
+ // Don't test tree for this one as it will change over time / be unstable
180
+ await expectBranchHasTree ( {
181
+ branch,
182
+ file : {
183
+ path : BASIC_FILE_CHANGES_PATH ,
184
+ oid : BASIC_FILE_CHANGES_OID ,
185
+ } ,
186
+ } ) ;
187
+ } ) ;
188
+
92
189
it ( "can commit using tag as a base" , async ( ) => {
93
190
const branch = `${ TEST_BRANCH_PREFIX } -tag-base` ;
94
191
branches . push ( branch ) ;
@@ -102,6 +199,15 @@ describe("node", () => {
102
199
} ,
103
200
...BASIC_FILE_CONTENTS ,
104
201
} ) ;
202
+
203
+ // Don't test tree for this one as it will change over time / be unstable
204
+ await expectBranchHasTree ( {
205
+ branch,
206
+ file : {
207
+ path : BASIC_FILE_CHANGES_PATH ,
208
+ oid : BASIC_FILE_CHANGES_OID ,
209
+ } ,
210
+ } ) ;
105
211
} ) ;
106
212
107
213
it ( "can commit using commit as a base" , async ( ) => {
@@ -113,10 +219,19 @@ describe("node", () => {
113
219
...REPO ,
114
220
branch,
115
221
base : {
116
- commit : "fce2760017eab6d85388ed5cfdfac171559d80b3" ,
222
+ commit : TEST_TARGET_COMMIT ,
117
223
} ,
118
224
...BASIC_FILE_CONTENTS ,
119
225
} ) ;
226
+
227
+ await expectBranchHasTree ( {
228
+ branch,
229
+ treeOid : TEST_TARGET_TREE_WITH_BASIC_CHANGES ,
230
+ file : {
231
+ path : BASIC_FILE_CHANGES_PATH ,
232
+ oid : BASIC_FILE_CHANGES_OID ,
233
+ } ,
234
+ } ) ;
120
235
} ) ;
121
236
122
237
describe ( "existing branches" , ( ) => {
@@ -129,7 +244,7 @@ describe("node", () => {
129
244
input : {
130
245
repositoryId,
131
246
name : `refs/heads/${ branch } ` ,
132
- oid : "31ded45f25a07726e02fd111d4c230718b49fa2a" ,
247
+ oid : TEST_TARGET_COMMIT_2 ,
133
248
} ,
134
249
} ) ;
135
250
@@ -138,11 +253,20 @@ describe("node", () => {
138
253
...REPO ,
139
254
branch,
140
255
base : {
141
- commit : "fce2760017eab6d85388ed5cfdfac171559d80b3" ,
256
+ commit : TEST_TARGET_COMMIT ,
142
257
} ,
143
258
...BASIC_FILE_CONTENTS ,
144
259
force : true ,
145
260
} ) ;
261
+
262
+ await expectBranchHasTree ( {
263
+ branch,
264
+ treeOid : TEST_TARGET_TREE_WITH_BASIC_CHANGES ,
265
+ file : {
266
+ path : BASIC_FILE_CHANGES_PATH ,
267
+ oid : BASIC_FILE_CHANGES_OID ,
268
+ } ,
269
+ } ) ;
146
270
} ) ;
147
271
148
272
it ( "cannot commit to existing branch when force is false" , async ( ) => {
@@ -154,7 +278,7 @@ describe("node", () => {
154
278
input : {
155
279
repositoryId,
156
280
name : `refs/heads/${ branch } ` ,
157
- oid : "31ded45f25a07726e02fd111d4c230718b49fa2a" ,
281
+ oid : TEST_TARGET_COMMIT_2 ,
158
282
} ,
159
283
} ) ;
160
284
@@ -164,13 +288,18 @@ describe("node", () => {
164
288
...REPO ,
165
289
branch,
166
290
base : {
167
- commit : "fce2760017eab6d85388ed5cfdfac171559d80b3" ,
291
+ commit : TEST_TARGET_COMMIT ,
168
292
} ,
169
293
...BASIC_FILE_CONTENTS ,
170
294
} ) ,
171
295
) . rejects . toThrow (
172
296
`Branch ${ branch } exists already and does not match base` ,
173
297
) ;
298
+
299
+ await expectBranchHasTree ( {
300
+ branch,
301
+ treeOid : TEST_TARGET_TREE_2 ,
302
+ } ) ;
174
303
} ) ;
175
304
176
305
it ( "can commit to existing branch when force is false and target matches base" , async ( ) => {
@@ -182,7 +311,7 @@ describe("node", () => {
182
311
input : {
183
312
repositoryId,
184
313
name : `refs/heads/${ branch } ` ,
185
- oid : "31ded45f25a07726e02fd111d4c230718b49fa2a" ,
314
+ oid : TEST_TARGET_COMMIT ,
186
315
} ,
187
316
} ) ;
188
317
@@ -191,10 +320,19 @@ describe("node", () => {
191
320
...REPO ,
192
321
branch,
193
322
base : {
194
- commit : "31ded45f25a07726e02fd111d4c230718b49fa2a" ,
323
+ commit : TEST_TARGET_COMMIT ,
195
324
} ,
196
325
...BASIC_FILE_CONTENTS ,
197
326
} ) ;
327
+
328
+ await expectBranchHasTree ( {
329
+ branch,
330
+ treeOid : TEST_TARGET_TREE_WITH_BASIC_CHANGES ,
331
+ file : {
332
+ path : BASIC_FILE_CHANGES_PATH ,
333
+ oid : BASIC_FILE_CHANGES_OID ,
334
+ } ,
335
+ } ) ;
198
336
} ) ;
199
337
200
338
it ( "can commit to same branch as base" , async ( ) => {
@@ -206,7 +344,7 @@ describe("node", () => {
206
344
input : {
207
345
repositoryId,
208
346
name : `refs/heads/${ branch } ` ,
209
- oid : "31ded45f25a07726e02fd111d4c230718b49fa2a" ,
347
+ oid : TEST_TARGET_COMMIT ,
210
348
} ,
211
349
} ) ;
212
350
@@ -219,6 +357,15 @@ describe("node", () => {
219
357
} ,
220
358
...BASIC_FILE_CONTENTS ,
221
359
} ) ;
360
+
361
+ await expectBranchHasTree ( {
362
+ branch,
363
+ treeOid : TEST_TARGET_TREE_WITH_BASIC_CHANGES ,
364
+ file : {
365
+ path : BASIC_FILE_CHANGES_PATH ,
366
+ oid : BASIC_FILE_CHANGES_OID ,
367
+ } ,
368
+ } ) ;
222
369
} ) ;
223
370
} ) ;
224
371
} ) ;
0 commit comments