From 8ef6d168c8c1353d1740e8b0df4a4881982a3edc Mon Sep 17 00:00:00 2001 From: koudo Date: Thu, 6 Jan 2022 20:10:31 +0100 Subject: [PATCH] adding tester --- package.json | 6 +- scripts/test_online.ts | 60 +++++++++++++++++++ src/marmiton_phantom.ts | 2 +- src/{marmitonType.d.ts => marmiton_type.d.ts} | 0 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 scripts/test_online.ts rename src/{marmitonType.d.ts => marmiton_type.d.ts} (100%) diff --git a/package.json b/package.json index 1518486..787b92a 100644 --- a/package.json +++ b/package.json @@ -5,22 +5,26 @@ "main": "index.js", "scripts": { "watch": "tsc --watch", - "auto-deploy": "phantombuster" + "auto-deploy": "phantombuster", + "online": "ts-node ./scripts/test_online.ts" }, "engines": { "node": "14.x" }, "devDependencies": { "@tsconfig/recommended": "^1.0.1", + "@types/axios": "^0.14.0", "@types/node": "^17.0.8", "@types/puppeteer": "1.6.2", "@typescript-eslint/eslint-plugin": "^5.8.0", "@typescript-eslint/parser": "^5.8.0", + "axios": "^0.24.0", "eslint": "^8.5.0", "eslint-config-prettier": "^8.3.0", "eslint-plugin-import": "^2.25.3", "eslint-plugin-prettier": "^4.0.0", "prettier": "^2.5.1", + "ts-node": "^10.4.0", "typescript": "^4.5.4" }, "author": "", diff --git a/scripts/test_online.ts b/scripts/test_online.ts new file mode 100644 index 0000000..017b657 --- /dev/null +++ b/scripts/test_online.ts @@ -0,0 +1,60 @@ +import axios from "axios"; + +const agentid = process.env.AGENT_ID; + +axios.defaults.headers.common["X-Phantombuster-Key"] = process.env + .API_KEY as string; +axios.defaults.headers.common["Content-Type"] = "application/json"; + +const MAX_TIME_TO_RUN = 10000; + +type FetchOutputResult = { + containerId: string; + status: string; + output: string; + outputPos: number; + mostRecentEndedAt: number; + isAgentRunning: boolean; + canSoftAbort: boolean; +}; + +async function getUntilRunStop( + url: string, + startTime: number +): Promise { + const timeRunning = Date.now() - startTime; + + if (timeRunning > MAX_TIME_TO_RUN) { + throw new Error("TIMEOUT: phantom run for too long"); + } + + return await axios + .get(url) + .then((res) => + res.data.status !== "finished" + ? getUntilRunStop(url, startTime) + : res.data + ); +} + +axios + .post<{ containerId: string }>( + "https://api.phantombuster.com/api/v2/agents/launch", + { id: agentid } + ) + .then((res) => res.data.containerId) + .then(() => + getUntilRunStop( + `https://api.phantombuster.com/api/v2/agents/fetch-output?id=${agentid}`, + Date.now() + ) + .then(console.log) + .catch(async (err) => { + console.error(err); + console.log("stopping agent"); + await axios.post("https://api.phantombuster.com/api/v2/agents/stop", { + id: agentid, + }); + process.exit(); + }) + ); diff --git a/src/marmiton_phantom.ts b/src/marmiton_phantom.ts index 99bd3ee..1e49535 100644 --- a/src/marmiton_phantom.ts +++ b/src/marmiton_phantom.ts @@ -1,7 +1,7 @@ /* eslint-disable */ // Phantombuster configuration { "phantombuster command: nodejs" -"phantombuster package: 5" +"phantombuster package: 6" // } /* eslint-enable */ diff --git a/src/marmitonType.d.ts b/src/marmiton_type.d.ts similarity index 100% rename from src/marmitonType.d.ts rename to src/marmiton_type.d.ts