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

// src/__tests__/proxy.test.ts
var import_supertest = __toESM(require_supertest());

// proxy.ts
var TestProxy = class extends Proxy {
  constructor(cfg) {
    super(cfg);
    this.config.proxy.inject = "init";
    this.config.proxy.before = ["fn"];
  }
  proxy() {
    return async (ctx, next) => {
      console.dir("TestProxy process child" + ctx.path);
      await next();
    };
  }
};
var TestProxy2 = class extends Proxy {
  constructor(cfg) {
    super(cfg);
    this.config.proxy.inject = "before";
    this.config.proxy.before = ["fn"];
  }
  proxy() {
    return async (ctx, next) => {
      console.dir("TestProxy2 process child" + ctx.path);
      await next();
    };
  }
};

// src/__tests__/proxy.test.ts
describe("proxy", async () => {
  it("should enable proxy1", async () => {
    const rpc = createServer({
      fn: {
        // prefix: '/apk2',
      }
    });
    rpc.fn("/a", function(a) {
      return { a };
    });
    const tp = new TestProxy({});
    rpc.plugin(tp);
    const request = (0, import_supertest.default)(rpc.callback());
    const spy = vi.spyOn(console, "dir");
    const res = await request.get('/api/a?$p=["hello"]');
    globalExpect(res.type).toEqual("application/json");
    globalExpect(res.status).toEqual(200);
    globalExpect(res.body).toEqual({ a: "hello" });
    globalExpect(spy).toHaveBeenCalled();
  });
  it("should enable proxy2", async () => {
    const rpc = createServer({
      fn: {
        // prefix: '/apk2',
      }
    });
    rpc.fn("/a", function(a) {
      return { a };
    });
    const tp = new TestProxy2({});
    rpc.plugin(tp);
    const request = (0, import_supertest.default)(rpc.callback());
    const spy = vi.spyOn(console, "dir");
    const res = await request.get('/api/a?$p=["hello"]');
    globalExpect(res.type).toEqual("application/json");
    globalExpect(res.status).toEqual(200);
    globalExpect(res.body).toEqual({ a: "hello" });
    globalExpect(spy).toHaveBeenCalled();
  });
  it("should enable proxy2", async () => {
    const tp = new Proxy({});
    tp.app.use(tp.proxy());
    tp.use(async (ctx) => {
      ctx.body = { a: "a" };
    });
    const request = (0, import_supertest.default)(tp.app.callback());
    const res = await request.get("/");
    globalExpect(res.type).toEqual("application/json");
    globalExpect(res.status).toEqual(200);
    globalExpect(res.body).toEqual({ a: "a" });
  });
  it("should enable proxy2", async () => {
    const tp = new Proxy({});
    tp.addInit(async (ctx) => {
      ctx.body = { a: "a" };
    });
    tp.app.use(combine(tp.init));
    const request = (0, import_supertest.default)(tp.app.callback());
    const res = await request.get("/");
    globalExpect(res.type).toEqual("application/json");
    globalExpect(res.status).toEqual(200);
    globalExpect(res.body).toEqual({ a: "a" });
  });
  it("should enable proxy2", async () => {
    const tp = new Proxy({});
    tp.app.use(tp.proxy());
    tp.addLoad(async (ctx) => {
      ctx.body = { a: "a" };
    });
    tp.app.use(combine(tp.load));
    const request = (0, import_supertest.default)(tp.app.callback());
    const res = await request.get("/");
    globalExpect(res.type).toEqual("application/json");
    globalExpect(res.status).toEqual(200);
    globalExpect(res.body).toEqual({ a: "a" });
  });
});
