mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-24 22:59:58 -07:00
global: snapshot
This commit is contained in:
@@ -1,5 +1,74 @@
|
||||
import { BrkClient } from "../index.js";
|
||||
|
||||
let client = new BrkClient("http://localhost:3110");
|
||||
const client = new BrkClient("http://localhost:3110");
|
||||
|
||||
let blocks = await client.getBlocks();
|
||||
console.log("Testing idiomatic API...\n");
|
||||
|
||||
// Test getter access (property)
|
||||
console.log("1. Getter access (.by.dateindex):");
|
||||
const all = await client.metrics.price.usd.split.close.by.dateindex;
|
||||
console.log(` Total: ${all.total}, Got: ${all.data.length} items\n`);
|
||||
|
||||
// Test dynamic access (bracket notation)
|
||||
console.log("2. Dynamic access (.by['dateindex']):");
|
||||
const allDynamic = await client.metrics.price.usd.split.close.by["dateindex"];
|
||||
console.log(
|
||||
` Total: ${allDynamic.total}, Got: ${allDynamic.data.length} items\n`,
|
||||
);
|
||||
|
||||
// Test fetch all (explicit .fetch())
|
||||
console.log("3. Explicit .fetch():");
|
||||
const allExplicit =
|
||||
await client.metrics.price.usd.split.close.by.dateindex.fetch();
|
||||
console.log(
|
||||
` Total: ${allExplicit.total}, Got: ${allExplicit.data.length} items\n`,
|
||||
);
|
||||
|
||||
// Test first(n)
|
||||
console.log("4. First 5 items (.first(5)):");
|
||||
const first5 = await client.metrics.price.usd.split.close.by.dateindex.first(5);
|
||||
console.log(
|
||||
` Total: ${first5.total}, Start: ${first5.start}, End: ${first5.end}, Got: ${first5.data.length} items\n`,
|
||||
);
|
||||
|
||||
// Test last(n)
|
||||
console.log("5. Last 5 items (.last(5)):");
|
||||
const last5 = await client.metrics.price.usd.split.close.by.dateindex.last(5);
|
||||
console.log(
|
||||
` Total: ${last5.total}, Start: ${last5.start}, End: ${last5.end}, Got: ${last5.data.length} items\n`,
|
||||
);
|
||||
|
||||
// Test slice(start, end)
|
||||
console.log("6. Slice 10-20 (.slice(10, 20)):");
|
||||
const sliced = await client.metrics.price.usd.split.close.by.dateindex.slice(
|
||||
10,
|
||||
20,
|
||||
);
|
||||
console.log(
|
||||
` Total: ${sliced.total}, Start: ${sliced.start}, End: ${sliced.end}, Got: ${sliced.data.length} items\n`,
|
||||
);
|
||||
|
||||
// Test get(index) - single item
|
||||
console.log("7. Single item (.get(100)):");
|
||||
const single = await client.metrics.price.usd.split.close.by.dateindex.get(100);
|
||||
console.log(
|
||||
` Total: ${single.total}, Start: ${single.start}, End: ${single.end}, Got: ${single.data.length} item(s)\n`,
|
||||
);
|
||||
|
||||
// Test skip(n).take(m) chaining
|
||||
console.log("8. Skip and take (.skip(100).take(10)):");
|
||||
const skipTake = await client.metrics.price.usd.split.close.by.dateindex
|
||||
.skip(100)
|
||||
.take(10);
|
||||
console.log(
|
||||
` Total: ${skipTake.total}, Start: ${skipTake.start}, End: ${skipTake.end}, Got: ${skipTake.data.length} items\n`,
|
||||
);
|
||||
|
||||
// Test fetchCsv
|
||||
console.log("9. Fetch as CSV (.last(3).fetchCsv()):");
|
||||
const csv = await client.metrics.price.usd.split.close.by.dateindex
|
||||
.last(3)
|
||||
.fetchCsv();
|
||||
console.log(` CSV preview: ${csv.substring(0, 100)}...\n`);
|
||||
|
||||
console.log("All tests passed!");
|
||||
|
||||
@@ -6,9 +6,9 @@ import { BrkClient } from "../index.js";
|
||||
|
||||
/**
|
||||
* Recursively collect all metric patterns from the tree.
|
||||
* @param {object} obj
|
||||
* @param {Record<string, any>} obj
|
||||
* @param {string} path
|
||||
* @returns {Array<{path: string, metric: object, indexes: string[]}>}
|
||||
* @returns {Array<{path: string, metric: Record<string, any>, indexes: string[]}>}
|
||||
*/
|
||||
function getAllMetrics(obj, path = "") {
|
||||
const metrics = [];
|
||||
@@ -21,10 +21,10 @@ function getAllMetrics(obj, path = "") {
|
||||
|
||||
const currentPath = path ? `${path}.${key}` : key;
|
||||
|
||||
// Check if this is a metric pattern (has 'by' property with index methods)
|
||||
// Check if this is a metric pattern (has 'by' property with index getters)
|
||||
if (attr.by && typeof attr.by === "object") {
|
||||
const indexes = Object.keys(attr.by).filter(
|
||||
(k) => !k.startsWith("_") && typeof attr.by[k] === "function",
|
||||
(k) => !k.startsWith("_") && typeof attr.by[k] === "object",
|
||||
);
|
||||
if (indexes.length > 0) {
|
||||
metrics.push({ path: currentPath, metric: attr, indexes });
|
||||
@@ -41,54 +41,38 @@ function getAllMetrics(obj, path = "") {
|
||||
}
|
||||
|
||||
async function testAllEndpoints() {
|
||||
const client = new BrkClient("http://localhost:3110");
|
||||
const client = new BrkClient({ baseUrl: "http://localhost:3110", timeout: 15000 });
|
||||
|
||||
const metrics = getAllMetrics(client.tree);
|
||||
const metrics = getAllMetrics(client.metrics);
|
||||
console.log(`\nFound ${metrics.length} metrics`);
|
||||
|
||||
let success = 0;
|
||||
let failed = 0;
|
||||
const errors = [];
|
||||
|
||||
for (const { path, metric, indexes } of metrics) {
|
||||
for (const idxName of indexes) {
|
||||
try {
|
||||
const endpoint = metric.by[idxName]();
|
||||
const res = await endpoint.range(-3);
|
||||
const endpoint = metric.by[idxName];
|
||||
const res = await endpoint.last(1);
|
||||
const count = res.data.length;
|
||||
if (count !== 3) {
|
||||
failed++;
|
||||
const errorMsg = `FAIL: ${path}.by.${idxName}() -> expected 3, got ${count}`;
|
||||
errors.push(errorMsg);
|
||||
console.log(errorMsg);
|
||||
} else {
|
||||
success++;
|
||||
console.log(`OK: ${path}.by.${idxName}() -> ${count} items`);
|
||||
if (count !== 1) {
|
||||
console.log(
|
||||
`FAIL: ${path}.by.${idxName} -> expected 1, got ${count}`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
success++;
|
||||
console.log(`OK: ${path}.by.${idxName} -> ${count} items`);
|
||||
} catch (e) {
|
||||
failed++;
|
||||
const errorMsg = `FAIL: ${path}.by.${idxName}() -> ${e.message}`;
|
||||
errors.push(errorMsg);
|
||||
console.log(errorMsg);
|
||||
console.log(
|
||||
`FAIL: ${path}.by.${idxName} -> ${e instanceof Error ? e.message : e}`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`\n=== Results ===`);
|
||||
console.log(`Success: ${success}`);
|
||||
console.log(`Failed: ${failed}`);
|
||||
|
||||
if (errors.length > 0) {
|
||||
console.log(`\nErrors:`);
|
||||
errors.slice(0, 10).forEach((err) => console.log(` ${err}`));
|
||||
if (errors.length > 10) {
|
||||
console.log(` ... and ${errors.length - 10} more`);
|
||||
}
|
||||
}
|
||||
|
||||
if (failed > 0) {
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
testAllEndpoints();
|
||||
|
||||
Reference in New Issue
Block a user