Skip to content

Commit

Permalink
Update and commit tests (#9)
Browse files Browse the repository at this point in the history
### What

- [x] Update tests for failing builds
- [x] Commit code to repos
- [x] Genericize test finder
- [x] Commit right code to repo
  • Loading branch information
slavingia authored Sep 25, 2024
2 parents d7ae524 + bc8a78c commit 4dd8176
Show file tree
Hide file tree
Showing 14 changed files with 1,379 additions and 248 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ POSTGRES_USER="default"
POSTGRES_HOST="***"
POSTGRES_PASSWORD="***"
POSTGRES_DATABASE="***"

#ai
OPENAI_API_KEY="sk-proj-***"
ANTHROPIC_API_KEY="sk-ant-***"
123 changes: 3 additions & 120 deletions app/(dashboard)/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,13 @@ import { useState, useEffect } from "react";
import { Button } from "@/components/ui/button";
import { Loader2, AlertCircle } from "lucide-react";
import { PullRequestItem } from "./pull-request";
import { PullRequest, TestFile } from "./types";
import {
getAssignedPullRequests,
commitChangesToPullRequest,
} from "@/lib/github";
import { PullRequest } from "./types";
import { getAssignedPullRequests } from "@/lib/github";

export default function DashboardPage() {
const [pullRequests, setPullRequests] = useState<PullRequest[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [selectedPR, setSelectedPR] = useState<PullRequest | null>(null);
const [testFiles, setTestFiles] = useState<TestFile[]>([]);
const [selectedFiles, setSelectedFiles] = useState<Record<string, boolean>>(
{}
);
const [expandedFiles, setExpandedFiles] = useState<Record<string, boolean>>(
{}
);
const [analyzing, setAnalyzing] = useState(false);
const [loadingPR, setLoadingPR] = useState<number | null>(null);

useEffect(() => {
const fetchAndSetPullRequests = async () => {
Expand Down Expand Up @@ -62,64 +49,8 @@ export default function DashboardPage() {
fetchAndSetPullRequests();
}, []);

const handleOpenTests = (pr: PullRequest, mode: "write" | "update") => {
setSelectedPR(pr);
setAnalyzing(true);
setLoadingPR(pr.id);

// Simulating API call to get test files
// TODO: update this to dynamically generate based on the PR diff and spec file directory
setTimeout(() => {
const mockTestFiles: TestFile[] = [];

if (mode === "write") {
mockTestFiles.push({
name: "signup_spec.rb",
oldContent: "",
newContent:
"describe 'Signup' do\n it 'allows new user to sign up' do\n # Test code here\n end\nend",
isEntirelyNew: true,
});

mockTestFiles.push({
name: "login_spec.rb",
oldContent:
"describe 'Login' do\n it 'allows existing user to log in' do\n visit '/login'\n fill_in 'Email', with: '[email protected]'\n fill_in 'Password', with: 'password123'\n click_button 'Log In'\n expect(page).to have_content('Welcome back!')\n end\nend",
newContent:
"describe 'Login' do\n it 'allows existing user to log in' do\n visit '/login'\n fill_in 'Email', with: '[email protected]'\n fill_in 'Password', with: 'password123'\n click_button 'Log In'\n expect(page).to have_content('Welcome back!')\n end\n\n\n it 'shows error message for invalid credentials' do\n visit '/login'\n fill_in 'Email', with: '[email protected]'\n fill_in 'Password', with: 'wrongpassword'\n click_button 'Log In'\n expect(page).to have_content('Invalid email or password')\n end\nend",
isEntirelyNew: false,
});
} else if (mode === "update") {
mockTestFiles.push({
name: "logic_spec.rb",
oldContent:
"describe 'BusinessLogic' do\n it 'calculates total correctly' do\n expect(calculate_total(10, 5)).to eq(15)\n end\n\n it 'applies discount' do\n expect(apply_discount(100, 0.1)).to eq(90)\n end\nend",
newContent:
"describe 'BusinessLogic' do\n it 'calculates total correctly' do\n expect(calculate_total(10, 5)).to eq(15)\n end\n\n it 'applies percentage discount' do\n expect(apply_percentage_discount(100, 10)).to eq(90)\n end\n\n it 'applies flat discount' do\n expect(apply_flat_discount(100, 10)).to eq(90)\n end\nend",
isEntirelyNew: false,
});
}

setTestFiles(mockTestFiles);
const newSelectedFiles: Record<string, boolean> = {};
const newExpandedFiles: Record<string, boolean> = {};
mockTestFiles.forEach((file) => {
newExpandedFiles[file.name] = true;
if (mode === "update") {
newSelectedFiles[file.name] = true;
}
});
setSelectedFiles(newSelectedFiles);
setExpandedFiles(newExpandedFiles);
setAnalyzing(false);
setLoadingPR(null);
}, 1000);
};

const handleReconnectGitHub = async () => {
try {
// Redirect to GitHub OAuth flow to get new permissions
// Make sure NEXT_PUBLIC_GITHUB_CLIENT_ID is properly set in your environment variables
const clientId = process.env.NEXT_PUBLIC_GITHUB_CLIENT_ID;
const baseUrl = process.env.NEXT_PUBLIC_BASE_URL;
if (!clientId) {
Expand All @@ -137,28 +68,6 @@ export default function DashboardPage() {
}
};

const handleConfirmChanges = async () => {
if (!selectedPR) return;

setLoadingPR(selectedPR.id);
try {
const filesToCommit = testFiles.filter(
(file) => selectedFiles[file.name]
);
// TODO: await commitChangesToPullRequest(selectedPR, filesToCommit);
// Reset state after successful commit
setSelectedPR(null);
setTestFiles([]);
setSelectedFiles({});
setExpandedFiles({});
} catch (error) {
console.error("Error committing changes:", error);
setError("Failed to commit changes. Please try again.");
} finally {
setLoadingPR(null);
}
};

if (loading) {
return (
<div className="flex justify-center items-center h-full">
Expand Down Expand Up @@ -191,33 +100,7 @@ export default function DashboardPage() {
{pr.repository.full_name}
</h3>
</div>
<PullRequestItem
pullRequest={pr}
onOpenTests={handleOpenTests}
selectedPR={selectedPR}
testFiles={testFiles}
selectedFiles={selectedFiles}
expandedFiles={expandedFiles}
analyzing={analyzing}
onFileToggle={(fileName) => {
setSelectedFiles((prev) => ({
...prev,
[fileName]: !prev[fileName],
}));
setExpandedFiles((prev) => ({
...prev,
[fileName]: !prev[fileName],
}));
}}
onConfirmChanges={handleConfirmChanges}
onCancelChanges={() => {
setSelectedPR(null);
setTestFiles([]);
setSelectedFiles({});
setExpandedFiles({});
}}
loadingPR={loadingPR}
/>
<PullRequestItem pullRequest={pr} />
</li>
))}
</ul>
Expand Down
Loading

0 comments on commit 4dd8176

Please sign in to comment.