adding tester
This commit is contained in:
parent
6418918a0b
commit
8ef6d168c8
@ -5,22 +5,26 @@
|
|||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"watch": "tsc --watch",
|
"watch": "tsc --watch",
|
||||||
"auto-deploy": "phantombuster"
|
"auto-deploy": "phantombuster",
|
||||||
|
"online": "ts-node ./scripts/test_online.ts"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": "14.x"
|
"node": "14.x"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@tsconfig/recommended": "^1.0.1",
|
"@tsconfig/recommended": "^1.0.1",
|
||||||
|
"@types/axios": "^0.14.0",
|
||||||
"@types/node": "^17.0.8",
|
"@types/node": "^17.0.8",
|
||||||
"@types/puppeteer": "1.6.2",
|
"@types/puppeteer": "1.6.2",
|
||||||
"@typescript-eslint/eslint-plugin": "^5.8.0",
|
"@typescript-eslint/eslint-plugin": "^5.8.0",
|
||||||
"@typescript-eslint/parser": "^5.8.0",
|
"@typescript-eslint/parser": "^5.8.0",
|
||||||
|
"axios": "^0.24.0",
|
||||||
"eslint": "^8.5.0",
|
"eslint": "^8.5.0",
|
||||||
"eslint-config-prettier": "^8.3.0",
|
"eslint-config-prettier": "^8.3.0",
|
||||||
"eslint-plugin-import": "^2.25.3",
|
"eslint-plugin-import": "^2.25.3",
|
||||||
"eslint-plugin-prettier": "^4.0.0",
|
"eslint-plugin-prettier": "^4.0.0",
|
||||||
"prettier": "^2.5.1",
|
"prettier": "^2.5.1",
|
||||||
|
"ts-node": "^10.4.0",
|
||||||
"typescript": "^4.5.4"
|
"typescript": "^4.5.4"
|
||||||
},
|
},
|
||||||
"author": "",
|
"author": "",
|
||||||
|
|||||||
60
scripts/test_online.ts
Normal file
60
scripts/test_online.ts
Normal file
@ -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<FetchOutputResult> {
|
||||||
|
const timeRunning = Date.now() - startTime;
|
||||||
|
|
||||||
|
if (timeRunning > MAX_TIME_TO_RUN) {
|
||||||
|
throw new Error("TIMEOUT: phantom run for too long");
|
||||||
|
}
|
||||||
|
|
||||||
|
return await axios
|
||||||
|
.get<FetchOutputResult>(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();
|
||||||
|
})
|
||||||
|
);
|
||||||
@ -1,7 +1,7 @@
|
|||||||
/* eslint-disable */
|
/* eslint-disable */
|
||||||
// Phantombuster configuration {
|
// Phantombuster configuration {
|
||||||
"phantombuster command: nodejs"
|
"phantombuster command: nodejs"
|
||||||
"phantombuster package: 5"
|
"phantombuster package: 6"
|
||||||
// }
|
// }
|
||||||
/* eslint-enable */
|
/* eslint-enable */
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user