Skip to content

Commit

Permalink
feat: test
Browse files Browse the repository at this point in the history
  • Loading branch information
yongenaelf committed Sep 5, 2024
1 parent c1dc383 commit 0fd1e66
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
42 changes: 42 additions & 0 deletions app/api/test/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { FileContent } from "@/data/db";
import { getBuildServerBaseUrl, getSolangBuildServerBaseUrl } from "@/lib/env";
import { fileContentToZip } from "@/lib/file-content-to-zip";
import { type NextRequest } from "next/server";
import { v4 as uuidv4 } from "uuid";
import { strFromU8, strToU8 } from "fflate";

export async function POST(request: NextRequest) {
const { files } = (await request.json()) as { files: FileContent[] };

if (files.some((i) => i.path.endsWith(".csproj"))) {
const zippedData = fileContentToZip(files);

const formData = new FormData();
const filePath = uuidv4() + ".zip";
formData.append(
"contractFiles",
new File([zippedData], filePath, { type: "application/zip" }),
filePath
);

const requestInit: RequestInit = {
method: "POST",
body: formData,
redirect: "follow",
};

const response = await fetch(
`${getBuildServerBaseUrl()}/playground/test`,
requestInit
);

if (!response.ok) {
const { message } = await response.json();
return Response.json({ error: message }, { status: response.status });
}

return Response.json({ message: await response.text() });
} else {
return Response.json({ error: "Invalid input files" }, { status: 400 });
}
}
17 changes: 16 additions & 1 deletion components/build-deploy-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useCliCommands } from "./workspace/use-cli-commands";
import useSWR, { mutate } from "swr";
import { db } from "@/data/db";
import { useWorkspaceId } from "./workspace/use-workspace-id";
import { Download, Rocket, ShieldCheck, Wrench } from "lucide-react";
import { Download, Rocket, ShieldCheck, Wrench, TestTube } from "lucide-react";
import UploadModal from "./workspace/upload-modal";
import { Tooltip } from "./tooltip";

Expand All @@ -15,6 +15,7 @@ export function BuildDeployPanel() {
const [isAuditing, setIsAuditing] = useState(false);
const [isBuilding, setIsBuilding] = useState(false);
const [isDeploying, setIsDeploying] = useState(false);
const [isTesting, setIsTesting] = useState(false);
const id = useWorkspaceId();

const { data: isDeployable } = useSWR(
Expand Down Expand Up @@ -61,6 +62,20 @@ export function BuildDeployPanel() {
},
icon: Wrench,
},
{
disabled: isTesting,
title: "Test",
onClick: async () => {
try {
setIsTesting(true);
await commands.test();
} catch (err) {
} finally {
setIsTesting(false);
}
},
icon: TestTube,
},
{
disabled: isBuilding || !isDeployable || isDeploying,
title: "Deploy",
Expand Down
44 changes: 44 additions & 0 deletions components/workspace/use-cli-commands.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function useCliCommands() {
audit - audits the current workspace using AI
</li>
<li className="ml-8">build - builds the current workspace</li>
<li className="ml-8">test - tests the current workspace</li>
<li className="ml-8">deploy - deploys the built smart contract</li>
<li className="ml-8">export - exports the current workspace</li>
<li className="ml-8">
Expand Down Expand Up @@ -147,6 +148,49 @@ export function useCliCommands() {
return;
}
},
test: async () => {
if (typeof id !== "string") throw new Error("id is not string");

const start = `${pathname}/`;
terminalContext.setBufferedContent(
<>
<p>Loading files...</p>
</>
);
const files = (
await db.files.filter((file) => file.path.startsWith(start)).toArray()
).map((file) => ({
path: decodeURIComponent(file.path.replace(start, "")),
contents: file.contents,
}));

terminalContext.setBufferedContent(
<>
<p>Loaded files: {files.map((i) => i.path).join(", ")}</p>
<p>
<Loader2 className="h-4 w-4 animate-spin inline" /> Building...
</p>
</>
);

try {
const res = await fetch(`/api/test`, {
method: "POST",
body: JSON.stringify({ files }),
});
const { message } = await res.json();

terminalContext.setBufferedContent(
<>
<p>{message}</p>
</>
);
} catch (err) {
if (err instanceof Error)
terminalContext.setBufferedContent(<>{err.message}</>);
return;
}
},
deploy: async () => {
if (typeof id !== "string") {
terminalContext.setBufferedContent(
Expand Down

0 comments on commit 0fd1e66

Please sign in to comment.