/**
 * Your flows can be tested locally prior to publishing your
 * code-native integration. In this file, we invoke our flow
 * with different selections and describe the results we expect to see.
 *
 * You can run this test with "npm run test"
 *
 * For more information on unit testing code-native integrations, see
 * https://prismatic.io/docs/integrations/code-native/testing/
 */

import { invokeFlow } from "@prismatic-io/spectral/testing";
import dotenv from "dotenv";
import { getPost } from "./flows";

dotenv.config({ path: ".env.testing" });

const acmeConnection = {
  key: "Acme Connection",
  fields: {
    baseUrl: process.env.ACME_ENDPOINT,
    apiKey: process.env.ACME_API_KEY,
  },
};

describe("Verify flow works as expected", () => {
  test.each(["2", "3"])("Fetch the selected post %s", async (postId) => {
    const { result } = await invokeFlow(getPost, {
      configVars: { "Acme Connection": acmeConnection, "Select Post": postId },
    });
    expect(result?.data).toMatchObject({
      id: Number(postId),
      title: expect.any(String),
      body: expect.any(String),
    });
  });

  test("Verify we see the expected log lines", async () => {
    const { loggerMock } = await invokeFlow(getPost, {
      configVars: { "Acme Connection": acmeConnection, "Select Post": "2" },
    });
    expect(loggerMock.info).toHaveBeenCalledWith(
      'During configuration, the customer selected the post "2"',
    );
    expect(loggerMock.info).toHaveBeenCalledWith(
      expect.stringContaining('with ID "2" belongs to user "1"'),
    );
  });
});
