nomadcode/services/core/internal/projectsync/provision_test.go
toki 096efb0a83 feat(workitempipeline): add provision service and archive G06 artifacts
- Add projectsync provision service with tests
- Update HTTP handlers with provision endpoint support
- Update workitempipeline service and tests
- Archive G06 workspace provision artifacts (plan, code review)
- Update G07 cloud code review
2026-06-14 05:13:16 +09:00

114 lines
3.7 KiB
Go

package projectsync
import (
"context"
"errors"
"os"
"path/filepath"
"testing"
)
func TestFilesystemWorkspaceProvisionerAcceptsPreparedCheckouts(t *testing.T) {
tmpDir := t.TempDir()
plan := ProvisionPlan{
ProjectWorkspaceRoot: tmpDir,
MainBranchPath: filepath.Join(tmpDir, "branches", "main"),
DevelopBranchPath: filepath.Join(tmpDir, "branches", "develop"),
DefaultSlotPath: filepath.Join(tmpDir, "slots", "000"),
}
// Create directories
for _, path := range []string{plan.MainBranchPath, plan.DevelopBranchPath, plan.DefaultSlotPath} {
if err := os.MkdirAll(path, 0755); err != nil {
t.Fatalf("failed to create dir %s: %v", path, err)
}
// Create .git directory
if err := os.MkdirAll(filepath.Join(path, ".git"), 0755); err != nil {
t.Fatalf("failed to create .git in %s: %v", path, err)
}
}
provisioner := FilesystemWorkspaceProvisioner{}
err := provisioner.EnsureProvisioned(context.Background(), plan)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
}
func TestFilesystemWorkspaceProvisionerRejectsMissingCheckout(t *testing.T) {
tmpDir := t.TempDir()
plan := ProvisionPlan{
ProjectWorkspaceRoot: tmpDir,
MainBranchPath: filepath.Join(tmpDir, "branches", "main"),
DevelopBranchPath: filepath.Join(tmpDir, "branches", "develop"),
DefaultSlotPath: filepath.Join(tmpDir, "slots", "000"),
}
// Create main and develop only, skip default slot
for _, path := range []string{plan.MainBranchPath, plan.DevelopBranchPath} {
if err := os.MkdirAll(path, 0755); err != nil {
t.Fatalf("failed to create dir %s: %v", path, err)
}
if err := os.MkdirAll(filepath.Join(path, ".git"), 0755); err != nil {
t.Fatalf("failed to create .git in %s: %v", path, err)
}
}
provisioner := FilesystemWorkspaceProvisioner{}
err := provisioner.EnsureProvisioned(context.Background(), plan)
if !errors.Is(err, ErrWorkspaceProvisionNotReady) {
t.Errorf("expected ErrWorkspaceProvisionNotReady error, got %v", err)
}
}
func TestFilesystemWorkspaceProvisionerRejectsPathWithoutGitMetadata(t *testing.T) {
tmpDir := t.TempDir()
plan := ProvisionPlan{
ProjectWorkspaceRoot: tmpDir,
MainBranchPath: filepath.Join(tmpDir, "branches", "main"),
DevelopBranchPath: filepath.Join(tmpDir, "branches", "develop"),
DefaultSlotPath: filepath.Join(tmpDir, "slots", "000"),
}
// Create directories but do not create .git for default slot
for _, path := range []string{plan.MainBranchPath, plan.DevelopBranchPath, plan.DefaultSlotPath} {
if err := os.MkdirAll(path, 0755); err != nil {
t.Fatalf("failed to create dir %s: %v", path, err)
}
}
// Create .git only for main and develop
for _, path := range []string{plan.MainBranchPath, plan.DevelopBranchPath} {
if err := os.MkdirAll(filepath.Join(path, ".git"), 0755); err != nil {
t.Fatalf("failed to create .git in %s: %v", path, err)
}
}
provisioner := FilesystemWorkspaceProvisioner{}
err := provisioner.EnsureProvisioned(context.Background(), plan)
if !errors.Is(err, ErrWorkspaceProvisionNotReady) {
t.Errorf("expected ErrWorkspaceProvisionNotReady error, got %v", err)
}
}
func TestFilesystemWorkspaceProvisionerHonorsCanceledContext(t *testing.T) {
tmpDir := t.TempDir()
plan := ProvisionPlan{
ProjectWorkspaceRoot: tmpDir,
MainBranchPath: filepath.Join(tmpDir, "branches", "main"),
DevelopBranchPath: filepath.Join(tmpDir, "branches", "develop"),
DefaultSlotPath: filepath.Join(tmpDir, "slots", "000"),
}
ctx, cancel := context.WithCancel(context.Background())
cancel() // Cancel immediately
provisioner := FilesystemWorkspaceProvisioner{}
err := provisioner.EnsureProvisioned(ctx, plan)
if !errors.Is(err, context.Canceled) {
t.Errorf("expected context.Canceled, got %v", err)
}
}