~james/core

core/spindle/models/clone_test.go -rw-r--r-- 13.4 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 14 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
package models

import (
	"strings"
	"testing"

	"tangled.org/core/workflow"
)

func sp(s string) *string { return &s }

func TestBuildCloneStep_PushTrigger(t *testing.T) {
	twf := workflow.WorkflowSpec{
		Clone: &workflow.CloneSpec{
			Depth:      1,
			Submodules: false,
			Skip:       false,
		},
	}
	tr := workflow.TriggerMetadata{
		Kind: string(workflow.TriggerKindPush),
		Push: &workflow.PushTriggerData{
			NewSha: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
			OldSha: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
			Ref:    "refs/heads/main",
		},
		Repo: &workflow.TriggerRepo{
			Knot:    "example.com",
			Did:     "did:plc:user123",
			Repo:    sp("my-repo"),
			RepoDid: sp("did:plc:boltless"),
		},
	}

	step := BuildCloneStep(twf, tr, false)

	if step.Kind() != StepKindSystem {
		t.Errorf("Expected StepKindSystem, got %v", step.Kind())
	}

	if step.Name() != "Clone repository into workspace" {
		t.Errorf("Expected 'Clone repository into workspace', got '%s'", step.Name())
	}

	commands := step.Commands()
	if len(commands) != 4 {
		t.Errorf("Expected 4 commands, got %d", len(commands))
	}

	// Verify commands contain expected git operations
	allCmds := strings.Join(commands, " ")
	if !strings.Contains(allCmds, "git init --object-format=sha1") {
		t.Error("Commands should contain 'git init'")
	}
	if !strings.Contains(allCmds, "git remote add origin") {
		t.Error("Commands should contain 'git remote add origin'")
	}
	if !strings.Contains(allCmds, "git fetch") {
		t.Error("Commands should contain 'git fetch'")
	}
	if !strings.Contains(allCmds, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") {
		t.Error("Commands should contain commit SHA")
	}
	if !strings.Contains(allCmds, "git checkout FETCH_HEAD") {
		t.Error("Commands should contain 'git checkout FETCH_HEAD'")
	}
	if !strings.Contains(allCmds, "https://example.com/did:plc:boltless") {
		t.Error("Commands should contain expected repo URL")
	}
}

func TestBuildCloneStep_PullRequestTrigger(t *testing.T) {
	twf := workflow.WorkflowSpec{
		Clone: &workflow.CloneSpec{
			Depth: 1,
			Skip:  false,
		},
	}
	tr := workflow.TriggerMetadata{
		Kind: string(workflow.TriggerKindPullRequest),
		PullRequest: &workflow.PullRequestTriggerData{
			SourceSha:    "cccccccccccccccccccccccccccccccccccccccc",
			SourceBranch: "feature-branch",
			TargetBranch: "main",
		},
		Repo: &workflow.TriggerRepo{
			Knot:    "example.com",
			Did:     "did:plc:user123",
			Repo:    sp("my-repo"),
			RepoDid: sp("did:plc:boltless"),
		},
	}

	step := BuildCloneStep(twf, tr, false)

	allCmds := strings.Join(step.Commands(), " ")
	if !strings.Contains(allCmds, "cccccccccccccccccccccccccccccccccccccccc") {
		t.Error("Commands should contain PR commit SHA")
	}
}

func TestBuildCloneStep_SourceRepo(t *testing.T) {
	twf := workflow.WorkflowSpec{
		Clone: &workflow.CloneSpec{
			Depth: 1,
			Skip:  false,
		},
	}
	sourceRepoDid := "did:plc:fork"
	tr := workflow.TriggerMetadata{
		Kind: string(workflow.TriggerKindPullRequest),
		PullRequest: &workflow.PullRequestTriggerData{
			SourceSha:    "cccccccccccccccccccccccccccccccccccccccc",
			SourceBranch: "feature-branch",
			TargetBranch: "main",
		},
		Repo: &workflow.TriggerRepo{
			Knot:    "fork.example.com",
			Did:     "did:plc:user456",
			Repo:    sp("fork-repo"),
			RepoDid: &sourceRepoDid,
		},
		SourceRepo: &sourceRepoDid,
	}

	step := BuildCloneStep(twf, tr, false)

	allCmds := strings.Join(step.Commands(), " ")
	if !strings.Contains(allCmds, "https://fork.example.com/did:plc:fork") {
		t.Error("Commands should clone from source repo URL")
	}
	if strings.Contains(allCmds, "https://target.example.com/did:plc:target") {
		t.Error("Commands should not clone from target repo URL when sourceRepo is set")
	}
}

