Some checks failed
Integration Tests / test-service-integration (push) Successful in 2m16s
Package Tests / Python package - aitbc-agent-sdk (push) Successful in 25s
Package Tests / Python package - aitbc-core (push) Successful in 22s
Package Tests / Python package - aitbc-crypto (push) Successful in 14s
Package Tests / Python package - aitbc-sdk (push) Successful in 18s
Package Tests / JavaScript package - aitbc-sdk-js (push) Successful in 6s
Package Tests / JavaScript package - aitbc-token (push) Successful in 21s
Production Tests / Production Integration Tests (push) Failing after 10s
Python Tests / test-python (push) Failing after 2m41s
Security Scanning / security-scan (push) Failing after 46s
Smart Contract Tests / test-solidity (map[name:aitbc-token path:packages/solidity/aitbc-token]) (push) Successful in 16s
Smart Contract Tests / lint-solidity (push) Successful in 12s
- Simplified npm install commands in CI workflows by removing fallback logic - Added aitbc-crypto local dependency installation for aitbc-sdk in package-tests.yml - Removed aitbc-token specific Hardhat dependency workarounds from package-tests.yml - Fixed bare except clause in agent_daemon.py to catch specific json.JSONDecodeError - Moved aitbc-crypto from poetry.dependencies to standard dependencies in aitbc-sdk - Fixed MyPy type errors in receip
220 lines
6.6 KiB
TypeScript
220 lines
6.6 KiB
TypeScript
import { expect } from "chai";
|
|
import type { HardhatEthers } from "@nomicfoundation/hardhat-ethers/types";
|
|
import type { NetworkHelpers } from "@nomicfoundation/hardhat-network-helpers/types";
|
|
import type { BaseContract, ContractTransactionResponse } from "ethers";
|
|
import { network } from "hardhat";
|
|
import type { NetworkConnection } from "hardhat/types/network";
|
|
import { AITokenRegistry__factory } from "../typechain-types";
|
|
|
|
type HardhatConnection = NetworkConnection & {
|
|
ethers: HardhatEthers;
|
|
networkHelpers: NetworkHelpers;
|
|
};
|
|
|
|
const { ethers, networkHelpers } =
|
|
(await network.connect()) as HardhatConnection;
|
|
|
|
async function expectRevert(
|
|
operation: Promise<unknown>,
|
|
expectedMessage?: string,
|
|
) {
|
|
let caughtError: unknown;
|
|
|
|
try {
|
|
await operation;
|
|
} catch (error) {
|
|
caughtError = error;
|
|
}
|
|
|
|
expect(caughtError).to.not.equal(undefined);
|
|
|
|
if (expectedMessage !== undefined) {
|
|
const message =
|
|
caughtError instanceof Error ? caughtError.message : String(caughtError);
|
|
expect(message).to.contain(expectedMessage);
|
|
}
|
|
}
|
|
|
|
async function expectEvent(
|
|
contract: BaseContract,
|
|
operation: Promise<ContractTransactionResponse>,
|
|
eventName: string,
|
|
expectedArgs: readonly unknown[],
|
|
) {
|
|
const tx = await operation;
|
|
const receipt = await tx.wait();
|
|
|
|
expect(receipt).to.not.equal(null);
|
|
|
|
const parsedLog = receipt!.logs
|
|
.map((log) => {
|
|
try {
|
|
return contract.interface.parseLog(log);
|
|
} catch {
|
|
return null;
|
|
}
|
|
})
|
|
.find((entry) => entry?.name === eventName);
|
|
|
|
expect(parsedLog).to.not.equal(undefined);
|
|
expect(parsedLog).to.not.equal(null);
|
|
expect(Array.from(parsedLog!.args)).to.deep.equal([...expectedArgs]);
|
|
}
|
|
|
|
async function deployRegistryFixture() {
|
|
const [admin, coordinator, provider1, provider2, outsider] =
|
|
await ethers.getSigners();
|
|
|
|
const factory = new AITokenRegistry__factory(admin);
|
|
const registry = await factory.deploy(admin.address);
|
|
await registry.waitForDeployment();
|
|
|
|
const coordinatorRole = await registry.COORDINATOR_ROLE();
|
|
await registry.grantRole(coordinatorRole, coordinator.address);
|
|
|
|
return { registry, admin, coordinator, provider1, provider2, outsider };
|
|
}
|
|
|
|
describe("AITokenRegistry", function () {
|
|
describe("Provider Registration", function () {
|
|
it("allows coordinator to register a provider", async function () {
|
|
const { registry, coordinator, provider1 } =
|
|
await networkHelpers.loadFixture(deployRegistryFixture);
|
|
|
|
const collateral = ethers.parseEther("100");
|
|
|
|
await expectEvent(
|
|
registry,
|
|
registry
|
|
.connect(coordinator)
|
|
.registerProvider(provider1.address, collateral),
|
|
"ProviderRegistered",
|
|
[provider1.address, collateral],
|
|
);
|
|
|
|
const info = await registry.providerInfo(provider1.address);
|
|
expect(info.active).to.equal(true);
|
|
expect(info.collateral).to.equal(collateral);
|
|
});
|
|
|
|
it("rejects registration of zero address", async function () {
|
|
const { registry, coordinator } = await networkHelpers.loadFixture(
|
|
deployRegistryFixture,
|
|
);
|
|
|
|
await expectRevert(
|
|
registry.connect(coordinator).registerProvider(ethers.ZeroAddress, 0),
|
|
"invalid provider",
|
|
);
|
|
});
|
|
|
|
it("rejects duplicate registration", async function () {
|
|
const { registry, coordinator, provider1 } =
|
|
await networkHelpers.loadFixture(deployRegistryFixture);
|
|
|
|
await registry
|
|
.connect(coordinator)
|
|
.registerProvider(provider1.address, 100);
|
|
|
|
await expectRevert(
|
|
registry.connect(coordinator).registerProvider(provider1.address, 200),
|
|
"already registered",
|
|
);
|
|
});
|
|
|
|
it("rejects registration from non-coordinator", async function () {
|
|
const { registry, provider1, outsider } =
|
|
await networkHelpers.loadFixture(deployRegistryFixture);
|
|
|
|
await expectRevert(
|
|
registry.connect(outsider).registerProvider(provider1.address, 100),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("Provider Updates", function () {
|
|
it("allows coordinator to update provider status", async function () {
|
|
const { registry, coordinator, provider1 } =
|
|
await networkHelpers.loadFixture(deployRegistryFixture);
|
|
|
|
await registry
|
|
.connect(coordinator)
|
|
.registerProvider(provider1.address, 100);
|
|
|
|
await expectEvent(
|
|
registry,
|
|
registry
|
|
.connect(coordinator)
|
|
.updateProvider(provider1.address, false, 50),
|
|
"ProviderUpdated",
|
|
[provider1.address, false, 50n],
|
|
);
|
|
|
|
const info = await registry.providerInfo(provider1.address);
|
|
expect(info.active).to.equal(false);
|
|
expect(info.collateral).to.equal(50);
|
|
});
|
|
|
|
it("allows reactivating a deactivated provider", async function () {
|
|
const { registry, coordinator, provider1 } =
|
|
await networkHelpers.loadFixture(deployRegistryFixture);
|
|
|
|
await registry
|
|
.connect(coordinator)
|
|
.registerProvider(provider1.address, 100);
|
|
await registry
|
|
.connect(coordinator)
|
|
.updateProvider(provider1.address, false, 100);
|
|
await registry
|
|
.connect(coordinator)
|
|
.updateProvider(provider1.address, true, 200);
|
|
|
|
const info = await registry.providerInfo(provider1.address);
|
|
expect(info.active).to.equal(true);
|
|
expect(info.collateral).to.equal(200);
|
|
});
|
|
|
|
it("rejects update of unregistered provider", async function () {
|
|
const { registry, coordinator, provider1 } =
|
|
await networkHelpers.loadFixture(deployRegistryFixture);
|
|
|
|
await expectRevert(
|
|
registry
|
|
.connect(coordinator)
|
|
.updateProvider(provider1.address, false, 100),
|
|
"provider not registered",
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("Access Control", function () {
|
|
it("admin can grant coordinator role", async function () {
|
|
const { registry, admin, outsider } = await networkHelpers.loadFixture(
|
|
deployRegistryFixture,
|
|
);
|
|
|
|
const coordinatorRole = await registry.COORDINATOR_ROLE();
|
|
await registry
|
|
.connect(admin)
|
|
.grantRole(coordinatorRole, outsider.address);
|
|
|
|
expect(
|
|
await registry.hasRole(coordinatorRole, outsider.address),
|
|
).to.equal(true);
|
|
});
|
|
|
|
it("non-admin cannot grant roles", async function () {
|
|
const { registry, coordinator, outsider } =
|
|
await networkHelpers.loadFixture(deployRegistryFixture);
|
|
|
|
const coordinatorRole = await registry.COORDINATOR_ROLE();
|
|
|
|
await expectRevert(
|
|
registry
|
|
.connect(coordinator)
|
|
.grantRole(coordinatorRole, outsider.address),
|
|
);
|
|
});
|
|
});
|
|
});
|