package ibd import ( "context" "net/http" "testing" "github.com/ansg191/ibd-trader/backend/internal/ibd/transport" "github.com/jarcoal/httpmock" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) const searchResponseJSON = ` { "_status": 200, "_timestamp": "1722879439.724106", "stockData": [ { "id": 13717, "symbol": "AAPL", "company": "Apple", "priceDate": "2024-08-05T09:18:00", "price": 212.33, "previousPrice": 219.86, "priceChange": -7.53, "pricePctChange": -3.42, "volume": 643433, "volumeChange": -2138, "volumePctChange": 124, "quoteUrl": "https://research.investors.com/stock-quotes/nasdaq-apple-aapl.htm" }, { "id": 79964, "symbol": "AAPU", "company": "Direxion AAPL Bull 2X", "priceDate": "2024-08-05T09:18:00", "price": 32.48, "previousPrice": 34.9, "priceChange": -2.42, "pricePctChange": -6.92, "volume": 15265, "volumeChange": -35, "volumePctChange": 212, "quoteUrl": "https://research.investors.com/stock-quotes/nasdaq-direxion-aapl-bull-2x-aapu.htm" }, { "id": 80423, "symbol": "APLY", "company": "YieldMax AAPL Option Incm", "priceDate": "2024-08-05T09:11:00", "price": 17.52, "previousPrice": 18.15, "priceChange": -0.63, "pricePctChange": -3.47, "volume": 617, "volumeChange": -2, "volumePctChange": 97, "quoteUrl": "https://research.investors.com/stock-quotes/nyse-yieldmax-aapl-option-incm-aply.htm" }, { "id": 79962, "symbol": "AAPD", "company": "Direxion Dly AAPL Br 1X", "priceDate": "2024-08-05T09:18:00", "price": 18.11, "previousPrice": 17.53, "priceChange": 0.58, "pricePctChange": 3.31, "volume": 14572, "volumeChange": -7, "volumePctChange": 885, "quoteUrl": "https://research.investors.com/stock-quotes/nasdaq-direxion-dly-aapl-br-1x-aapd.htm" }, { "id": 79968, "symbol": "AAPB", "company": "GraniteSh 2x Lg AAPL", "priceDate": "2024-08-05T09:16:00", "price": 25.22, "previousPrice": 27.25, "priceChange": -2.03, "pricePctChange": -7.45, "volume": 2505, "volumeChange": -7, "volumePctChange": 151, "quoteUrl": "https://research.investors.com/stock-quotes/nasdaq-granitesh-2x-lg-aapl-aapb.htm" } ], "news": [ { "title": "Warren Buffett Dumped Berkshire Hathaway's Favorite Stocks — Right Before They Plunged", "category": "News", "body": "Berkshire Hathaway earnings rose solidly in Q2. Warren Buffett sold nearly half his Apple stock stake. Berkshire stock fell...", "imageAlt": "", "imageUrl": "https://www.investors.com/wp-content/uploads/2024/06/Stock-WarrenBuffettwave-01-shutt-640x360.jpg", "newsUrl": "https://investors.com/news/berkshire-hathaway-earnings-q2-2024-warren-buffett-apple/", "categoryUrl": "https://investors.com/category/news/", "publishDate": "2024-08-05T15:51:57+00:00", "publishDateUnixts": 1722858717, "stocks": [ { "id": 13717, "index": 0, "symbol": "AAPL", "pricePctChange": "-3.42" } ], "videoFormat": false }, { "title": "Nvidia Plunges On Report Of AI Chip Flaw; Is It A Buy Now?", "category": "Research", "body": "Nvidia will roll out its Blackwell chip at least three months later than planned.", "imageAlt": "", "imageUrl": "https://www.investors.com/wp-content/uploads/2024/01/Stock-Nvidia-studio-01-company-640x360.jpg", "newsUrl": "https://investors.com/research/nvda-stock-is-nvidia-a-buy-2/", "categoryUrl": "https://investors.com/category/research/", "publishDate": "2024-08-05T14:59:22+00:00", "publishDateUnixts": 1722855562, "stocks": [ { "id": 38607, "index": 0, "symbol": "NVDA", "pricePctChange": "-5.18" } ], "videoFormat": false }, { "title": "Magnificent Seven Stocks Roiled: Nvidia Plunges On AI Chip Delay; Apple, Tesla Dive", "category": "Research", "body": "Nvidia stock dived Monday, while Apple and Tesla also fell sharply.", "imageAlt": "", "imageUrl": "https://www.investors.com/wp-content/uploads/2022/08/Stock-Nvidia-RTXa5500-comp-640x360.jpg", "newsUrl": "https://investors.com/research/magnificent-seven-stocks-to-buy-and-and-watch/", "categoryUrl": "https://investors.com/category/research/", "publishDate": "2024-08-05T14:51:42+00:00", "publishDateUnixts": 1722855102, "stocks": [ { "id": 13717, "index": 0, "symbol": "AAPL", "pricePctChange": "-3.42" } ], "videoFormat": false } ], "fullUrl": "https://www.investors.com/search-results/?query=AAPL" }` const emptySearchResponseJSON = ` { "_status": 200, "_timestamp": "1722879662.804395", "stockData": [], "news": [], "fullUrl": "https://www.investors.com/search-results/?query=abcdefg" }` func TestClient_Search(t *testing.T) { tests := []struct { name string response string f func(t *testing.T, client *Client) }{ { name: "found", response: searchResponseJSON, f: func(t *testing.T, client *Client) { u, err := client.Search(context.Background(), "AAPL") require.NoError(t, err) assert.Equal(t, "AAPL", u.Symbol) assert.Equal(t, "Apple", u.Name) assert.Equal(t, "https://research.investors.com/stock-quotes/nasdaq-apple-aapl.htm", u.IBDUrl) }, }, { name: "not found", response: emptySearchResponseJSON, f: func(t *testing.T, client *Client) { _, err := client.Search(context.Background(), "abcdefg") assert.Error(t, err) assert.ErrorIs(t, err, ErrSymbolNotFound) }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { tp := httpmock.NewMockTransport() tp.RegisterResponder("GET", searchUrl, httpmock.NewStringResponder(200, tt.response)) client := NewClient( db, new(kmsStub), transport.NewStandardTransport(&http.Client{Transport: tp}), ) tt.f(t, client) }) } }