func TestBuildCloneStep_ManualTrigger(t *testing.T) {
	twf := workflow.WorkflowSpec{
		Clone: &workflow.CloneSpec{
			Depth: 1,
			Skip:  false,
		},
	}
	tr := workflow.TriggerMetadata{
		Kind: string(workflow.TriggerKindManual),
		Manual: &workflow.ManualTriggerData{
			Sha:    "dddddddddddddddddddddddddddddddddddddddd",
			Inputs: nil,
		},
		Repo: &workflow.TriggerRepo{
			Knot:    "example.com",
			Did:     "did:plc:user123",
			Repo:    sp("my-repo"),
			RepoDid: sp("did:plc:boltless"),
		},
	}

	step := BuildCloneStep(twf, tr, false)

	allCmds := strings.Join(step.Commands(), " ")
	// Should still have basic git commands
	if !strings.Contains(allCmds, "git init") {
		t.Error("Commands should contain 'git init'")
	}
	if !strings.Contains(allCmds, "git fetch") {
		t.Error("Commands should contain 'git fetch'")
	}
	// Manual triggers now carry an explicit SHA, which the fetch targets
	if !strings.Contains(allCmds, "dddddddddddddddddddddddddddddddddddddddd") {
		t.Error("Commands should contain the manual trigger SHA")
	}
}

func TestBuildCloneStep_SkipFlag(t *testing.T) {
	twf := workflow.WorkflowSpec{
		Clone: &workflow.CloneSpec{
			Skip: true,
		},
	}
	tr := workflow.TriggerMetadata{
		Kind: string(workflow.TriggerKindPush),
		Push: &workflow.PushTriggerData{
			NewSha: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
		},
		Repo: &workflow.TriggerRepo{
			Knot:    "example.com",
			Did:     "did:plc:user123",
			Repo:    sp("my-repo"),
			RepoDid: sp("did:plc:boltless"),
		},
	}

	step := BuildCloneStep(twf, tr, false)

	// Empty step when skip is true
	if step.Name() != "" {
		t.Error("Expected empty step name when Skip is true")
	}
	if len(step.Commands()) != 0 {
		t.Errorf("Expected no commands when Skip is true, got %d commands", len(step.Commands()))
	}
}

func TestBuildCloneStep_DevMode(t *testing.T) {
	twf := workflow.WorkflowSpec{
		Engine: "nixery",
		Clone: &workflow.CloneSpec{
			Depth: 1,
			Skip:  false,
		},
	}
	tr := workflow.TriggerMetadata{
		Kind: string(workflow.TriggerKindPush),
		Push: &workflow.PushTriggerData{
			NewSha: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
		},
		Repo: &workflow.TriggerRepo{
			Knot:    "knot.tngl.boltless.dev",
			Did:     "did:plc:user123",
			Repo:    sp("my-repo"),
			RepoDid: sp("did:plc:boltless"),
		},
	}

	step := BuildCloneStep(twf, tr, true)

	// In dev mode, sslVerify should be disabled
	allCmds := strings.Join(step.Commands(), " ")
	if !strings.Contains(allCmds, "git -c http.sslVerify=false fetch") {
		t.Error("Expected sslVerify to be disabled in dev mode clone commands")
	}
}

func TestBuildCloneStep_DepthAndSubmodules(t *testing.T) {
	twf := workflow.WorkflowSpec{
		Clone: &workflow.CloneSpec{
			Depth:      10,
			Submodules: true,
			Skip:       false,
		},
	}
	tr := workflow.TriggerMetadata{
		Kind: string(workflow.TriggerKindPush),
		Push: &workflow.PushTriggerData{
			NewSha: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
		},
		Repo: &workflow.TriggerRepo{
			Knot:    "example.com",
			Did:     "did:plc:user123",
			Repo:    sp("my-repo"),
			RepoDid: sp("did:plc:boltless"),
		},
	}

	step := BuildCloneStep(twf, tr, false)

	allCmds := strings.Join(step.Commands(), " ")
	if !strings.Contains(allCmds, "--depth=10") {
		t.Error("Commands should contain '--depth=10'")
	}

	if !strings.Contains(allCmds, "--recurse-submodules=yes") {
		t.Error("Commands should contain '--recurse-submodules=yes'")
	}
}

