import {
  require_supertest
} from "../chunk-X4KDK3QI.mjs";
import {
  describe,
  globalExpect,
  it,
  vi
} from "../chunk-C6GVXLFJ.mjs";
import "../chunk-RBFZAKDG.mjs";
import {
  Fn
} from "../chunk-YONIONVZ.mjs";
import "../chunk-TRQQUNIW.mjs";
import "../chunk-XMSQJY6Q.mjs";
import "../chunk-BU4RBUBJ.mjs";
import {
  __toESM
} from "../chunk-2CV3WTQN.mjs";

// src/__tests__/fn.test.ts
var import_supertest = __toESM(require_supertest());
import Koa from "koa";
describe("app", async () => {
  it("init default", async () => {
    const fn = new Fn();
    globalExpect(fn.name).toEqual("Fn");
    globalExpect(fn.config.prefix).toEqual("/api");
  });
  it("should config prefix", async () => {
    const fn = new Fn({ prefix: "/apk" });
    globalExpect(fn.config.prefix).toEqual("/apk");
  });
  it("use fn add a function", async () => {
    const fn = new Fn({ prefix: "/apk" });
    fn.fn("/a", function(a) {
      return { a };
    });
    const count = Object.keys(fn.config["functions"]).length;
    globalExpect(1).to.equal(count);
  });
  it("use add method add a function", async () => {
    const fn = new Fn({ prefix: "/apk" });
    fn.add({
      c: (a) => {
        return a;
      },
      a: function(a, b) {
        return `${this.path} , ${a} c2 ${b}`;
      }
    });
    const count = Object.keys(fn.config["functions"]).length;
    globalExpect(2).to.equal(count);
  });
  it("use default response", async () => {
    const fn = new Fn({ prefix: "/apk" });
    const app = new Koa();
    app.use(fn.default());
    const request = (0, import_supertest.default)(app.callback());
    const res = await request.get("/");
    globalExpect(res.type).toEqual("text/plain");
    globalExpect(res.status).toEqual(200);
    globalExpect(res.text).toEqual("[fn plugin] no fn repsonse, please check your fn if exist");
  });
  it("use fn add a function", async () => {
    const fn = new Fn({ prefix: "/apk" });
    fn.fn("/a", function(a) {
      return { a };
    });
    const request = (0, import_supertest.default)(fn.app.callback());
    const res = await request.get('/a?$p=["hello"]');
    globalExpect(res.type).toEqual("application/json");
    globalExpect(res.status).toEqual(200);
    globalExpect(res.body).toEqual({ a: "hello" });
  });
  it("use fn add a lock function", async () => {
    const fn = new Fn({ prefix: "/apk" });
    fn.fn("/getAbc", function(a) {
      return { a };
    });
    fn.fn("/Abclock", function(a) {
      return { a };
    });
    const request = (0, import_supertest.default)(fn.app.callback());
    const res = await request.get('/Abclock?$p=["hello"]');
    globalExpect(res.type).toEqual("text/plain");
    globalExpect(res.status).toEqual(200);
    globalExpect(res.text).toEqual("process fn:Abclock , you need send lock request from client");
  });
  it("use fn add two function with the same path", async () => {
    const spy = vi.spyOn(console, "log");
    const fn = new Fn({ prefix: "/apk" });
    fn.fn("/a", function(a) {
      return { a };
    });
    fn.add({
      "/a": function(a, b) {
        return `${this.path} , ${a} c2 ${b}`;
      }
    });
    globalExpect(spy).toHaveBeenCalled();
  });
  it("use fn add arrow function", async () => {
    const spy = vi.spyOn(console, "log");
    const fn = new Fn({ prefix: "/apk" });
    fn.fn("/a", (a) => {
      return { a };
    });
    fn.add({
      "/a": (a, b, ctx) => {
        return `${ctx.path} , ${a} c2 ${b}`;
      }
    });
    globalExpect(spy).toHaveBeenCalled();
  });
});
