|
| 1 | +import { GitHubClient, GitHubError } from '../src/github/client'; |
| 2 | + |
| 3 | +async function runTest() { |
| 4 | + const pat = process.env.GITHUB_PAT; |
| 5 | + const repo = process.env.GITHUB_REPO; // e.g. "username/repo" |
| 6 | + |
| 7 | + if (!pat || !repo) { |
| 8 | + console.error("Please provide GITHUB_PAT and GITHUB_REPO environment variables."); |
| 9 | + process.exit(1); |
| 10 | + } |
| 11 | + |
| 12 | + const client = new GitHubClient(pat, repo); |
| 13 | + const testPath = 'test-file.txt'; // Root level |
| 14 | + |
| 15 | + try { |
| 16 | + console.log(`1. Testing connection to ${repo}...`); |
| 17 | + await client.testConnection(); |
| 18 | + console.log("Connection successful!"); |
| 19 | + |
| 20 | + console.log(`\n2. Creating file at ${testPath}...`); |
| 21 | + const initialContent = "Hello CommitCode (V1) - " + Date.now(); |
| 22 | + await client.putFile(testPath, initialContent, "chore: create test file"); |
| 23 | + console.log("File created successfully."); |
| 24 | + |
| 25 | + console.log(`\n3. Fetching file to get current SHA...`); |
| 26 | + const file = await client.getFile(testPath); |
| 27 | + console.log("File fetched. SHA:", file.sha); |
| 28 | + |
| 29 | + console.log(`\n4. Updating file with correct SHA...`); |
| 30 | + const updatedContent = "Hello CommitCode (V2) - " + Date.now(); |
| 31 | + await client.putFile(testPath, updatedContent, "chore: update test file", file.sha); |
| 32 | + console.log("File updated successfully."); |
| 33 | + |
| 34 | + console.log(`\n5. Testing 409 Conflict handling (stale SHA)...`); |
| 35 | + try { |
| 36 | + await client.putFile(testPath, "This should fail", "chore: conflicting update", file.sha); // using the old SHA from step 3 |
| 37 | + console.error("ERROR: The update should have failed with a 409 conflict, but it succeeded!"); |
| 38 | + } catch (e: any) { |
| 39 | + if (e instanceof GitHubError && e.status === 409) { |
| 40 | + console.log("SUCCESS: Caught 409 Conflict error as expected!"); |
| 41 | + console.log("Error message:", e.message); |
| 42 | + } else { |
| 43 | + console.error("ERROR: Expected 409 Conflict, got something else:", e); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + } catch (err) { |
| 48 | + console.error("Test failed:", err); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +runTest(); |
0 commit comments