|
| 1 | +// |
| 2 | +// DeploymentTests.swift |
| 3 | +// Ignite |
| 4 | +// https://www.github.com/twostraws/Ignite |
| 5 | +// See LICENSE for license information. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import Testing |
| 10 | + |
| 11 | +@testable import Ignite |
| 12 | + |
| 13 | +/// Tests for the deployment foundation types. |
| 14 | +@Suite("Deployment Tests") |
| 15 | +struct DeploymentTests { |
| 16 | + |
| 17 | + // MARK: - DeploymentDiff |
| 18 | + |
| 19 | + @Test("Diff reports added files") |
| 20 | + func diffAdded() { |
| 21 | + let diff = DeploymentDiff( |
| 22 | + added: ["index.html", "about.html"], |
| 23 | + removed: [], |
| 24 | + unchanged: ["style.css"] |
| 25 | + ) |
| 26 | + #expect(diff.added.count == 2) |
| 27 | + #expect(diff.isAvailable) |
| 28 | + } |
| 29 | + |
| 30 | + @Test("Diff reports removed files") |
| 31 | + func diffRemoved() { |
| 32 | + let diff = DeploymentDiff( |
| 33 | + added: [], |
| 34 | + removed: ["old-page.html"], |
| 35 | + unchanged: ["index.html"] |
| 36 | + ) |
| 37 | + #expect(diff.removed.count == 1) |
| 38 | + } |
| 39 | + |
| 40 | + @Test("Empty diff means no changes") |
| 41 | + func diffEmpty() { |
| 42 | + let diff = DeploymentDiff( |
| 43 | + added: [], |
| 44 | + removed: [], |
| 45 | + unchanged: ["index.html"] |
| 46 | + ) |
| 47 | + #expect(!diff.hasChanges) |
| 48 | + } |
| 49 | + |
| 50 | + @Test("Diff with changes reports hasChanges") |
| 51 | + func diffHasChanges() { |
| 52 | + let diff = DeploymentDiff( |
| 53 | + added: ["new.html"], |
| 54 | + removed: [], |
| 55 | + unchanged: [] |
| 56 | + ) |
| 57 | + #expect(diff.hasChanges) |
| 58 | + } |
| 59 | + |
| 60 | + @Test("Total file count is correct") |
| 61 | + func diffTotalCount() { |
| 62 | + let diff = DeploymentDiff( |
| 63 | + added: ["a.html"], |
| 64 | + removed: ["b.html"], |
| 65 | + unchanged: ["c.html", "d.html"] |
| 66 | + ) |
| 67 | + #expect(diff.totalFileCount == 4) |
| 68 | + } |
| 69 | + |
| 70 | + @Test("First deploy diff is not available") |
| 71 | + func firstDeployDiff() { |
| 72 | + let diff = DeploymentDiff.firstDeploy(fileCount: 5) |
| 73 | + #expect(!diff.isAvailable) |
| 74 | + #expect(diff.added.count == 5) |
| 75 | + } |
| 76 | + |
| 77 | + // MARK: - DeploymentResult |
| 78 | + |
| 79 | + @Test("Result summary includes file counts") |
| 80 | + func resultSummary() { |
| 81 | + let result = DeploymentResult( |
| 82 | + siteURL: URL(string: "https://example.com"), |
| 83 | + filesUploaded: 10, |
| 84 | + filesDeleted: 2, |
| 85 | + filesSkipped: 5, |
| 86 | + bytesTransferred: 1024, |
| 87 | + duration: .seconds(3), |
| 88 | + isDryRun: false, |
| 89 | + warnings: [] |
| 90 | + ) |
| 91 | + #expect(result.summary.contains("10")) |
| 92 | + #expect(result.summary.contains("example.com")) |
| 93 | + } |
| 94 | + |
| 95 | + @Test("Dry run summary says 'Would deploy'") |
| 96 | + func dryRunSummary() { |
| 97 | + let result = DeploymentResult( |
| 98 | + siteURL: nil, |
| 99 | + filesUploaded: 5, |
| 100 | + filesDeleted: 0, |
| 101 | + filesSkipped: 0, |
| 102 | + bytesTransferred: 0, |
| 103 | + duration: .seconds(1), |
| 104 | + isDryRun: true, |
| 105 | + warnings: [] |
| 106 | + ) |
| 107 | + #expect(result.summary.contains("Would deploy")) |
| 108 | + } |
| 109 | + |
| 110 | + @Test("Real deploy summary says 'Deployed'") |
| 111 | + func realDeploySummary() { |
| 112 | + let result = DeploymentResult( |
| 113 | + siteURL: nil, |
| 114 | + filesUploaded: 3, |
| 115 | + filesDeleted: 0, |
| 116 | + filesSkipped: 0, |
| 117 | + bytesTransferred: 0, |
| 118 | + duration: .seconds(1), |
| 119 | + isDryRun: false, |
| 120 | + warnings: [] |
| 121 | + ) |
| 122 | + #expect(result.summary.contains("Deployed")) |
| 123 | + } |
| 124 | + |
| 125 | + // MARK: - DeploymentManifest |
| 126 | + |
| 127 | + @Test("Manifest is Codable round-trip") |
| 128 | + func manifestCodable() throws { |
| 129 | + let manifest = DeploymentManifest( |
| 130 | + files: ["index.html": "abc123", "style.css": "def456"], |
| 131 | + timestamp: Date(timeIntervalSince1970: 1000000), |
| 132 | + targetName: "GitHub Pages" |
| 133 | + ) |
| 134 | + let data = try JSONEncoder().encode(manifest) |
| 135 | + let decoded = try JSONDecoder().decode(DeploymentManifest.self, from: data) |
| 136 | + |
| 137 | + #expect(decoded.files.count == 2) |
| 138 | + #expect(decoded.files["index.html"] == "abc123") |
| 139 | + #expect(decoded.targetName == "GitHub Pages") |
| 140 | + } |
| 141 | + |
| 142 | + // MARK: - DeploymentEnvironment |
| 143 | + |
| 144 | + @Test("Optional returns nil for missing variable") |
| 145 | + func envOptionalMissing() { |
| 146 | + #expect(DeploymentEnvironment.optional("IGNITE_TEST_NONEXISTENT_VAR_12345") == nil) |
| 147 | + } |
| 148 | + |
| 149 | + @Test("Required throws for missing variable") |
| 150 | + func envRequiredThrows() { |
| 151 | + #expect(throws: DeploymentError.self) { |
| 152 | + try DeploymentEnvironment.required("IGNITE_TEST_NONEXISTENT_VAR_12345") |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + // MARK: - DeploymentError |
| 157 | + |
| 158 | + @Test("Error descriptions are human-readable") |
| 159 | + func errorDescriptions() { |
| 160 | + let errors: [DeploymentError] = [ |
| 161 | + .missingEnvironmentVariable("TOKEN"), |
| 162 | + .missingConfiguration("repo URL"), |
| 163 | + .invalidBuildDirectory(URL(fileURLWithPath: "/tmp/build")), |
| 164 | + .targetRejected(statusCode: 403, message: "Forbidden"), |
| 165 | + .fileError("permission denied"), |
| 166 | + .networkError("timeout") |
| 167 | + ] |
| 168 | + for error in errors { |
| 169 | + #expect(error.errorDescription != nil) |
| 170 | + #expect(!error.errorDescription!.isEmpty) |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments