const { expect } = require("chai");
const { ethers } = require("hardhat");

describe("AereBridgedToken — generic bridged stablecoin", () => {
  let bridge, alice, bob;
  let usdte, eurce, usdee;

  beforeEach(async () => {
    [bridge, alice, bob] = await ethers.getSigners();
    const F = await ethers.getContractFactory("AereBridgedToken");
    usdte = await F.deploy("Bridged Tether USD", "USDT.e", 6, bridge.address); await usdte.waitForDeployment();
    eurce = await F.deploy("Bridged EURC",        "EURC.e", 6, bridge.address); await eurce.waitForDeployment();
    usdee = await F.deploy("Bridged USDe",        "USDe.e", 18, bridge.address); await usdee.waitForDeployment();
  });

  it("preserves the per-token decimal config", async () => {
    expect(await usdte.decimals()).to.equal(6);
    expect(await eurce.decimals()).to.equal(6);
    expect(await usdee.decimals()).to.equal(18);
  });

  it("only BRIDGE can mint + burn", async () => {
    await expect(
      usdte.connect(alice).mint(alice.address, 1000n)
    ).to.be.revertedWithCustomError(usdte, "OnlyBridge");
    await usdte.connect(bridge).mint(alice.address, ethers.parseUnits("100", 6));
    expect(await usdte.balanceOf(alice.address)).to.equal(ethers.parseUnits("100", 6));

    await expect(
      usdte.connect(alice).burnFrom(alice.address, 1n)
    ).to.be.revertedWithCustomError(usdte, "OnlyBridge");
    // AUDIT FIX (HIGH #15): user must approve bridge for the burn amount.
    await usdte.connect(alice).approve(bridge.address, ethers.parseUnits("40", 6));
    await usdte.connect(bridge).burnFrom(alice.address, ethers.parseUnits("40", 6));
    expect(await usdte.balanceOf(alice.address)).to.equal(ethers.parseUnits("60", 6));
  });

  it("standard ERC-20 transfer flow", async () => {
    await usdte.connect(bridge).mint(alice.address, ethers.parseUnits("50", 6));
    await usdte.connect(alice).transfer(bob.address, ethers.parseUnits("20", 6));
    expect(await usdte.balanceOf(bob.address)).to.equal(ethers.parseUnits("20", 6));
  });

  it("rejects zero bridge in constructor", async () => {
    const F = await ethers.getContractFactory("AereBridgedToken");
    await expect(F.deploy("x", "x", 6, ethers.ZeroAddress)).to.be.revertedWith("AereBridgedToken: zero bridge");
  });

  it("name + symbol match constructor args", async () => {
    expect(await usdte.name()).to.equal("Bridged Tether USD");
    expect(await usdte.symbol()).to.equal("USDT.e");
    expect(await usdee.name()).to.equal("Bridged USDe");
    expect(await usdee.symbol()).to.equal("USDe.e");
  });
});
