|
| 1 | +import update from "immutability-helper"; |
| 2 | +import _ from "lodash"; |
| 3 | +import { sampleTracingLayer } from "test/fixtures/dataset_server_object"; |
| 4 | +import { initialState as defaultVolumeState } from "test/fixtures/volumetracing_object"; |
| 5 | +import { chainReduce } from "test/helpers/chainReducer"; |
| 6 | +import type { Action } from "viewer/model/actions/actions"; |
| 7 | +import { |
| 8 | + addUserBoundingBoxAction, |
| 9 | + changeUserBoundingBoxAction, |
| 10 | + deleteUserBoundingBoxAction, |
| 11 | +} from "viewer/model/actions/annotation_actions"; |
| 12 | +import * as VolumeTracingActions from "viewer/model/actions/volumetracing_actions"; |
| 13 | +import { setActiveUserBoundingBoxId } from "viewer/model/actions/ui_actions"; |
| 14 | +import compactUpdateActions from "viewer/model/helpers/compaction/compact_update_actions"; |
| 15 | +import { diffVolumeTracing } from "viewer/model/sagas/volumetracing_saga"; |
| 16 | +import type { |
| 17 | + ApplicableVolumeUpdateAction, |
| 18 | + UpdateActionWithoutIsolationRequirement, |
| 19 | +} from "viewer/model/sagas/update_actions"; |
| 20 | +import { combinedReducer, type WebknossosState } from "viewer/store"; |
| 21 | +import { makeBasicGroupObject } from "viewer/view/right-border-tabs/trees_tab/tree_hierarchy_view_helpers"; |
| 22 | +import { afterAll, describe, expect, test } from "vitest"; |
| 23 | + |
| 24 | +const enforceVolumeTracing = (state: WebknossosState) => { |
| 25 | + const tracing = state.annotation.volumes[0]; |
| 26 | + if (tracing == null || state.annotation.volumes.length !== 1) { |
| 27 | + throw new Error("No volume tracing found"); |
| 28 | + } |
| 29 | + return tracing; |
| 30 | +}; |
| 31 | + |
| 32 | +const initialState: WebknossosState = update(defaultVolumeState, { |
| 33 | + annotation: { |
| 34 | + restrictions: { |
| 35 | + allowUpdate: { |
| 36 | + $set: true, |
| 37 | + }, |
| 38 | + branchPointsAllowed: { |
| 39 | + $set: true, |
| 40 | + }, |
| 41 | + }, |
| 42 | + annotationType: { $set: "Explorational" }, |
| 43 | + }, |
| 44 | + dataset: { |
| 45 | + dataSource: { |
| 46 | + dataLayers: { |
| 47 | + $set: [sampleTracingLayer], |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | +}); |
| 52 | + |
| 53 | +const { tracingId } = initialState.annotation.volumes[0]; |
| 54 | + |
| 55 | +const applyActions = chainReduce(combinedReducer); |
| 56 | + |
| 57 | +// This helper dict exists so that we can ensure via typescript that |
| 58 | +// the list contains all members of ApplicableVolumeUpdateAction. As soon as |
| 59 | +// ApplicableVolumeUpdateAction is extended with another action, TS will complain |
| 60 | +// if the following dictionary doesn't contain that action. |
| 61 | +const actionNamesHelper: Record<ApplicableVolumeUpdateAction["name"], true> = { |
| 62 | + updateLargestSegmentId: true, |
| 63 | + updateSegment: true, |
| 64 | + createSegment: true, |
| 65 | + deleteSegment: true, |
| 66 | + updateSegmentGroups: true, |
| 67 | + addUserBoundingBoxInVolumeTracing: true, |
| 68 | + updateUserBoundingBoxInVolumeTracing: true, |
| 69 | + deleteUserBoundingBoxInVolumeTracing: true, |
| 70 | + updateSegmentGroupsExpandedState: true, |
| 71 | + updateUserBoundingBoxVisibilityInVolumeTracing: true, |
| 72 | +}; |
| 73 | +const actionNamesList = Object.keys(actionNamesHelper); |
| 74 | + |
| 75 | +describe("Update Action Application for VolumeTracing", () => { |
| 76 | + const seenActionTypes = new Set<string>(); |
| 77 | + |
| 78 | + /* |
| 79 | + * Hardcode these values if you want to focus on a specific test. |
| 80 | + */ |
| 81 | + const compactionModes = [true, false]; |
| 82 | + const hardcodedBeforeVersionIndex: number | null = null; |
| 83 | + const hardcodedAfterVersionIndex: number | null = null; |
| 84 | + |
| 85 | + const userActions: Action[] = [ |
| 86 | + VolumeTracingActions.updateSegmentAction(2, { somePosition: [1, 2, 3] }, tracingId), |
| 87 | + VolumeTracingActions.updateSegmentAction(3, { somePosition: [3, 4, 5] }, tracingId), |
| 88 | + VolumeTracingActions.updateSegmentAction( |
| 89 | + 3, |
| 90 | + { |
| 91 | + name: "name", |
| 92 | + groupId: 3, |
| 93 | + metadata: [ |
| 94 | + { |
| 95 | + key: "someKey", |
| 96 | + stringValue: "some string value", |
| 97 | + }, |
| 98 | + ], |
| 99 | + }, |
| 100 | + tracingId, |
| 101 | + ), |
| 102 | + addUserBoundingBoxAction({ |
| 103 | + boundingBox: { min: [0, 0, 0], max: [10, 10, 10] }, |
| 104 | + name: "UserBBox", |
| 105 | + color: [1, 2, 3], |
| 106 | + isVisible: true, |
| 107 | + }), |
| 108 | + changeUserBoundingBoxAction(1, { name: "Updated Name" }), |
| 109 | + deleteUserBoundingBoxAction(1), |
| 110 | + VolumeTracingActions.setSegmentGroupsAction( |
| 111 | + [makeBasicGroupObject(3, "group 3"), makeBasicGroupObject(7, "group 7")], |
| 112 | + tracingId, |
| 113 | + ), |
| 114 | + VolumeTracingActions.removeSegmentAction(3, tracingId), |
| 115 | + VolumeTracingActions.setLargestSegmentIdAction(10000), |
| 116 | + ]; |
| 117 | + |
| 118 | + test("User actions for test should not contain no-ops", () => { |
| 119 | + let state = initialState; |
| 120 | + for (const action of userActions) { |
| 121 | + const newState = combinedReducer(state, action); |
| 122 | + expect(newState !== state).toBeTruthy(); |
| 123 | + |
| 124 | + state = newState; |
| 125 | + } |
| 126 | + }); |
| 127 | + |
| 128 | + const beforeVersionIndices = |
| 129 | + hardcodedBeforeVersionIndex != null |
| 130 | + ? [hardcodedBeforeVersionIndex] |
| 131 | + : _.range(0, userActions.length); |
| 132 | + |
| 133 | + describe.each(compactionModes)( |
| 134 | + "[Compaction=%s]: should re-apply update actions from complex diff and get same state", |
| 135 | + (withCompaction) => { |
| 136 | + describe.each(beforeVersionIndices)("From v=%i", (beforeVersionIndex: number) => { |
| 137 | + const afterVersionIndices = |
| 138 | + hardcodedAfterVersionIndex != null |
| 139 | + ? [hardcodedAfterVersionIndex] |
| 140 | + : _.range(beforeVersionIndex, userActions.length + 1); |
| 141 | + |
| 142 | + test.each(afterVersionIndices)("To v=%i", (afterVersionIndex: number) => { |
| 143 | + const state2WithActiveTree = applyActions( |
| 144 | + initialState, |
| 145 | + userActions.slice(0, beforeVersionIndex), |
| 146 | + ); |
| 147 | + |
| 148 | + const state2WithoutActiveState = applyActions(state2WithActiveTree, [ |
| 149 | + VolumeTracingActions.setActiveCellAction(0), |
| 150 | + setActiveUserBoundingBoxId(null), |
| 151 | + ]); |
| 152 | + |
| 153 | + const actionsToApply = userActions.slice(beforeVersionIndex, afterVersionIndex + 1); |
| 154 | + const state3 = applyActions( |
| 155 | + state2WithActiveTree, |
| 156 | + actionsToApply.concat([ |
| 157 | + VolumeTracingActions.setActiveCellAction(0), |
| 158 | + setActiveUserBoundingBoxId(null), |
| 159 | + ]), |
| 160 | + ); |
| 161 | + expect(state2WithoutActiveState !== state3).toBeTruthy(); |
| 162 | + |
| 163 | + const volumeTracing2 = enforceVolumeTracing(state2WithoutActiveState); |
| 164 | + const volumeTracing3 = enforceVolumeTracing(state3); |
| 165 | + |
| 166 | + const updateActionsBeforeCompaction = Array.from( |
| 167 | + diffVolumeTracing(volumeTracing2, volumeTracing3), |
| 168 | + ); |
| 169 | + console.log( |
| 170 | + "updateActionsBeforeCompaction", |
| 171 | + updateActionsBeforeCompaction.map((el) => el.name), |
| 172 | + ); |
| 173 | + const maybeCompact = withCompaction |
| 174 | + ? compactUpdateActions |
| 175 | + : (updateActions: UpdateActionWithoutIsolationRequirement[]) => updateActions; |
| 176 | + const updateActions = maybeCompact( |
| 177 | + updateActionsBeforeCompaction, |
| 178 | + volumeTracing2, |
| 179 | + volumeTracing3, |
| 180 | + ) as ApplicableVolumeUpdateAction[]; |
| 181 | + |
| 182 | + for (const action of updateActions) { |
| 183 | + seenActionTypes.add(action.name); |
| 184 | + } |
| 185 | + |
| 186 | + const reappliedNewState = applyActions(state2WithoutActiveState, [ |
| 187 | + VolumeTracingActions.applyVolumeUpdateActionsFromServerAction(updateActions), |
| 188 | + VolumeTracingActions.setActiveCellAction(0), |
| 189 | + setActiveUserBoundingBoxId(null), |
| 190 | + ]); |
| 191 | + |
| 192 | + expect(reappliedNewState).toEqual(state3); |
| 193 | + }); |
| 194 | + }); |
| 195 | + }, |
| 196 | + ); |
| 197 | + |
| 198 | + afterAll(() => { |
| 199 | + expect(seenActionTypes).toEqual(new Set(actionNamesList)); |
| 200 | + }); |
| 201 | +}); |
0 commit comments