import Fixture from "../helpers/fixture"
import ethUtil from "ethereumjs-util"
import ethAbi from "ethereumjs-abi"
import expectThrow from "../helpers/expectThrow"

const Manager = artifacts.require("Manager")
const Controller = artifacts.require("Controller")

contract("Controller", accounts => {
    describe("constructor", () => {
        it("should create contract", async () => {
            const controller = await Controller.new()

            assert.equal(await controller.owner.call(), accounts[0], "did not set owner correctly")
        })
    })

    let fixture
    let controller

    before(async () => {
        fixture = new Fixture(web3)
        controller = await Controller.new()
    })

    beforeEach(async () => {
        await fixture.setUp()
    })

    afterEach(async () => {
        await fixture.tearDown()
    })

    describe("setContractInfo", () => {
        it("should throw when caller is not the owner", async () => {
            const randomAddress = "0x0000000000000000000000000000000000001234"
            const commitHash = "0x1230000000000000000000000000000000000000"
            await expectThrow(controller.setContractInfo(ethUtil.bufferToHex(ethAbi.soliditySHA3(["string"], ["Manager"])), randomAddress, commitHash, {from: accounts[1]}))
        })

        it("should set contract info", async () => {
            const contractId = ethUtil.bufferToHex(ethAbi.soliditySHA3(["string"], ["Manager"]))
            const manager = await Manager.new(controller.address)
            const commitHash = "0x1230000000000000000000000000000000000000"
            await controller.setContractInfo(contractId, manager.address, commitHash)

            const cInfo = await controller.getContractInfo(contractId)
            assert.equal(cInfo[0], manager.address, "did not register contract address correctly")
            assert.equal(cInfo[1], commitHash, "did not register commit hash correctly")
        })
    })

    describe("updateController", () => {
        let contractId
        let manager

        beforeEach(async () => {
            contractId = ethUtil.bufferToHex(ethAbi.soliditySHA3(["string"], ["Manager"]))
            manager = await Manager.new(controller.address)
            const commitHash = "0x1230000000000000000000000000000000000000"
            await controller.setContractInfo(contractId, manager.address, commitHash)
        })

        it("should throw when caller is not the owner", async () => {
            const randomAddress = "0x0000000000000000000000000000000000001234"
            await expectThrow(controller.updateController(contractId, randomAddress, {from: accounts[1]}))
        })

        it("should throw for invalid key", async () => {
            const randomAddress = "0x0000000000000000000000000000000000001234"
            const invalidId = "0x123"
            await expectThrow(controller.updateController(invalidId, randomAddress))
        })

        it("should update a manager's controller", async () => {
            const randomAddress = "0x0000000000000000000000000000000000001234"
            await controller.updateController(contractId, randomAddress)

            const newController = await manager.controller.call()
            assert.equal(newController, randomAddress, "controller for manager incorrect")
        })
    })
})

@@ -7,33 +7,32 @@
  Fixture from %22.
-.
 /helpers/fixture
@@ -24,17 +24,17 @@
 helpers/
-f
+F
 ixture%22%0A
@@ -44,73 +44,46 @@
 ort 
-ethUtil from %22ethereumjs-util%22%0Aimport ethAbi from %22ethereumjs-abi
+%7BcontractId%7D from %22../../utils/helpers
 %22%0Aim
@@ -1136,61 +1136,19 @@
 nfo(
-ethUtil.bufferToHex(ethAbi.soliditySHA3(%5B%22string%22%5D, %5B
+contractId(
 %22Man
@@ -1152,18 +1152,16 @@
 Manager%22
-%5D)
 ), rando
@@ -1282,24 +1282,29 @@
        const
+ id =
  contractId 
@@ -1306,64 +1306,9 @@
 ctId
- = ethUtil.bufferToHex(ethAbi.soliditySHA3(%5B%22string%22%5D, %5B
+(
 %22Man
@@ -1304,34 +1304,32 @@
 ractId(%22Manager%22
-%5D)
 )%0A            co
@@ -1497,33 +1497,25 @@
 ontractInfo(
-contractI
+i
 d, manager.a
@@ -1594,25 +1594,17 @@
 actInfo(
-contractI
+i
 d)%0A     
@@ -1858,25 +1858,17 @@
     let 
-contractI
+i
 d%0A      
@@ -1922,24 +1922,29 @@
 %0A           
+ id =
  contractId 
@@ -1946,64 +1946,9 @@
 ctId
- = ethUtil.bufferToHex(ethAbi.soliditySHA3(%5B%22string%22%5D, %5B
+(
 %22Man
@@ -1956,10 +1956,8 @@
 ger%22
-%5D)
 )%0A  
@@ -2135,25 +2135,17 @@
 actInfo(
-contractI
+i
 d, manag
@@ -2383,33 +2383,25 @@
 eController(
-contractI
+i
 d, randomAdd
@@ -2901,25 +2901,17 @@
 troller(
-contractI
+i
 d, rando

Update Controller unit test to use unit test fixture