mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-05 02:44:02 +00:00
Merge branch 'main' into feat/tools-area-breakdowns
This commit is contained in:
115 files changed
+12540
-194
No files matched your search
@@ -0,0 +1,51 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { composeCatalog, defineTool } from './catalog.js';
|
||||
|
||||
for (const intercept of [false, true]) {
|
||||
test(`cancelled query refuses execution${intercept ? ' after an awaited interceptor' : ''}`, async () => {
|
||||
let calls = 0;
|
||||
const controller = new AbortController();
|
||||
const query = defineTool({
|
||||
name: 'fixture_query',
|
||||
kind: 'query',
|
||||
title: 'Fixture',
|
||||
description: 'Offline fixture',
|
||||
inputSchema: { type: 'object' },
|
||||
run: async () => {
|
||||
calls++;
|
||||
return { summary: 'fixture', data: {} };
|
||||
},
|
||||
});
|
||||
const catalog = composeCatalog({
|
||||
tools: [query],
|
||||
interceptors: intercept
|
||||
? [
|
||||
async (call, next) => {
|
||||
await Promise.resolve();
|
||||
controller.abort();
|
||||
return next(call);
|
||||
},
|
||||
]
|
||||
: [],
|
||||
});
|
||||
if (!intercept) controller.abort();
|
||||
await assert.rejects(
|
||||
catalog.call('fixture_query', {}, { signal: controller.signal }),
|
||||
{ name: 'AbortError' },
|
||||
);
|
||||
assert.equal(calls, 0);
|
||||
const fresh = composeCatalog({ tools: [query] });
|
||||
assert.equal(
|
||||
(
|
||||
await fresh.call(
|
||||
'fixture_query',
|
||||
{},
|
||||
{ signal: new AbortController().signal },
|
||||
)
|
||||
).summary,
|
||||
'fixture',
|
||||
);
|
||||
assert.equal(calls, 1);
|
||||
});
|
||||
}
|
||||
@@ -178,6 +178,7 @@ export function composeCatalog({
|
||||
return chain({ tool, args, signal, ...(parent ? { parent } : {}) });
|
||||
};
|
||||
const execute = async ({ tool, args, signal }) => {
|
||||
signal?.throwIfAborted();
|
||||
const problems = validateValue(tool.inputSchema, args);
|
||||
if (problems.length)
|
||||
throw new ToolError('invalid_arguments', problems.join('; '));
|
||||
|
||||
@@ -100,7 +100,9 @@ function sum(values) {
|
||||
|
||||
/** Format an epoch millisecond time as ISO 8601, or null. */
|
||||
export function isoTime(ms) {
|
||||
return Number.isFinite(ms) ? new Date(ms).toISOString() : null;
|
||||
if (!Number.isFinite(ms)) return null;
|
||||
const date = new Date(ms);
|
||||
return Number.isFinite(date.getTime()) ? date.toISOString() : null;
|
||||
}
|
||||
|
||||
/** "1 earthquake", "3 earthquakes". */
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { isoTime } from './results.js';
|
||||
import { getBikeShare } from './queries/mobility.js';
|
||||
test('out-of-range finite timestamps are unknown rather than throwing', async () => {
|
||||
for (const value of [1e30, -1e30, NaN, Infinity, null])
|
||||
assert.equal(isoTime(value), null);
|
||||
assert.equal(isoTime(0), '1970-01-01T00:00:00.000Z');
|
||||
assert.equal(isoTime(8640000000000000), '+275760-09-13T00:00:00.000Z');
|
||||
assert.equal(isoTime(-8640000000000000), '-271821-04-20T00:00:00.000Z');
|
||||
for (const value of [8640000000000001, -8640000000000001, -Infinity])
|
||||
assert.equal(isoTime(value), null);
|
||||
assert.equal(isoTime(-1), '1969-12-31T23:59:59.999Z');
|
||||
const station = {
|
||||
station_id: 's1',
|
||||
name: 'Fixture',
|
||||
lat: 0,
|
||||
lon: 0,
|
||||
capacity: 4,
|
||||
};
|
||||
const result = await getBikeShare.run(
|
||||
{ area: { bbox: [-1, -1, 1, 1] } },
|
||||
{
|
||||
services: {
|
||||
bikeshare: {
|
||||
systems: [
|
||||
{
|
||||
id: 'fixture',
|
||||
city: 'Fixture',
|
||||
centerLat: 0,
|
||||
centerLon: 0,
|
||||
loadRadiusKm: 30,
|
||||
stationInformationUrl: 'information',
|
||||
stationStatusUrl: 'status',
|
||||
},
|
||||
],
|
||||
async getStations(url) {
|
||||
return {
|
||||
data: {
|
||||
stations:
|
||||
url === 'information'
|
||||
? [station]
|
||||
: [
|
||||
{
|
||||
station_id: 's1',
|
||||
last_reported: 1e30,
|
||||
num_bikes_available: 2,
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
assert.equal(result.data.returned, 1);
|
||||
assert.equal(result.data.rows[0].last_reported, null);
|
||||
});
|
||||
Reference in new issue
Block a user