func TestBuildCloneStep_DefaultDepth(t *testing.T) {
	twf := workflow.WorkflowSpec{
		Clone: &workflow.CloneSpec{
			Depth: 0, // Default should be 1
			Skip:  false,
		},
	}
	tr := workflow.TriggerMetadata{
		Kind: string(workflow.TriggerKindPush),
		Push: &workflow.PushTriggerData{
			NewSha: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
		},
		Repo: &workflow.TriggerRepo{
			Knot:    "example.com",
			Did:     "did:plc:user123",
			Repo:    sp("my-repo"),
			RepoDid: sp("did:plc:boltless"),
		},
	}

	step := BuildCloneStep(twf, tr, false)

	allCmds := strings.Join(step.Commands(), " ")
	if !strings.Contains(allCmds, "--depth=1") {
		t.Error("Commands should default to '--depth=1'")
	}
}

func TestBuildCloneStep_NilPushData(t *testing.T) {
	twf := workflow.WorkflowSpec{
		Clone: &workflow.CloneSpec{
			Depth: 1,
			Skip:  false,
		},
	}
	tr := workflow.TriggerMetadata{
		Kind: string(workflow.TriggerKindPush),
		Push: nil, // Nil push data should create error step
		Repo: &workflow.TriggerRepo{
			Knot:    "example.com",
			Did:     "did:plc:user123",
			Repo:    sp("my-repo"),
			RepoDid: sp("did:plc:boltless"),
		},
	}

	step := BuildCloneStep(twf, tr, false)

	// Should return an error step
	if !strings.Contains(step.Name(), "error") {
		t.Error("Expected error in step name when push data is nil")
	}

	allCmds := strings.Join(step.Commands(), " ")
	if !strings.Contains(allCmds, "Failed to get clone info") {
		t.Error("Commands should contain error message")
	}
	if !strings.Contains(allCmds, "exit 1") {
		t.Error("Commands should exit with error")
	}
}

func TestBuildCloneStep_NilPRData(t *testing.T) {
	twf := workflow.WorkflowSpec{
		Clone: &workflow.CloneSpec{
			Depth: 1,
			Skip:  false,
		},
	}
	tr := workflow.TriggerMetadata{
		Kind:        string(workflow.TriggerKindPullRequest),
		PullRequest: nil, // Nil PR data should create error step
		Repo: &workflow.TriggerRepo{
			Knot:    "example.com",
			Did:     "did:plc:user123",
			Repo:    sp("my-repo"),
			RepoDid: sp("did:plc:boltless"),
		},
	}

	step := BuildCloneStep(twf, tr, false)

	// Should return an error step
	if !strings.Contains(step.Name(), "error") {
		t.Error("Expected error in step name when pull request data is nil")
	}

	allCmds := strings.Join(step.Commands(), " ")
	if !strings.Contains(allCmds, "Failed to get clone info") {
		t.Error("Commands should contain error message")
	}
}

func TestBuildCloneStep_UnknownTriggerKind(t *testing.T) {
	twf := workflow.WorkflowSpec{
		Clone: &workflow.CloneSpec{
			Depth: 1,
			Skip:  false,
		},
	}
	tr := workflow.TriggerMetadata{
		Kind: "unknown_trigger",
		Repo: &workflow.TriggerRepo{
			Knot:    "example.com",
			Did:     "did:plc:user123",
			Repo:    sp("my-repo"),
			RepoDid: sp("did:plc:boltless"),
		},
	}

	step := BuildCloneStep(twf, tr, false)

	// Should return an error step
	if !strings.Contains(step.Name(), "error") {
		t.Error("Expected error in step name for unknown trigger kind")
	}

	allCmds := strings.Join(step.Commands(), " ")
	if !strings.Contains(allCmds, "unknown trigger kind") {
		t.Error("Commands should contain error message about unknown trigger kind")
	}
}

