Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1326725995 | |||
| 508fa39835 |
@@ -5,8 +5,28 @@ on:
|
|||||||
types: [created]
|
types: [created]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
|
test:
|
||||||
|
name: Test
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout source code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Setup Bun
|
||||||
|
uses: oven-sh/setup-bun@v2
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: bun install --frozen-lockfile
|
||||||
|
|
||||||
|
- name: Generate Prisma client
|
||||||
|
run: DATABASE_URL="postgresql://dummy:dummy@localhost:5432/dummy" bunx prisma generate
|
||||||
|
|
||||||
|
- name: Run tests
|
||||||
|
run: bun test --preload ./tests/setup.ts
|
||||||
|
|
||||||
build:
|
build:
|
||||||
name: Build
|
name: Build
|
||||||
|
needs: [test]
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: read
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
name: Tests
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: ["**"]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
name: Test
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout source code
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Setup Bun
|
||||||
|
uses: oven-sh/setup-bun@v2
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: bun install --frozen-lockfile
|
||||||
|
|
||||||
|
- name: Generate Prisma client
|
||||||
|
run: DATABASE_URL="postgresql://dummy:dummy@localhost:5432/dummy" bunx prisma generate
|
||||||
|
|
||||||
|
- name: Run tests
|
||||||
|
run: bun test --preload ./tests/setup.ts
|
||||||
Vendored
+5
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"chat.tools.terminal.autoApprove": {
|
||||||
|
"bun": true
|
||||||
|
}
|
||||||
|
}
|
||||||
+4
-1
@@ -67,4 +67,7 @@ RUN bun install --frozen-lockfile
|
|||||||
COPY prisma/ prisma/
|
COPY prisma/ prisma/
|
||||||
COPY prisma.config.ts ./
|
COPY prisma.config.ts ./
|
||||||
|
|
||||||
CMD ["bunx", "prisma", "migrate", "deploy"]
|
COPY prisma/migrate-entrypoint.sh ./prisma/migrate-entrypoint.sh
|
||||||
|
RUN chmod +x prisma/migrate-entrypoint.sh
|
||||||
|
|
||||||
|
CMD ["sh", "prisma/migrate-entrypoint.sh"]
|
||||||
@@ -19,6 +19,7 @@
|
|||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "NODE_ENV=development bun --watch src/index.ts",
|
"dev": "NODE_ENV=development bun --watch src/index.ts",
|
||||||
|
"test": "bun test --preload ./tests/setup.ts",
|
||||||
"db:gen": "prisma generate",
|
"db:gen": "prisma generate",
|
||||||
"db:push": "prisma migrate dev --skip-generate",
|
"db:push": "prisma migrate dev --skip-generate",
|
||||||
"db:deploy": "prisma migrate deploy",
|
"db:deploy": "prisma migrate deploy",
|
||||||
|
|||||||
Executable
+44
@@ -0,0 +1,44 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -e
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# 1. Resolve any previously failed migrations so deploy can proceed.
|
||||||
|
# Prisma marks failed migrations in _prisma_migrations; we roll them back
|
||||||
|
# so the current run can re-apply them cleanly.
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
echo "[migrate] Checking for failed migrations..."
|
||||||
|
FAILED=$(bunx prisma migrate status 2>&1 || true)
|
||||||
|
|
||||||
|
# Extract failed migration names and mark them as rolled back
|
||||||
|
echo "$FAILED" | grep -oE '[0-9]{14}_[a-z_]+' | while read -r MIGRATION; do
|
||||||
|
# Only resolve if the status output says it failed
|
||||||
|
if echo "$FAILED" | grep -q "failed"; then
|
||||||
|
echo "[migrate] Resolving failed migration: $MIGRATION"
|
||||||
|
bunx prisma migrate resolve --rolled-back "$MIGRATION" 2>/dev/null || true
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# 2. Generate diff SQL between current migrations and the Prisma schema.
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
DIFF_SQL=$(bunx prisma migrate diff \
|
||||||
|
--from-migrations prisma/migrations \
|
||||||
|
--to-schema-datamodel prisma/schema.prisma \
|
||||||
|
--script 2>/dev/null || true)
|
||||||
|
|
||||||
|
# If there's a meaningful diff (not just empty/comments), create a migration
|
||||||
|
if [ -n "$DIFF_SQL" ] && echo "$DIFF_SQL" | grep -qvE '^\s*$|^--'; then
|
||||||
|
TIMESTAMP=$(date -u +"%Y%m%d%H%M%S")
|
||||||
|
MIGRATION_DIR="prisma/migrations/${TIMESTAMP}_auto_generated"
|
||||||
|
mkdir -p "$MIGRATION_DIR"
|
||||||
|
echo "$DIFF_SQL" > "$MIGRATION_DIR/migration.sql"
|
||||||
|
echo "[migrate] Created migration: $MIGRATION_DIR"
|
||||||
|
else
|
||||||
|
echo "[migrate] Schema and migrations are in sync — no migration needed."
|
||||||
|
fi
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# 3. Deploy all pending migrations.
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
echo "[migrate] Running prisma migrate deploy..."
|
||||||
|
bunx prisma migrate deploy
|
||||||
+32
-10
@@ -45,36 +45,58 @@ if (!existingAdmin) {
|
|||||||
events.emit("role:created", new RoleController(created));
|
events.emit("role:created", new RoleController(created));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper to run a startup sync safely — failures are logged but never crash the process.
|
||||||
|
const safeStartup = async (label: string, fn: () => Promise<void>) => {
|
||||||
|
try {
|
||||||
|
await fn();
|
||||||
|
} catch (err) {
|
||||||
|
console.error(
|
||||||
|
`[startup] ${label} failed — will retry on next interval`,
|
||||||
|
err,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Refresh the internal list of companies every minute
|
// Refresh the internal list of companies every minute
|
||||||
await refreshCompanies();
|
await safeStartup("refreshCompanies", refreshCompanies);
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
return refreshCompanies();
|
return refreshCompanies().catch((err) =>
|
||||||
|
console.error("[interval] refreshCompanies failed", err),
|
||||||
|
);
|
||||||
}, 60 * 1000);
|
}, 60 * 1000);
|
||||||
|
|
||||||
// Refresh the internal catalog every minute
|
// Refresh the internal catalog every minute
|
||||||
await refreshCatalog();
|
await safeStartup("refreshCatalog", refreshCatalog);
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
return refreshCatalog();
|
return refreshCatalog().catch((err) =>
|
||||||
|
console.error("[interval] refreshCatalog failed", err),
|
||||||
|
);
|
||||||
}, 60 * 1000);
|
}, 60 * 1000);
|
||||||
|
|
||||||
// Refresh inventory on hand every 2 minutes
|
// Refresh inventory on hand every 2 minutes
|
||||||
await refreshInventory();
|
await safeStartup("refreshInventory", refreshInventory);
|
||||||
setInterval(
|
setInterval(
|
||||||
() => {
|
() => {
|
||||||
return refreshInventory();
|
return refreshInventory().catch((err) =>
|
||||||
|
console.error("[interval] refreshInventory failed", err),
|
||||||
|
);
|
||||||
},
|
},
|
||||||
2 * 60 * 1000,
|
2 * 60 * 1000,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Refresh opportunities every minute
|
// Refresh opportunities every minute
|
||||||
await refreshOpportunities();
|
await safeStartup("refreshOpportunities", refreshOpportunities);
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
return refreshOpportunities();
|
return refreshOpportunities().catch((err) =>
|
||||||
|
console.error("[interval] refreshOpportunities failed", err),
|
||||||
|
);
|
||||||
}, 60 * 1000);
|
}, 60 * 1000);
|
||||||
|
|
||||||
await unifiSites.syncSites();
|
await safeStartup("syncSites", () => unifiSites.syncSites());
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
return unifiSites.syncSites();
|
return unifiSites
|
||||||
|
.syncSites()
|
||||||
|
.catch((err) => console.error("[interval] syncSites failed", err));
|
||||||
}, 60 * 1000);
|
}, 60 * 1000);
|
||||||
|
|
||||||
Bun.serve({
|
Bun.serve({
|
||||||
|
|||||||
+17
-5
@@ -4,6 +4,18 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { mock } from "bun:test";
|
import { mock } from "bun:test";
|
||||||
|
import crypto from "crypto";
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Generate a real RSA key pair for modules that call crypto.createPrivateKey()
|
||||||
|
// at import time (e.g. readSecureValue.ts).
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
const { privateKey: _testPrivateKey, publicKey: _testPublicKey } =
|
||||||
|
crypto.generateKeyPairSync("rsa", {
|
||||||
|
modulusLength: 2048,
|
||||||
|
privateKeyEncoding: { type: "pkcs8", format: "pem" },
|
||||||
|
publicKeyEncoding: { type: "spki", format: "pem" },
|
||||||
|
});
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Mock the constants module — almost every source file imports from here.
|
// Mock the constants module — almost every source file imports from here.
|
||||||
@@ -17,11 +29,11 @@ mock.module("../src/constants", () => ({
|
|||||||
sessionDuration: 30 * 24 * 60 * 60_000,
|
sessionDuration: 30 * 24 * 60 * 60_000,
|
||||||
accessTokenDuration: "10min",
|
accessTokenDuration: "10min",
|
||||||
refreshTokenDuration: "30d",
|
refreshTokenDuration: "30d",
|
||||||
accessTokenPrivateKey: "mock-access-private-key",
|
accessTokenPrivateKey: _testPrivateKey,
|
||||||
refreshTokenPrivateKey: "mock-refresh-private-key",
|
refreshTokenPrivateKey: _testPrivateKey,
|
||||||
permissionsPrivateKey: "mock-permissions-private-key",
|
permissionsPrivateKey: _testPrivateKey,
|
||||||
secureValuesPrivateKey: "mock-secure-values-private-key",
|
secureValuesPrivateKey: _testPrivateKey,
|
||||||
secureValuesPublicKey: "mock-secure-values-public-key",
|
secureValuesPublicKey: _testPublicKey,
|
||||||
msalClient: { acquireTokenByCode: mock(() => Promise.resolve({})) },
|
msalClient: { acquireTokenByCode: mock(() => Promise.resolve({})) },
|
||||||
connectWiseApi: {
|
connectWiseApi: {
|
||||||
get: mock(() => Promise.resolve({ data: {} })),
|
get: mock(() => Promise.resolve({ data: {} })),
|
||||||
|
|||||||
Reference in New Issue
Block a user