|
| 1 | +/** |
| 2 | + * This file implement test code for the public API interfaces. |
| 3 | + */ |
| 4 | +import { |
| 5 | + EventName, |
| 6 | + IBApi, |
| 7 | + isNonFatalError |
| 8 | +} from "../../.."; |
| 9 | +import configuration from "../../../common/configuration"; |
| 10 | +import logger from "../../../common/logger"; |
| 11 | + |
| 12 | +describe("IBApi Historical news Tests", () => { |
| 13 | + jest.setTimeout(10 * 1000); |
| 14 | + |
| 15 | + let ib: IBApi; |
| 16 | + const clientId = Math.floor(Math.random() * 32766) + 1; // ensure unique client |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + ib = new IBApi({ |
| 20 | + host: configuration.ib_host, |
| 21 | + port: configuration.ib_port, |
| 22 | + clientId, |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + afterEach(() => { |
| 27 | + if (ib) { |
| 28 | + ib.disconnect(); |
| 29 | + ib = undefined; |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + it("Get news providers", (done) => { |
| 34 | + ib.once(EventName.connected, () => { |
| 35 | + ib.reqNewsProviders |
| 36 | + }) |
| 37 | + .on(EventName.newsProviders, |
| 38 | + (newsProviders) => { |
| 39 | + expect(newsProviders).toBeDefined(); |
| 40 | + expect(newsProviders).toBeInstanceOf(Array); |
| 41 | + |
| 42 | + const firstProvider = newsProviders[0]; |
| 43 | + expect(firstProvider).toBeDefined(); |
| 44 | + expect(firstProvider.providerCode).toBeDefined(); |
| 45 | + expect(firstProvider.providerName).toBeDefined(); |
| 46 | + done(); |
| 47 | + }, |
| 48 | + ) |
| 49 | + ib.on(EventName.info, (msg, code) => logger.info(code, msg)) |
| 50 | + .on(EventName.error, (error, code, reqId) => { |
| 51 | + const msg = `[${reqId}] ${error.message} (Error #${code})`; |
| 52 | + isNonFatalError(code, error) ? logger.warn(msg) : done(msg); |
| 53 | + }) |
| 54 | + .connect() |
| 55 | + .reqNewsProviders(); |
| 56 | + }); |
| 57 | + |
| 58 | + it("Get PLTR news data with BRFG", (done) => { |
| 59 | + const refId = 46; |
| 60 | + const contractId = 444857009; // PLTR |
| 61 | + const providerCode = "BRFG"; |
| 62 | + |
| 63 | + let received = false; |
| 64 | + |
| 65 | + ib.once(EventName.connected, () => { |
| 66 | + ib.reqHistoricalNews( |
| 67 | + refId, |
| 68 | + contractId, |
| 69 | + providerCode, |
| 70 | + "2025-01-13 00:00:00", |
| 71 | + "2025-01-14 00:00:00", |
| 72 | + 10, |
| 73 | + null |
| 74 | + ); |
| 75 | + }) |
| 76 | + .on(EventName.historicalNews, |
| 77 | + (reqId, time, providerCode, articleId, headline) => { |
| 78 | + expect(reqId).toEqual(refId); |
| 79 | + if (reqId == refId) received = true; |
| 80 | + expect(time).toBeDefined(); |
| 81 | + expect(providerCode).toBeDefined(); |
| 82 | + expect(articleId).toBeDefined(); |
| 83 | + expect(headline).toBeDefined(); |
| 84 | + }, |
| 85 | + ) |
| 86 | + .on(EventName.historicalNewsEnd, (reqId: number) => { |
| 87 | + expect(reqId).toEqual(refId); |
| 88 | + if (received) done(); |
| 89 | + else done("Didn't get any result"); |
| 90 | + }) |
| 91 | + |
| 92 | + ib.on(EventName.info, (msg, code) => logger.info(code, msg)) |
| 93 | + .on(EventName.error, (error, code, reqId) => { |
| 94 | + const msg = `[${reqId}] ${error.message} (Error #${code})`; |
| 95 | + isNonFatalError(code, error) ? logger.warn(msg) : done(msg); |
| 96 | + }) |
| 97 | + .connect(); |
| 98 | + }); |
| 99 | + |
| 100 | +}); |
0 commit comments