func TestBuildCloneStep_NilCloneOpts(t *testing.T) {
	twf := workflow.WorkflowSpec{
		Clone: nil, // Nil clone options should use defaults
	}
	tr := workflow.TriggerMetadata{
		Kind: string(workflow.TriggerKindPush),
		Push: &workflow.PushTriggerData{
			NewSha: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
		},
		Repo: &workflow.TriggerRepo{
			Knot:    "example.com",
			Did:     "did:plc:user123",
			Repo:    sp("my-repo"),
			RepoDid: sp("did:plc:boltless"),
		},
	}

	step := BuildCloneStep(twf, tr, false)

	// Should still work with default options
	if step.Kind() != StepKindSystem {
		t.Errorf("Expected StepKindSystem, got %v", step.Kind())
	}

	allCmds := strings.Join(step.Commands(), " ")
	if !strings.Contains(allCmds, "--depth=1") {
		t.Error("Commands should default to '--depth=1' when Clone is nil")
	}
	if !strings.Contains(allCmds, "git init") {
		t.Error("Commands should contain 'git init'")
	}
}

func TestBuildCloneStep_ScheduleTrigger(t *testing.T) {
	tr := workflow.TriggerMetadata{
		Kind: string(workflow.TriggerKindSchedule),
		Schedule: &workflow.ScheduleTriggerData{
			Sha:         "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
			Ref:         "refs/heads/main",
			ScheduledAt: "2026-08-10T09:00:00Z",
		},
		Repo: &workflow.TriggerRepo{
			Knot:    "example.com",
			Did:     "did:plc:user123",
			RepoDid: sp("did:plc:boltless"),
		},
	}

	step := BuildCloneStep(workflow.WorkflowSpec{}, tr, false)
	if commands := strings.Join(step.Commands(), " "); !strings.Contains(commands, "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee") {
		t.Fatalf("clone commands do not target scheduled SHA: %s", commands)
	}
}

func TestBuildCloneStep_ObjectFormat(t *testing.T) {
	for _, tt := range []struct {
		name   string
		sha    string
		format string
	}{
		{"sha1", strings.Repeat("a", 40), "sha1"},
		{"sha256", strings.Repeat("b", 64), "sha256"},
		{"empty", "", ""},
		{"abbreviated", "abc123", ""},
		{"short_sha1", strings.Repeat("a", 39), ""},
		{"long_sha1", strings.Repeat("a", 41), ""},
		{"short_sha256", strings.Repeat("b", 63), ""},
		{"long_sha256", strings.Repeat("b", 65), ""},
	} {
		t.Run(tt.name, func(t *testing.T) {
			tr := workflow.TriggerMetadata{
				Kind: string(workflow.TriggerKindPush),
				Push: &workflow.PushTriggerData{NewSha: tt.sha},
				Repo: &workflow.TriggerRepo{
					Knot:    "example.com",
					RepoDid: sp("did:plc:boltless"),
				},
			}
			step := BuildCloneStep(workflow.WorkflowSpec{}, tr, false)
			if step.Kind() != StepKindSystem {
				t.Fatalf("expected system step, got %v", step.Kind())
			}
			commands := step.Commands()
			if tt.format == "" {
				if step.Name() != "Clone repository into workspace (error)" {
					t.Errorf("expected error step, got %q", step.Name())
				}
				if len(commands) != 1 || commands[0] != "echo 'Failed to determine repo object format' && exit 1" {
					t.Fatalf("expected only an error command, got %q", commands)
				}
				return
			}
			if step.Name() != "Clone repository into workspace" {
				t.Errorf("unexpected step name: %q", step.Name())
			}
			if len(commands) != 4 {
				t.Fatalf("expected four checkout commands, got %q", commands)
			}
			if want := "git init --object-format=" + tt.format; commands[0] != want {
				t.Errorf("init command = %q, want %q", commands[0], want)
			}
			if !strings.HasSuffix(commands[2], "origin "+tt.sha) {
				t.Errorf("fetch does not target full commit ID: %q", commands[2])
			}
		})
	}
}