Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fulcanelly committed Jan 11, 2024
1 parent ae20913 commit 5c0e98d
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 99 deletions.
8 changes: 2 additions & 6 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@ function App() {


useEffect(() => {
(window as any).state = state
document.addEventListener('keydown', function(event) {
if (event.key === 'r') {
// console.log('a')

dispatch({type:'scale_change', deltaY: -1})
dispatch({type:'scale_change', deltaY: 1})
}
});


const interval = setInterval(() => sendCellsUpdate(dispatch), 200)
const interval = setInterval(() => sendCellsUpdate(dispatch), 2100)

return () => clearInterval(interval)
}, [])
Expand Down
98 changes: 5 additions & 93 deletions src/model.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { actualStatePoweredLens, datasheets, floorMod, getNeighbours, notDatasheet, rotateReverseTimes, rotateTimes, visualToPins } from "./engine"
import { Cell, findByPosition, findWires, getOppositeIndex, NotCell, PinCell, PowerCell, updateCellsNToActuall, WireCell } from "./model"

import { datasheets, floorMod, notDatasheet, rotateTimes, visualToPins } from "./engine"
import { Cell, findWires, NotCell, PinCell, PowerCell } from "./model"
import * as R from 'ramda'
import { buildPath, isMatch } from './utils'
import { buildPath } from './utils'
import { genericWire } from "./reducer"
import { notGateEntry, powerSourceEntry } from "./circuit"

Expand All @@ -19,7 +20,7 @@ function builtLens(handler: (proxy: ProxyType) => ProxyType) {
return R.lensPath(buildPath(handler))
}

const helpers = {
export const helpers = {

cells2PinCells(cells: Cell[]): PinCell[] {
return cells.map(visualToPins)
Expand Down Expand Up @@ -178,98 +179,9 @@ describe("model", () => {

let [wires, _] = findWires(helpers.cells2PinCells([first, second]))

console.log(wires[0].inputs)
expect(wires[0].inputs[0].pinIndex).toBe(0)
})


it("power source should power a wire", () => {
let first: Cell = R.mergeDeepLeft({
position: { x: 0, y: 0 },
state: { rotation: 1 }
}, genericWire)

let second: Cell = R.mergeDeepLeft({
position: { x: 1, y: 0 },
cellType: 'power'
}, genericWire)


let cells = updateCellsNToActuall(
helpers.cells2PinCells([first, second]))

let updatedWire = cells.find(cell => cell.cellType == 'wire') as WireCell

expect(updatedWire.state.powered).toBe(true)
})

/**
* =>
* x O > x x x => x O > X X X
* =>
*/
it("NOT should power wire", () => {
let first: Cell = {
cellType: 'not',
position: { x: 1, y: 0 },
state: {
rotation: 1, //TODO WHY?
powered: true
}
}

let second: Cell = {
cellType: 'wire',
position: { x: 0, y: 0 },
state: {
rotation: 1,
wireType: 0,
powered: false
}
}

let cells = updateCellsNToActuall(
helpers.cells2PinCells([first, second]))

let updatedWire = cells.find(cell => cell.cellType == 'wire') as WireCell

expect(updatedWire.state.powered).toBe(true)
})

/**
* =>
* x O > x O > => x O > X o >
* =>
*/
it("NOT should power another NOT", () => {
let first: any = {
id: 1,
cellType: 'not',
position: { x: 0, y: 0 },
state: {
rotation: 1,
powered: true
}
}
let second: any = {
id: 2,
cellType: 'not',
position: { x: 1, y: 0 },
state: {
rotation: 1, //not
powered: true
}
}

let cells = updateCellsNToActuall(
helpers.cells2PinCells([first, second]))

let updatedNot = cells.find(cell => (cell as any).id == 1) as NotCell

expect(updatedNot.state.powered).toBe(false)

})

/**
*
* O O O x => O O O X <- test this pin
Expand Down
164 changes: 164 additions & 0 deletions src/update.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import { visualToPins } from "./engine"
import { Cell, NotCell, State, WireCell, findWires, updateWiresAndGatesInState } from "./model"
import { genericWire, initState } from "./reducer"
import * as R from 'ramda'
import { buildLens } from "./utils"

describe('wire updates', () => {

describe('when precompile, 2 ticks', () => {
let precompile = (state: State) => {
const pinsCells = state.cells.map(visualToPins)
let [wires, rest] = findWires(pinsCells)

return R.pipe(
R.set(buildLens<State>().compiled.gates!._(), rest),
R.set(buildLens<State>().compiled.wires!._(), wires)
)(state)

}
let subject = updateWiresAndGatesInState

it("NOT chain should update on every tick", () => {
const items = new Array(8)
.fill(0)
.map((_, i) => ({
cellType: 'not',
position: { x: i, y: 0 },
state: {
rotation: 1, //not
powered: true
}
}) as Cell)

const state = initState()
state.cells = items

const precompiled = precompile(state)

const tick1 = subject(precompiled)

const tick2 = subject(tick1)
const tick3 = subject(tick2)
const tick4 = subject(tick3)
const tick5 = subject(tick4)

const lens = R.view(buildLens<Cell>().state.powered!._())

expect(precompiled.cells.map(lens)).toEqual(
[true, true, true, true, true, true, true, true]);
expect(tick1.cells.map(lens)).toEqual(
[false, false, false, false, false, false, false, true]);
expect(tick2.cells.map(lens)).toEqual(
[true, true, true, true, true, true, false, true]);
expect(tick3.cells.map(lens)).toEqual(
[false, false, false, false, false, true, false, true]);
expect(tick4.cells.map(lens)).toEqual(
[true, true, true, true, false, true, false, true]);
expect(tick5.cells.map(lens)).toEqual(
[false, false, false, true, false, true, false, true]);
})
})

describe('when first run', () => {

let subject: (c: Cell[]) => State = (cells: Cell[]) => {
const pinsCells = cells.map(visualToPins)
let [wires, rest] = findWires(pinsCells)

const result = R.pipe(
R.set(buildLens<State>().compiled.gates!._(), rest),
R.set(buildLens<State>().compiled.wires!._(), wires)
)(initState())

return updateWiresAndGatesInState({
...result, cells
})
}

it("power source should power a wire", () => {
let first: Cell = R.mergeDeepLeft({
position: { x: 0, y: 0 },
state: { rotation: 1 }
}, genericWire)

let second: Cell = R.mergeDeepLeft({
position: { x: 1, y: 0 },
cellType: 'power'
}, genericWire)


let cells = subject([first, second]).cells

let updatedWire = cells.find(cell => cell.cellType === 'wire') as WireCell

expect(updatedWire.state.powered).toBe(true)
})
/**
* =>
* x O > x x x => x O > X X X
* =>
*/
it("NOT should power wire", () => {
let first: Cell = {
cellType: 'not',
position: { x: 1, y: 0 },
state: {
rotation: 1, //TODO WHY?
powered: true
}
}

let second: Cell = {
cellType: 'wire',
position: { x: 0, y: 0 },
state: {
rotation: 1,
wireType: 0,
powered: false
}
}

let cells = subject([first, second]).cells

let updatedWire = cells.find(cell => cell.cellType === 'wire') as WireCell

expect(updatedWire.state.powered).toBe(true)
})

/**
* =>
* x O > x O > => x O > X o >
* =>
*/
it("NOT should power another NOT", () => {
let first: any = {
id: 1,
cellType: 'not',
position: { x: 0, y: 0 },
state: {
rotation: 1,
powered: true
}
}
let second: any = {
id: 2,
cellType: 'not',
position: { x: 1, y: 0 },
state: {
rotation: 1, //not
powered: true
}
}

let cells = subject([first, second]).cells

let updatedNot = cells.find(cell => (cell as any).id === 1) as NotCell

expect(updatedNot.state.powered).toBe(false)

})
})


})

0 comments on commit 5c0e98d

Please sign in to comment.