-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestOnlineInstallPackage.js
More file actions
119 lines (102 loc) · 3.44 KB
/
testOnlineInstallPackage.js
File metadata and controls
119 lines (102 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import BosBase from "bosbase";
const baseUrl = process.env.BOSBASE_BASE_URL ?? "http://127.0.0.1:8090";
const authEmail =
process.env.BOSBASE_EMAIL ??
process.env.BOSBASE_SUPERUSER_EMAIL ??
"try@bosbase.com";
const authPassword =
process.env.BOSBASE_PASSWORD ??
process.env.BOSBASE_SUPERUSER_PASSWORD ??
"bosbasepass";
async function main() {
try {
console.log("[INFO] Online install package test starting...");
const pb = new BosBase(baseUrl);
await pb.collection("_superusers").authWithPassword(authEmail, authPassword);
console.log("[SUCCESS] Authenticated as superuser");
if (!pb.scripts) {
throw new Error(
"pb.scripts is not available. Ensure the JS SDK includes the Scripts API.",
);
}
// Install Python dependency into the managed functions environment.
// Preferred: uv (fast). Fallback: pip.
console.log("[INFO] Installing polars...");
let installResult;
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const runAsyncCommand = async (cmd) => {
const started = await pb.scripts.commandAsync(cmd);
const id = started?.id;
if (!id) {
throw new Error(`Async command did not return id for: ${cmd}`);
}
for (let i = 0; i < 600; i++) {
const status = await pb.scripts.commandStatus(id);
if (status?.status === "done") {
return { output: status.output };
}
if (status?.status === "error") {
throw new Error(status.error || `Command failed: ${cmd}`);
}
await sleep(1000);
}
throw new Error(`Timed out waiting for command: ${cmd}`);
};
try {
installResult = await runAsyncCommand("uv add polars");
} catch (err) {
console.warn(
"[WARN] uv install failed, falling back to python -m pip install polars:",
err,
);
installResult = await runAsyncCommand("python -m pip install polars");
}
console.log("[SUCCESS] Install output:\n" + (installResult?.output ?? ""));
const scriptName = `polars_install_${Date.now()}.py`;
const scriptContent = `
import polars as pl
import datetime as dt
def build_df(label: str = ""):
df = pl.DataFrame({"a": [1, 2, 3], "b": [10, 20, 30]})
now = dt.datetime.utcnow().isoformat()
label = str(label or "")
return f"polars_ok label={label} rows={df.height} cols={df.width} now={now}"
`.trim();
// Cleanup in case an earlier run used the same name (unlikely but safe).
try {
await pb.scripts.delete(scriptName);
} catch (_) {
/* ignore */
}
console.log("[INFO] Creating script:", scriptName);
await pb.scripts.create({
name: scriptName,
content: scriptContent,
description: "Install polars + execute smoke test",
});
console.log("[SUCCESS] Script created");
console.log("[INFO] Executing script...");
const execResult = await pb.scripts.execute(scriptName, {
function_name: "build_df",
arguments: ["from_js_sdk"],
});
console.log("[SUCCESS] Execution output:\n" + (execResult?.output ?? ""));
if (!execResult?.output?.includes("polars_ok")) {
throw new Error(`Unexpected execution output: ${execResult?.output}`);
}
if (!execResult?.output?.includes("label=from_js_sdk")) {
throw new Error(`Unexpected execution output label: ${execResult?.output}`);
}
// best-effort cleanup
try {
await pb.scripts.delete(scriptName);
} catch (_) {
/* ignore */
}
console.log("[SUCCESS] Online install package test completed.");
} catch (err) {
console.error("[ERROR] Online install package test failed:", err);
process.exitCode = 1;
}
}
main();