Skip to content

Commit 8c59265

Browse files
committed
test(price-pusher): add unit tests for readMnemonic + bump minor
Per review feedback: - Bump @pythnetwork/price-pusher to 10.5.0 (minor) — backward-compatible feature addition (MNEMONIC env var fallback). 10.4.0 was consumed by #3687 hermes-access-token. - Add tests/utils.test.ts covering all three readMnemonic paths plus the file-precedence-over-env behaviour: file source, env fallback, empty-string path (treated as not supplied), file > env precedence, missing-both error, empty-env error. - Wire `test:unit` script into apps/price_pusher/package.json so the workspace runner picks it up. `pnpm --filter @pythnetwork/price-pusher exec test-unit` — 6 passing.
1 parent 5243016 commit 8c59265

2 files changed

Lines changed: 65 additions & 2 deletions

File tree

apps/price_pusher/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,10 @@
215215
"dev": "ts-node src/index.ts",
216216
"prepublishOnly": "pnpm run build",
217217
"start": "node dist/index.cjs",
218-
"test:types": "tsc"
218+
"test:types": "tsc",
219+
"test:unit": "test-unit"
219220
},
220221
"type": "module",
221222
"types": "./dist/index.d.ts",
222-
"version": "10.4.0"
223+
"version": "10.5.0"
223224
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// biome-ignore-all lint/style/noProcessEnv: test file manipulates env vars
2+
3+
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
4+
import { tmpdir } from "node:os";
5+
import { join } from "node:path";
6+
7+
import { readMnemonic } from "../src/utils.js";
8+
9+
describe("readMnemonic", () => {
10+
let tmpDir: string;
11+
const originalMnemonic = process.env.MNEMONIC;
12+
13+
beforeEach(() => {
14+
tmpDir = mkdtempSync(join(tmpdir(), "price-pusher-test-"));
15+
delete process.env.MNEMONIC;
16+
});
17+
18+
afterEach(() => {
19+
rmSync(tmpDir, { recursive: true, force: true });
20+
if (originalMnemonic === undefined) {
21+
delete process.env.MNEMONIC;
22+
} else {
23+
process.env.MNEMONIC = originalMnemonic;
24+
}
25+
});
26+
27+
it("reads mnemonic from file when path supplied", () => {
28+
const path = join(tmpDir, "mnemonic");
29+
writeFileSync(path, "from file mnemonic\n");
30+
expect(readMnemonic(path)).toBe("from file mnemonic");
31+
});
32+
33+
it("falls back to MNEMONIC env var when no file path supplied", () => {
34+
process.env.MNEMONIC = "from env mnemonic";
35+
expect(readMnemonic(undefined)).toBe("from env mnemonic");
36+
});
37+
38+
it("treats empty-string file path as not supplied", () => {
39+
process.env.MNEMONIC = "from env mnemonic";
40+
expect(readMnemonic("")).toBe("from env mnemonic");
41+
});
42+
43+
it("file source takes precedence over env var", () => {
44+
const path = join(tmpDir, "mnemonic");
45+
writeFileSync(path, "from file\n");
46+
process.env.MNEMONIC = "from env";
47+
expect(readMnemonic(path)).toBe("from file");
48+
});
49+
50+
it("throws when neither file nor env var supplied", () => {
51+
expect(() => readMnemonic(undefined)).toThrow(
52+
/No mnemonic provided/,
53+
);
54+
});
55+
56+
it("throws when env var is empty string", () => {
57+
process.env.MNEMONIC = "";
58+
expect(() => readMnemonic(undefined)).toThrow(
59+
/No mnemonic provided/,
60+
);
61+
});
62+
});

0 commit comments

Comments
 (0)