Files
worldmonitor/tests/stock-backtest.test.mts
Sebastien Melki fcbb8bc0a1 feat(proto): unified OpenAPI bundle via sebuf v0.11.0 (#3341)
* feat(proto): generate unified worldmonitor.openapi.yaml bundle

Adds a third protoc-gen-openapiv3 invocation that merges every service
into a single docs/api/worldmonitor.openapi.yaml spanning all 68 RPCs,
using the new bundle support shipped in sebuf 0.11.0
(SebastienMelki/sebuf#158).

Per-service YAML/JSON files are untouched and continue to back the
Mintlify docs in docs/docs.json. The bundle runs with strategy: all and
bundle_only=true so only the aggregate file is emitted, avoiding
duplicate-output conflicts with the existing per-service generator.

Requires protoc-gen-openapiv3 >= v0.11.0 locally:
  go install github.com/SebastienMelki/sebuf/cmd/protoc-gen-openapiv3@v0.11.0

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* docs(proto): bump sebuf to v0.11.0 and document unified OpenAPI bundle

- Makefile: SEBUF_VERSION v0.7.0 → v0.11.0 (required for bundle support).
- proto/buf.gen.yaml: point bundle_server at https://api.worldmonitor.app.
- CONTRIBUTING.md: new "OpenAPI Output" section covering per-service specs
  vs the unified worldmonitor.openapi.yaml bundle, plus a note that all
  three sebuf plugins must be installed from the pinned version.
- AGENTS.md: clarify that `make generate` also produces the unified spec
  and requires sebuf v0.11.0.
- CHANGELOG.md: Unreleased entry announcing the bundle and version bump.

Also regenerates the bundle with the updated server URL.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* chore(codegen): regenerate TS client/server with sebuf v0.11.0

Mechanical output of the bumped protoc-gen-ts-client and
protoc-gen-ts-server. Two behavioral improvements roll in from sebuf:

- Proto enum fields now use the proper `*_UNSPECIFIED` sentinel in
  default-value checks instead of the empty string, so generated
  query-string serializers correctly omit enum params only when they
  actually equal the proto default.
- `repeated string` query params now serialize via
  `forEach(v => params.append(...))` instead of being coerced through
  `String(req.field)`, matching the existing `parseStringArray()`
  contract on the server side.

All files also drop the `// @ts-nocheck` header that earlier sebuf
versions emitted — 0.11.0 output type-checks cleanly under our tsconfig.

No hand edits. Reproduce with `make install-plugins && make generate`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(proto): bump sebuf v0.11.0 → v0.11.1, realign tests with repeated-param wire format

- Bump SEBUF_VERSION to v0.11.1, pulling in the OpenAPI fix for repeated
  scalar query params (SebastienMelki/sebuf#161). `repeated string` fields
  now emit `type: array` + `items.type: string` + `style: form` +
  `explode: true` instead of `type: string`, so SDK generators consuming
  the unified bundle produce correct array clients.
- Regenerate all 12 OpenAPI specs (unified bundle + Aviation, Economic,
  Infrastructure, Market, Trade per-service). TS client/server codegen
  is byte-identical to v0.11.0 — only the OpenAPI emitter was out of sync.
- Update three tests that asserted the pre-v0.11 comma-joined wire format
  (`symbols=AAPL,MSFT`) to match the current repeated-param form
  (`symbols=AAPL&symbols=MSFT`) produced by `params.append(...)`:
  - tests/market-service-symbol-casing.test.mjs (2 cases: getAll)
  - tests/stock-analysis-history.test.mts
  - tests/stock-backtest.test.mts

Locally: test:data 6619/6619 pass, typecheck clean, lint exit 0.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Apply suggestions from code review

Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
2026-04-23 16:24:03 +03:00

267 lines
9.8 KiB
TypeScript

import assert from 'node:assert/strict';
import { afterEach, describe, it } from 'node:test';
import { backtestStock } from '../server/worldmonitor/market/v1/backtest-stock.ts';
import { listStoredStockBacktests } from '../server/worldmonitor/market/v1/list-stored-stock-backtests.ts';
import { MarketServiceClient } from '../src/generated/client/worldmonitor/market/v1/service_client.ts';
const originalFetch = globalThis.fetch;
const originalRedisUrl = process.env.UPSTASH_REDIS_REST_URL;
const originalRedisToken = process.env.UPSTASH_REDIS_REST_TOKEN;
function buildReplaySeries(length = 120) {
const candles: Array<{
timestamp: number;
open: number;
high: number;
low: number;
close: number;
volume: number;
}> = [];
let price = 100;
for (let index = 0; index < length; index++) {
const drift = 0.28;
const pullback = index % 14 >= 10 && index % 14 <= 12 ? -0.35 : 0;
const noise = index % 9 === 0 ? 0.12 : index % 11 === 0 ? -0.08 : 0.04;
const change = drift + pullback + noise;
const open = price;
price = Math.max(20, price + change);
const close = price;
const high = Math.max(open, close) + 0.7;
const low = Math.min(open, close) - 0.6;
const volume = index % 14 >= 10 && index % 14 <= 12 ? 780_000 : 1_120_000;
candles.push({
timestamp: 1_700_000_000 + (index * 86_400),
open,
high,
low,
close,
volume,
});
}
return candles;
}
afterEach(() => {
globalThis.fetch = originalFetch;
if (originalRedisUrl == null) delete process.env.UPSTASH_REDIS_REST_URL;
else process.env.UPSTASH_REDIS_REST_URL = originalRedisUrl;
if (originalRedisToken == null) delete process.env.UPSTASH_REDIS_REST_TOKEN;
else process.env.UPSTASH_REDIS_REST_TOKEN = originalRedisToken;
});
function createRedisAwareBacktestFetch(mockChartPayload: unknown) {
const redis = new Map<string, string>();
const sortedSets = new Map<string, Array<{ member: string; score: number }>>();
const upsertSortedSet = (key: string, score: number, member: string) => {
const next = (sortedSets.get(key) ?? []).filter((item) => item.member !== member);
next.push({ member, score });
next.sort((a, b) => a.score - b.score || a.member.localeCompare(b.member));
sortedSets.set(key, next);
};
return (async (input: RequestInfo | URL, init?: RequestInit) => {
const url = typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url;
if (url.includes('query1.finance.yahoo.com')) {
return new Response(JSON.stringify(mockChartPayload), { status: 200 });
}
if (url.startsWith(process.env.UPSTASH_REDIS_REST_URL || '')) {
const parsed = new URL(url);
if (parsed.pathname.startsWith('/get/')) {
const key = decodeURIComponent(parsed.pathname.slice('/get/'.length));
return new Response(JSON.stringify({ result: redis.get(key) ?? null }), { status: 200 });
}
if (parsed.pathname.startsWith('/set/')) {
const parts = parsed.pathname.split('/');
const key = decodeURIComponent(parts[2] || '');
const value = decodeURIComponent(parts[3] || '');
redis.set(key, value);
return new Response(JSON.stringify({ result: 'OK' }), { status: 200 });
}
if (parsed.pathname === '/pipeline') {
const commands = JSON.parse(typeof init?.body === 'string' ? init.body : '[]') as string[][];
const result = commands.map((command) => {
const [verb, key = '', ...args] = command;
if (verb === 'GET') {
return { result: redis.get(key) ?? null };
}
if (verb === 'SET') {
redis.set(key, args[0] || '');
return { result: 'OK' };
}
if (verb === 'ZADD') {
for (let index = 0; index < args.length; index += 2) {
upsertSortedSet(key, Number(args[index] || 0), args[index + 1] || '');
}
return { result: 1 };
}
if (verb === 'ZREVRANGE') {
const items = [...(sortedSets.get(key) ?? [])].sort((a, b) => b.score - a.score || a.member.localeCompare(b.member));
const start = Number(args[0] || 0);
const stop = Number(args[1] || 0);
return { result: items.slice(start, stop + 1).map((item) => item.member) };
}
if (verb === 'ZREM') {
const removals = new Set(args);
sortedSets.set(key, (sortedSets.get(key) ?? []).filter((item) => !removals.has(item.member)));
return { result: removals.size };
}
if (verb === 'EXPIRE') {
return { result: 1 };
}
throw new Error(`Unexpected pipeline command: ${verb}`);
});
return new Response(JSON.stringify(result), { status: 200 });
}
}
throw new Error(`Unexpected URL: ${url}`);
}) as typeof fetch;
}
describe('backtestStock handler', () => {
it('replays actionable stock-analysis signals over recent Yahoo history', async () => {
const candles = buildReplaySeries();
const mockChartPayload = {
chart: {
result: [
{
meta: {
currency: 'USD',
regularMarketPrice: 148,
previousClose: 147,
},
timestamp: candles.map((candle) => candle.timestamp),
indicators: {
quote: [
{
open: candles.map((candle) => candle.open),
high: candles.map((candle) => candle.high),
low: candles.map((candle) => candle.low),
close: candles.map((candle) => candle.close),
volume: candles.map((candle) => candle.volume),
},
],
},
},
],
},
};
globalThis.fetch = (async (input: RequestInfo | URL) => {
const url = typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url;
if (url.includes('query1.finance.yahoo.com')) {
return new Response(JSON.stringify(mockChartPayload), { status: 200 });
}
throw new Error(`Unexpected URL: ${url}`);
}) as typeof fetch;
const response = await backtestStock({} as never, {
symbol: 'AAPL',
name: 'Apple',
evalWindowDays: 10,
});
assert.equal(response.available, true);
assert.equal(response.symbol, 'AAPL');
assert.equal(response.currency, 'USD');
assert.ok(response.actionableEvaluations > 0);
assert.ok(response.evaluations.length > 0);
assert.match(response.evaluations[0]?.analysisId || '', /^ledger:/);
assert.match(response.latestSignal, /buy/i);
assert.match(response.summary, /stored analysis/i);
});
});
describe('server-backed stored stock backtests', () => {
it('stores fresh backtests in Redis and serves them back in batch', async () => {
const candles = buildReplaySeries();
const mockChartPayload = {
chart: {
result: [
{
meta: {
currency: 'USD',
regularMarketPrice: 148,
previousClose: 147,
},
timestamp: candles.map((candle) => candle.timestamp),
indicators: {
quote: [
{
open: candles.map((candle) => candle.open),
high: candles.map((candle) => candle.high),
low: candles.map((candle) => candle.low),
close: candles.map((candle) => candle.close),
volume: candles.map((candle) => candle.volume),
},
],
},
},
],
},
};
process.env.UPSTASH_REDIS_REST_URL = 'https://redis.example';
process.env.UPSTASH_REDIS_REST_TOKEN = 'token';
globalThis.fetch = createRedisAwareBacktestFetch(mockChartPayload);
const response = await backtestStock({} as never, {
symbol: 'AAPL',
name: 'Apple',
evalWindowDays: 10,
});
assert.equal(response.available, true);
const stored = await listStoredStockBacktests({} as never, {
symbols: 'AAPL,MSFT' as never,
evalWindowDays: 10,
});
assert.equal(stored.items.length, 1);
assert.equal(stored.items[0]?.symbol, 'AAPL');
assert.equal(stored.items[0]?.latestSignal, response.latestSignal);
});
});
describe('MarketServiceClient backtestStock', () => {
it('serializes the backtest-stock query parameters using generated names', async () => {
let requestedUrl = '';
globalThis.fetch = (async (input: RequestInfo | URL) => {
requestedUrl = typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url;
return new Response(JSON.stringify({ available: false, evaluations: [] }), { status: 200 });
}) as typeof fetch;
const client = new MarketServiceClient('');
await client.backtestStock({ symbol: 'MSFT', name: 'Microsoft', evalWindowDays: 7 });
assert.match(requestedUrl, /\/api\/market\/v1\/backtest-stock\?/);
assert.match(requestedUrl, /symbol=MSFT/);
assert.match(requestedUrl, /name=Microsoft/);
assert.match(requestedUrl, /eval_window_days=7/);
});
});
describe('MarketServiceClient listStoredStockBacktests', () => {
it('serializes the stored backtest batch query parameters using generated names', async () => {
let requestedUrl = '';
globalThis.fetch = (async (input: RequestInfo | URL) => {
requestedUrl = typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url;
return new Response(JSON.stringify({ items: [] }), { status: 200 });
}) as typeof fetch;
const client = new MarketServiceClient('');
await client.listStoredStockBacktests({ symbols: ['MSFT', 'NVDA'], evalWindowDays: 7 });
assert.match(requestedUrl, /\/api\/market\/v1\/list-stored-stock-backtests\?/);
assert.match(requestedUrl, /symbols=MSFT&symbols=NVDA/);
assert.match(requestedUrl, /eval_window_days=7/);
});
});