Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a mock service for versioned tests #1846

Closed
bizob2828 opened this issue Nov 6, 2023 · 4 comments · Fixed by #1858
Closed

Create a mock service for versioned tests #1846

bizob2828 opened this issue Nov 6, 2023 · 4 comments · Fixed by #1858
Assignees

Comments

@bizob2828
Copy link
Member

bizob2828 commented Nov 6, 2023

OpenAI is a http service and we will be building a mock http server that can handle a variety of calls to test out our instrumentation. We plan on using the Python agent mock OpenAI server for inspiration. We will use nock to intercept the http calls and respond with a variety of responses.

Note: There is a FR to replace node-fetch with undici in openai which would make our mock service useless as nock does not work. Design this in a way that we can easily swap out what is intercepting http calls

@workato-integration
Copy link

@jsumners-nr
Copy link
Contributor

jsumners-nr commented Nov 8, 2023

In regard to designing this in a way that the tests can be replaced later, I see that the openai module supports an options object that allows defining the base URL, e.g. const options = { baseURL: 'http://example.com/v1' }. Given that, what are your thoughts on writing tests similar to:

function getServer(method, handler) {
  const server = fastify({ host: '127.0.0.1', port: 0 })
  server.route({
    method,
    handler
  })
  server.listen()

  return { server, addr: server.addresses()[0] }
}

tap.test('something', async t => {
  const { server, addr } = getServer('get', async (req, res) => {
    return { some: 'payload' }
  })
  t.teardown(() => server.close())
  
  client = openai({ baseURL: `http://${addr.address}:${addr.port}/v1` })
  res = client.sendRequest()
  t.equal(res, `{"some":"payload"}`)
})

@bizob2828
Copy link
Member Author

Yea should be ok. I just was hoping for something more declarative and less boiler plate so we can a sample of request/responses from a file. It's hard to tell from your code snippet suggestion

@jsumners-nr
Copy link
Contributor

I have re-reviewed the Python agent's approach. They are basically doing what I am suggesting, except they are pulling from a set of pre-defined responses based upon the input received in the HTTP handler. We can certainly do the same thing. The benefit to they way the Python team is doing it, and my suggestion, is that we don't have to intercept any network requests; we will hit a "real" HTTP that does what we are expecting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants