diff --git a/deno.json b/deno.json index 2303ea3..c418592 100644 --- a/deno.json +++ b/deno.json @@ -1,7 +1,7 @@ { "tasks": { - "fs": "deno run -A https://deno.land/std@0.166.0/http/file_server.ts", - "test": "deno test *_test.js --watch" + "fs": "deno run -A --no-lock https://deno.land/std@0.166.0/http/file_server.ts", + "test": "deno test 'store_test.js' --no-lock" } } \ No newline at end of file diff --git a/deno.lock b/deno.lock deleted file mode 100644 index 0ecd71f..0000000 --- a/deno.lock +++ /dev/null @@ -1,9 +0,0 @@ -{ - "version": "2", - "remote": { - "https://deno.land/std@0.166.0/fmt/colors.ts": "9e36a716611dcd2e4865adea9c4bec916b5c60caad4cdcdc630d4974e6bb8bd4", - "https://deno.land/std@0.166.0/testing/_diff.ts": "a23e7fc2b4d8daa3e158fa06856bedf5334ce2a2831e8bf9e509717f455adb2c", - "https://deno.land/std@0.166.0/testing/_format.ts": "cd11136e1797791045e639e9f0f4640d5b4166148796cad37e6ef75f7d7f3832", - "https://deno.land/std@0.166.0/testing/asserts.ts": "1e340c589853e82e0807629ba31a43c84ebdcdeca910c4a9705715dfdb0f5ce8" - } -} diff --git a/store.js b/store.js index e9b45db..172a45d 100644 --- a/store.js +++ b/store.js @@ -23,20 +23,45 @@ export const ColumnLookup =(inFrequency)=> if(map[0] == inFrequency){ return map; } } return false; +}; + +/** @typedef {{Min:number, Max:number}} Limit */ +/** @type {Record} */ +export const ToneLimit = +{ + Freq: { Min: 0, Max: ColumnMapping.length-1 }, + Stim: { Min: -10, Max: 120 }, + Chan: { Min: 0, Max: 1}, +}; +/** @type {(inValue:number, inLimit:Limit)=>number} */ +export const ApplyLimit =(inValue, inLimit)=> +{ + if(inValue < inLimit.Min){ return inLimit.Min; } + else if(inValue > inLimit.Max) { return inLimit.Max; } + else{ return inValue; } } +/** @typedef {(freq:TestFrequency, chan:number)=>TestFrequencySample|undefined} MarkLookup */ +/** @type {Record} */ +export const ChanMark = +{ + User: (freq, chan)=> chan == 0 ? freq.UserL : freq.UserR, + Test: (freq, chan)=> chan == 0 ? freq.TestL : freq.TestR +}; + + /** @typedef {{Stim:number, Resp:boolean}} TestFrequencySample */ /** @typedef {{Hz:number, TestL:TestFrequencySample, TestR:TestFrequencySample, UserL?:TestFrequencySample, UserR?:TestFrequencySample}} TestFrequency */ /** @typedef {{Name:string, Plot:Array}} Test */ /** @typedef {{Test?:Test, Freq?:TestFrequency, Mark?:TestFrequencySample}} Context */ -/** @typedef {{Chan:"left"|"right", Freq:number, Stim:number, Selection:Context, Tests:Array}} State */ +/** @typedef {{Chan:number, Freq:number, Stim:number, Live:Context, Tests:Array}} State */ /** @type {State} */ export const Initial = { - Chan: "left", + Chan: 0, Freq: 3, Stim: 30, - Selection: + Live: { Test: undefined, Freq: undefined, @@ -54,26 +79,30 @@ export const Initial = ] }; -/** @typedef {{Name:"Mark", Data:boolean|undefined}} ActionMark */ -/** @typedef {{Name:"Test", Data:number}} ActionTest*/ -/** @typedef {ActionMark|ActionTest} Action */ +/** @typedef {{Name:"Mark", Data:boolean|null}} ActionMark */ +/** @typedef {{Name:"Test", Data:number}} ActionTest */ +/** @typedef {{Name:"Chan", Data:number}} ActionChan */ +/** @typedef {{Name:"Freq", Data:number}} ActionFreq */ +/** @typedef {{Name:"Stim", Data:number}} ActionStim */ +/** @typedef {ActionMark|ActionTest|ActionChan|ActionFreq|ActionStim} Action */ /** @typedef {(inState:State, inAction:Action)=>State} Reducer */ /** @typedef {(inState:State)=>boolean} SelectionUpdater */ /** @type {Record} */ -const Update = { +const Update = +{ Freq(inState) { const column = ColumnMapping[inState.Freq]; - if(column && inState.Selection.Test) + if(column && inState.Live.Test) { const hz = column[0]; - inState.Selection.Freq = undefined; - for(let i=0; i { - const state = Initial; - await t.step("Selections are empty", ()=> { - assertEquals(state.Selection.Test, undefined); - assertEquals(state.Selection.Freq, undefined); - assertEquals(state.Selection.Mark, undefined); + assertEquals(States.latest.Live.Test, undefined); + assertEquals(States.latest.Live.Freq, undefined); + assertEquals(States.latest.Live.Mark, undefined); }); await t.step("Frequency index maps to 1k hz", ()=> { - assertEquals(state.Freq, 3); - assertEquals(ColumnMapping[state.Freq][0], 1000); + assertEquals(States.latest.Freq, 3); + assertEquals(ColumnMapping[States.latest.Freq][0], 1000); }); - await t.step("The first test has its 2nd plot at 1k hz, and no user marks", ()=> + await t.step("The first test has its 2nd plot at 1k hz", ()=> { - const plot = state.Tests[0].Plot[1]; + const plot = States.latest.Tests[0].Plot[1]; assertEquals(plot.Hz, 1000); - assertEquals(plot.UserL, undefined); - assertEquals(plot.UserR, undefined); }); }) -Deno.test("Select Test", async (t)=> +Deno.test("Select First Test", async (t)=> { - let state; await t.step("dispatch action", ()=> { - state = Reducer(Initial, {Name:"Test", Data:0}); + States.latest = Reducer(States.latest, {Name:"Test", Data:0}); }); await t.step("check selections: test and freq, but no mark", ()=> { - assertEquals(state.Selection.Test, state.Tests[0]); - assertEquals(state.Selection.Freq, state.Tests[0].Plot[1]); - assertEquals(state.Selection.Mark, undefined); + const s = States.latest; + assertEquals(s.Live.Test, s.Tests[0]); + assertEquals(s.Live.Freq, s.Tests[0].Plot[1]); + assertEquals(s.Live.Mark, undefined); + }); +}); + +Deno.test("Make Marks", async (t)=> +{ + let s; + await t.step("Left channel selected", ()=> + { + assertEquals(States.latest.Chan, 0); + }); + await t.step("Dispatch Mark action", ()=> + { + States.latest = Reducer(States.latest, {Name:"Mark", Data:true}); + }); + s = States.latest; + + await t.step("Check selections: test, freq, and mark", ()=> + { + assertEquals(s.Live.Test, s.Tests[0]); + assertEquals(s.Live.Freq, s.Tests[0].Plot[1]); + assertEquals(s.Live.Mark, s.Tests[0].Plot[1].UserL); }); + await t.step("Check marked value", ()=> + { + assertEquals(s.Live.Mark.Stim, s.Stim); + assertEquals(s.Live.Mark.Resp, true); + }); - -}) + await t.step("Dispatch Mark delete action", ()=> + { + States.latest = Reducer(States.latest, {Name:"Mark", Data:null}); + }); + s = States.latest; + + await t.step("Check marked value", ()=> + { + assertEquals(States.latest.Live.Mark, undefined); + }); + +}); + + +Deno.test("Update Tone State", async(t)=> +{ + await t.step("all three", ()=> + { + States.latest = Reducer(States.latest, {Name:"Freq", Data:2}); + States.latest = Reducer(States.latest, {Name:"Stim", Data:25}); + States.latest = Reducer(States.latest, {Name:"Chan", Data:1}); + }); + + await t.step("check tone values", ()=> + { + + assertEquals(States.latest.Stim, 25); + assertEquals(States.latest.Freq, 2); + assertEquals(States.latest.Chan, 1); + }); +});