From b10dbae4885e8194cd21d9bcc6c0bc6fed37e3ef Mon Sep 17 00:00:00 2001 From: JGerla Date: Sat, 20 Dec 2025 22:58:22 +0100 Subject: [PATCH 1/7] test: added tests for the ProgramStore also added documentation ref: N25B-428 --- src/pages/VisProgPage/VisProg.tsx | 5 ++ src/utils/programStore.ts | 81 ++++++++++++++++++++++++ test/setupFlowTests.ts | 5 ++ test/utils/programStore.test.ts | 100 ++++++++++++++++++++++++++++++ 4 files changed, 191 insertions(+) create mode 100644 src/utils/programStore.ts create mode 100644 test/utils/programStore.test.ts diff --git a/src/pages/VisProgPage/VisProg.tsx b/src/pages/VisProgPage/VisProg.tsx index 06e072c..1a3720b 100644 --- a/src/pages/VisProgPage/VisProg.tsx +++ b/src/pages/VisProgPage/VisProg.tsx @@ -9,6 +9,7 @@ import { import '@xyflow/react/dist/style.css'; import {useEffect} from "react"; import {useShallow} from 'zustand/react/shallow'; +import useProgramStore from "../../utils/programStore.ts"; import {DndToolbar} from './visualProgrammingUI/components/DragDropSidebar.tsx'; import useFlowStore from './visualProgrammingUI/VisProgStores.tsx'; import type {FlowState} from './visualProgrammingUI/VisProgTypes.tsx'; @@ -152,6 +153,10 @@ function runProgram() { ).then((res) => { if (!res.ok) throw new Error("Failed communicating with the backend.") console.log("Successfully sent the program to the backend."); + + // store reduced program in global program store for further use in the UI + // when the program was sent to the backend successfully: + useProgramStore.getState().setProgramState(structuredClone(program)); }).catch(() => console.log("Failed to send program to the backend.")); } diff --git a/src/utils/programStore.ts b/src/utils/programStore.ts new file mode 100644 index 0000000..e6bcc3a --- /dev/null +++ b/src/utils/programStore.ts @@ -0,0 +1,81 @@ +import {create} from "zustand"; + +// the type of a reduced program +export type ReducedProgram = { phases: Record[] }; + +/** + * the type definition of the programStore + */ +export type ProgramState = { + // Basic store functionality: + currentProgram: ReducedProgram; + setProgramState: (state: ReducedProgram) => void; + getProgramState: () => ReducedProgram; + + // Utility functions: + // to avoid having to manually go through the entire state for every instance where data is required + getPhaseIds: () => string[]; + getNormsInPhase: (currentPhaseId: string) => Record[]; + getGoalsInPhase: (currentPhaseId: string) => Record[]; + getTriggersInPhase: (currentPhaseId: string) => Record[]; + // if more specific utility functions are needed they can be added here: +} + +/** + * the ProgramStore can be used to access all information of the most recently sent program, + * it contains basic functions to set and get the current program. + * And it contains some utility functions that allow you to easily gain access + * to the norms, triggers and goals of a specific phase. + */ +const useProgramStore = create((set, get) => ({ + currentProgram: { phases: [] as Record[]}, + /** + * sets the current program by cloning the provided program using a structuredClone + */ + setProgramState: (program: ReducedProgram) => set({currentProgram: structuredClone(program)}), + /** + * gets the current program + */ + getProgramState: () => get().currentProgram, + + // utility functions: + /** + * gets the ids of all phases in the program + */ + getPhaseIds: () => get().currentProgram.phases.map(entry => entry["id"] as string), + /** + * gets the norms for the provided phase + */ + getNormsInPhase: (currentPhaseId) => { + const program = get().currentProgram; + const phase = program.phases.find(val => val["id"] === currentPhaseId); + if (phase) { + return phase["norms"] as Record[]; + } + throw new Error(`phase with id:"${currentPhaseId}" not found`) + }, + /** + * gets the goals for the provided phase + */ + getGoalsInPhase: (currentPhaseId) => { + const program = get().currentProgram; + const phase = program.phases.find(val => val["id"] === currentPhaseId); + if (phase) { + return phase["goals"] as Record[]; + } + throw new Error(`phase with id:"${currentPhaseId}" not found`) + }, + /** + * gets the triggers for the provided phase + */ + getTriggersInPhase: (currentPhaseId) => { + const program = get().currentProgram; + const phase = program.phases.find(val => val["id"] === currentPhaseId); + if (phase) { + return phase["triggers"] as Record[]; + } + throw new Error(`phase with id:"${currentPhaseId}" not found`) + } +})); + +export default useProgramStore; \ No newline at end of file diff --git a/test/setupFlowTests.ts b/test/setupFlowTests.ts index 3ce8c3a..c37cd0e 100644 --- a/test/setupFlowTests.ts +++ b/test/setupFlowTests.ts @@ -2,6 +2,11 @@ import '@testing-library/jest-dom'; import { cleanup } from '@testing-library/react'; import useFlowStore from '../src/pages/VisProgPage/visualProgrammingUI/VisProgStores.tsx'; +if (!globalThis.structuredClone) { + globalThis.structuredClone = (obj: any) => { + return JSON.parse(JSON.stringify(obj)); + }; +} // To make sure that the tests are working, it's important that you are using // this implementation of ResizeObserver and DOMMatrixReadOnly diff --git a/test/utils/programStore.test.ts b/test/utils/programStore.test.ts new file mode 100644 index 0000000..4109eac --- /dev/null +++ b/test/utils/programStore.test.ts @@ -0,0 +1,100 @@ +import useProgramStore, {type ReducedProgram} from "../../src/utils/programStore.ts"; + + +describe('useProgramStore', () => { + beforeEach(() => { + // Reset store before each test + useProgramStore.setState({ + currentProgram: { phases: [] }, + }); + }); + + const mockProgram: ReducedProgram = { + phases: [ + { + id: 'phase-1', + norms: [{ id: 'norm-1' }], + goals: [{ id: 'goal-1' }], + triggers: [{ id: 'trigger-1' }], + }, + { + id: 'phase-2', + norms: [{ id: 'norm-2' }], + goals: [{ id: 'goal-2' }], + triggers: [{ id: 'trigger-2' }], + }, + ], + }; + + it('should set and get the program state', () => { + useProgramStore.getState().setProgramState(mockProgram); + + const program = useProgramStore.getState().getProgramState(); + expect(program).toEqual(mockProgram); + }); + + it('should return the ids of all phases in the program', () => { + useProgramStore.getState().setProgramState(mockProgram); + + const phaseIds = useProgramStore.getState().getPhaseIds(); + expect(phaseIds).toEqual(['phase-1', 'phase-2']); + }); + + it('should return all norms for a given phase', () => { + useProgramStore.getState().setProgramState(mockProgram); + + const norms = useProgramStore.getState().getNormsInPhase('phase-1'); + expect(norms).toEqual([{ id: 'norm-1' }]); + }); + + it('should return all goals for a given phase', () => { + useProgramStore.getState().setProgramState(mockProgram); + + const goals = useProgramStore.getState().getGoalsInPhase('phase-2'); + expect(goals).toEqual([{ id: 'goal-2' }]); + }); + + it('should return all triggers for a given phase', () => { + useProgramStore.getState().setProgramState(mockProgram); + + const triggers = useProgramStore.getState().getTriggersInPhase('phase-1'); + expect(triggers).toEqual([{ id: 'trigger-1' }]); + }); + + it('throws if phase does not exist when getting norms', () => { + useProgramStore.getState().setProgramState(mockProgram); + + expect(() => + useProgramStore.getState().getNormsInPhase('missing-phase') + ).toThrow('phase with id:"missing-phase" not found'); + }); + + it('throws if phase does not exist when getting goals', () => { + useProgramStore.getState().setProgramState(mockProgram); + + expect(() => + useProgramStore.getState().getGoalsInPhase('missing-phase') + ).toThrow('phase with id:"missing-phase" not found'); + }); + + it('throws if phase does not exist when getting triggers', () => { + useProgramStore.getState().setProgramState(mockProgram); + + expect(() => + useProgramStore.getState().getTriggersInPhase('missing-phase') + ).toThrow('phase with id:"missing-phase" not found'); + }); + + // this test should be at the bottom to avoid conflicts with the previous tests + it('should clone program state when setting it (no shared references should exist)', () => { + useProgramStore.getState().setProgramState(mockProgram); + + const storedProgram = useProgramStore.getState().getProgramState(); + + // mutate original + (mockProgram.phases[0].norms as any[]).push({ id: 'norm-mutated' }); + + // store should NOT change + expect(storedProgram.phases[0]['norms']).toHaveLength(1); + }); +}); \ No newline at end of file -- 2.49.1 From f0fe520ea09c0c6eb60014512055f9dfee809cae Mon Sep 17 00:00:00 2001 From: JobvAlewijk Date: Tue, 30 Dec 2025 18:10:51 +0100 Subject: [PATCH 2/7] feat: first version of simple program shown shows up if you run the program ref: N25B-405 --- src/pages/SimpleProgram/SimpleProgram.tsx | 136 ++++++++++++++++++++++ src/pages/VisProgPage/VisProg.tsx | 24 +++- 2 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 src/pages/SimpleProgram/SimpleProgram.tsx diff --git a/src/pages/SimpleProgram/SimpleProgram.tsx b/src/pages/SimpleProgram/SimpleProgram.tsx new file mode 100644 index 0000000..b682b1a --- /dev/null +++ b/src/pages/SimpleProgram/SimpleProgram.tsx @@ -0,0 +1,136 @@ +import React from "react"; +import styles from "../VisProgPage/VisProg.module.css"; + +/* ---------- Types (mirrors backend / reducer output) ---------- */ + +type Norm = { + id: string; + label: string; + norm: string; +}; + +type Goal = { + id: string; + label: string; + description: string; + achieved: boolean; +}; + +type TriggerKeyword = { + id: string; + keyword: string; +}; + +type KeywordTrigger = { + id: string; + label: string; + type: string; + keywords: TriggerKeyword[]; +}; + +type Phase = { + id: string; + label: string; + norms: Norm[]; + goals: Goal[]; + triggers: KeywordTrigger[]; +}; + +type SimpleProgramProps = { + phases: Phase[]; +}; + + +/* ---------- Component ---------- */ + + +/** + * SimpleProgram + * + * Read-only oversight view for a reduced program. + * Displays norms, goals, and triggers grouped per phase. + */ +const SimpleProgram: React.FC = ({ phases }) => { + return ( +
+

Simple Program Overview

+ + {phases.map((phase) => ( +
+

{phase.label}

+ + {/* Norms */} +
+

Norms

+ {phase.norms.length === 0 ? ( +

No norms defined.

+ ) : ( +
    + {phase.norms.map((norm) => ( +
  • + {norm.label}: {norm.norm} +
  • + ))} +
+ )} +
+ + {/* Goals */} +
+

Goals

+ {phase.goals.length === 0 ? ( +

No goals defined.

+ ) : ( +
    + {phase.goals.map((goal) => ( +
  • + {goal.label}: {goal.description}{" "} + + [{goal.achieved ? "✔" : "❌"}] + +
  • + ))} +
+ )} +
+ + {/* Triggers */} +
+

Triggers

+ {phase.triggers.length === 0 ? ( +

No triggers defined.

+ ) : ( +
    + {phase.triggers.map((trigger) => ( +
  • + {trigger.label} ({trigger.type}) +
      + {trigger.keywords.map((kw) => ( +
    • {kw.keyword}
    • + ))} +
    +
  • + ))} +
+ )} +
+
+ ))} +
+ ); +}; + +export default SimpleProgram; diff --git a/src/pages/VisProgPage/VisProg.tsx b/src/pages/VisProgPage/VisProg.tsx index 06e072c..60d66de 100644 --- a/src/pages/VisProgPage/VisProg.tsx +++ b/src/pages/VisProgPage/VisProg.tsx @@ -7,7 +7,7 @@ import { MarkerType, } from '@xyflow/react'; import '@xyflow/react/dist/style.css'; -import {useEffect} from "react"; +import {useEffect, useState} from "react"; import {useShallow} from 'zustand/react/shallow'; import {DndToolbar} from './visualProgrammingUI/components/DragDropSidebar.tsx'; import useFlowStore from './visualProgrammingUI/VisProgStores.tsx'; @@ -15,6 +15,7 @@ import type {FlowState} from './visualProgrammingUI/VisProgTypes.tsx'; import styles from './VisProg.module.css' import { NodeReduces, NodeTypes } from './visualProgrammingUI/NodeRegistry.ts'; import SaveLoadPanel from './visualProgrammingUI/components/SaveLoadPanel.tsx'; +import SimpleProgram from "../SimpleProgram/SimpleProgram.tsx"; // --| config starting params for flow |-- @@ -138,7 +139,7 @@ function VisualProgrammingUI() { } // currently outputs the prepared program to the console -function runProgram() { +function runProgramm() { const phases = graphReducer(); const program = {phases} console.log(JSON.stringify(program, null, 2)); @@ -174,6 +175,25 @@ function graphReducer() { * @constructor */ function VisProgPage() { + const [showSimpleProgram, setShowSimpleProgram] = useState(false); + const [phases, setPhases] = useState([]); + + const runProgram = () => { + const reducedPhases = graphReducer(); + setPhases(reducedPhases); + setShowSimpleProgram(true); + runProgramm(); + }; + + if (showSimpleProgram) { + return ( + + ); + } + return ( <> -- 2.49.1 From b0a5e4770c372f74e3818519f35cb4e9b4fd94f0 Mon Sep 17 00:00:00 2001 From: JobvAlewijk Date: Tue, 30 Dec 2025 20:56:05 +0100 Subject: [PATCH 3/7] feat: improved visuals and structure ref: N25B-402 --- .../SimpleProgram/SimpleProgram.module.css | 167 ++++++++++++++ src/pages/SimpleProgram/SimpleProgram.tsx | 206 +++++++++++------- 2 files changed, 293 insertions(+), 80 deletions(-) create mode 100644 src/pages/SimpleProgram/SimpleProgram.module.css diff --git a/src/pages/SimpleProgram/SimpleProgram.module.css b/src/pages/SimpleProgram/SimpleProgram.module.css new file mode 100644 index 0000000..69cc65c --- /dev/null +++ b/src/pages/SimpleProgram/SimpleProgram.module.css @@ -0,0 +1,167 @@ +/* ---------- Layout ---------- */ + +.container { + height: 100%; + display: flex; + flex-direction: column; + background: #1e1e1e; + color: #f5f5f5; +} + +.header { + display: flex; + justify-content: space-between; + align-items: center; + padding: clamp(0.75rem, 2vw, 1.25rem); + background: #2a2a2a; + border-bottom: 1px solid #3a3a3a; +} + +.header h2 { + font-size: clamp(1rem, 2.2vw, 1.4rem); + font-weight: 600; +} + +.controls button { + margin-left: 0.5rem; + padding: 0.4rem 0.9rem; + border-radius: 6px; + border: none; + background: #111; + color: white; + cursor: pointer; +} + +.controls button:disabled { + opacity: 0.4; + cursor: not-allowed; +} + +/* ---------- Content ---------- */ + +.content { + flex: 1; + padding: 2%; +} + +/* ---------- Grid ---------- */ + +.phaseGrid { + height: 100%; + display: grid; + grid-template-columns: repeat(2, minmax(0, 1fr)); + grid-template-rows: repeat(2, minmax(0, 1fr)); + gap: 2%; +} + +/* ---------- Box ---------- */ + +.box { + display: flex; + flex-direction: column; + background: #ffffff; + color: #1e1e1e; + border-radius: 10px; + overflow: hidden; + box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25); +} + +.boxHeader { + padding: 0.6rem 0.9rem; + background: linear-gradient(135deg, #dcdcdc, #e9e9e9); + font-style: italic; + font-weight: 500; + font-size: clamp(0.9rem, 1.5vw, 1.05rem); + border-bottom: 1px solid #cfcfcf; +} + +.boxContent { + flex: 1; + padding: 0.8rem 1rem; + overflow-y: auto; +} + +/* ---------- Lists ---------- */ + +.iconList { + list-style: none; + padding: 0; + margin: 0; +} + +.iconList li { + display: flex; + align-items: center; + gap: 0.6rem; + margin-bottom: 0.5rem; + font-size: clamp(0.85rem, 1.3vw, 1rem); +} + +.bulletList { + margin: 0; + padding-left: 1.2rem; +} + +.bulletList li { + margin-bottom: 0.4rem; +} + +/* ---------- Icons ---------- */ + +.successIcon, +.failIcon { + display: inline-flex; + align-items: center; + justify-content: center; + min-width: 1.5rem; + height: 1.5rem; + border-radius: 4px; + font-weight: bold; + color: white; + flex-shrink: 0; +} + +.successIcon { + background: #3cb371; +} + +.failIcon { + background: #e5533d; +} + +/* ---------- Empty ---------- */ + +.empty { + opacity: 0.55; + font-style: italic; + font-size: 0.9rem; +} + +/* ---------- Responsive ---------- */ + +@media (max-width: 900px) { + .phaseGrid { + grid-template-columns: 1fr; + grid-template-rows: repeat(4, minmax(0, 1fr)); + gap: 1rem; + } +} + +.leftControls { + display: flex; + align-items: center; + gap: 1rem; +} + +.backButton { + background: transparent; + border: 1px solid #555; + color: #ddd; + padding: 0.35rem 0.75rem; + border-radius: 6px; + cursor: pointer; +} + +.backButton:hover { + background: #333; +} diff --git a/src/pages/SimpleProgram/SimpleProgram.tsx b/src/pages/SimpleProgram/SimpleProgram.tsx index b682b1a..d548108 100644 --- a/src/pages/SimpleProgram/SimpleProgram.tsx +++ b/src/pages/SimpleProgram/SimpleProgram.tsx @@ -1,7 +1,7 @@ import React from "react"; -import styles from "../VisProgPage/VisProg.module.css"; +import styles from "./SimpleProgram.module.css"; -/* ---------- Types (mirrors backend / reducer output) ---------- */ +/* ---------- Types ---------- */ type Norm = { id: string; @@ -40,95 +40,141 @@ type SimpleProgramProps = { phases: Phase[]; }; +/* ---------- Reusable UI ---------- */ -/* ---------- Component ---------- */ +type BoxProps = { + title: string; + children: React.ReactNode; +}; +const Box: React.FC = ({ title, children }) => ( +
+
{title}
+
{children}
+
+); + +/* ---------- Lists ---------- */ + +const GoalList: React.FC<{ goals: Goal[] }> = ({ goals }) => { + if (goals.length === 0) return

No goals defined.

; -/** - * SimpleProgram - * - * Read-only oversight view for a reduced program. - * Displays norms, goals, and triggers grouped per phase. - */ -const SimpleProgram: React.FC = ({ phases }) => { return ( -
-

Simple Program Overview

- - {phases.map((phase) => ( -
-

{phase.label}

+ ← Back + +

+ Phase {phaseIndex + 1} / {phases.length}: {phase.label} +

- {/* Norms */} -
-

Norms

- {phase.norms.length === 0 ? ( -

No norms defined.

- ) : ( -
    - {phase.norms.map((norm) => ( -
  • - {norm.label}: {norm.norm} -
  • - ))} -
- )} -
+
+ - {/* Goals */} -
-

Goals

- {phase.goals.length === 0 ? ( -

No goals defined.

- ) : ( -
    - {phase.goals.map((goal) => ( -
  • - {goal.label}: {goal.description}{" "} - - [{goal.achieved ? "✔" : "❌"}] - -
  • - ))} -
- )} -
- - {/* Triggers */} -
-

Triggers

- {phase.triggers.length === 0 ? ( -

No triggers defined.

- ) : ( -
    - {phase.triggers.map((trigger) => ( -
  • - {trigger.label} ({trigger.type}) -
      - {trigger.keywords.map((kw) => ( -
    • {kw.keyword}
    • - ))} -
    -
  • - ))} -
- )} -
+
- ))} + +
+ +
+
); }; -- 2.49.1 From cd1aa84f897bfdb70a940307b1cf52ca9f52f25e Mon Sep 17 00:00:00 2001 From: JobvAlewijk Date: Fri, 2 Jan 2026 20:43:20 +0100 Subject: [PATCH 4/7] feat: using programstore ref: N25B-399 --- src/pages/SimpleProgram/SimpleProgram.tsx | 53 +++++++---------------- src/pages/VisProgPage/VisProg.tsx | 20 +++++---- 2 files changed, 27 insertions(+), 46 deletions(-) diff --git a/src/pages/SimpleProgram/SimpleProgram.tsx b/src/pages/SimpleProgram/SimpleProgram.tsx index d548108..16cb512 100644 --- a/src/pages/SimpleProgram/SimpleProgram.tsx +++ b/src/pages/SimpleProgram/SimpleProgram.tsx @@ -1,5 +1,6 @@ import React from "react"; import styles from "./SimpleProgram.module.css"; +import useProgramStore from "../../utils/programStore.ts"; /* ---------- Types ---------- */ @@ -36,10 +37,6 @@ type Phase = { triggers: KeywordTrigger[]; }; -type SimpleProgramProps = { - phases: Phase[]; -}; - /* ---------- Reusable UI ---------- */ type BoxProps = { @@ -63,11 +60,7 @@ const GoalList: React.FC<{ goals: Goal[] }> = ({ goals }) => {
    {goals.map((goal) => (
  • - + {goal.achieved ? "✔" : "✖"} {goal.description} @@ -77,11 +70,8 @@ const GoalList: React.FC<{ goals: Goal[] }> = ({ goals }) => { ); }; -const TriggerList: React.FC<{ triggers: KeywordTrigger[] }> = ({ - triggers, -}) => { - if (triggers.length === 0) - return

    No triggers defined.

    ; +const TriggerList: React.FC<{ triggers: KeywordTrigger[] }> = ({ triggers }) => { + if (triggers.length === 0) return

    No triggers defined.

    ; return (
      @@ -133,48 +123,37 @@ const PhaseGrid: React.FC<{ phase: Phase }> = ({ phase }) => { /* ---------- Main Component ---------- */ -const SimpleProgram: React.FC = ({ phases }) => { +const SimpleProgram: React.FC = () => { + // Get the phases from the program store + const phases = useProgramStore((state) => state.currentProgram.phases) as Phase[]; const [phaseIndex, setPhaseIndex] = React.useState(0); + + // If no phases are available, display a message + if (phases.length === 0) return

      No program loaded.

      ; + const phase = phases[phaseIndex]; return (
      -

      Phase {phaseIndex + 1} / {phases.length}: {phase.label}

      - -
      -
      - -
      +
      + +
      ); }; diff --git a/src/pages/VisProgPage/VisProg.tsx b/src/pages/VisProgPage/VisProg.tsx index e289580..3db9a00 100644 --- a/src/pages/VisProgPage/VisProg.tsx +++ b/src/pages/VisProgPage/VisProg.tsx @@ -181,21 +181,23 @@ function graphReducer() { */ function VisProgPage() { const [showSimpleProgram, setShowSimpleProgram] = useState(false); - const [phases, setPhases] = useState([]); + const setProgramState = useProgramStore((state) => state.setProgramState); const runProgram = () => { - const reducedPhases = graphReducer(); - setPhases(reducedPhases); - setShowSimpleProgram(true); - runProgramm(); + const phases = graphReducer(); // reduce graph + setProgramState({ phases }); // <-- save to store + setShowSimpleProgram(true); // show SimpleProgram + runProgramm(); // send to backend if needed }; if (showSimpleProgram) { return ( - +
      + + +
      ); } -- 2.49.1 From d80ced547cc054e37f2192a5fb03dc0c53967178 Mon Sep 17 00:00:00 2001 From: JobvAlewijk Date: Fri, 2 Jan 2026 20:55:24 +0100 Subject: [PATCH 5/7] feat: SimpleProgram no longer relies on types ref: N25B-399 --- src/pages/SimpleProgram/SimpleProgram.tsx | 163 ++++++++++++---------- 1 file changed, 92 insertions(+), 71 deletions(-) diff --git a/src/pages/SimpleProgram/SimpleProgram.tsx b/src/pages/SimpleProgram/SimpleProgram.tsx index 16cb512..56020a4 100644 --- a/src/pages/SimpleProgram/SimpleProgram.tsx +++ b/src/pages/SimpleProgram/SimpleProgram.tsx @@ -2,41 +2,6 @@ import React from "react"; import styles from "./SimpleProgram.module.css"; import useProgramStore from "../../utils/programStore.ts"; -/* ---------- Types ---------- */ - -type Norm = { - id: string; - label: string; - norm: string; -}; - -type Goal = { - id: string; - label: string; - description: string; - achieved: boolean; -}; - -type TriggerKeyword = { - id: string; - keyword: string; -}; - -type KeywordTrigger = { - id: string; - label: string; - type: string; - keywords: TriggerKeyword[]; -}; - -type Phase = { - id: string; - label: string; - norms: Norm[]; - goals: Goal[]; - triggers: KeywordTrigger[]; -}; - /* ---------- Reusable UI ---------- */ type BoxProps = { @@ -53,70 +18,111 @@ const Box: React.FC = ({ title, children }) => ( /* ---------- Lists ---------- */ -const GoalList: React.FC<{ goals: Goal[] }> = ({ goals }) => { - if (goals.length === 0) return

      No goals defined.

      ; +const GoalList: React.FC<{ goals: unknown[] }> = ({ goals }) => { + if (!goals.length) { + return

      No goals defined.

      ; + } return (
        - {goals.map((goal) => ( -
      • - - {goal.achieved ? "✔" : "✖"} - - {goal.description} -
      • - ))} + {goals.map((g, idx) => { + const goal = g as { + id?: string; + description?: string; + achieved?: boolean; + }; + + return ( +
      • + + {goal.achieved ? "✔" : "✖"} + + {goal.description ?? "Unnamed goal"} +
      • + ); + })}
      ); }; -const TriggerList: React.FC<{ triggers: KeywordTrigger[] }> = ({ triggers }) => { - if (triggers.length === 0) return

      No triggers defined.

      ; +const TriggerList: React.FC<{ triggers: unknown[] }> = ({ triggers }) => { + if (!triggers.length) { + return

      No triggers defined.

      ; + } return (
        - {triggers.map((trigger) => ( -
      • - - {trigger.label} -
      • - ))} + {triggers.map((t, idx) => { + const trigger = t as { + id?: string; + label?: string; + }; + + return ( +
      • + + {trigger.label ?? "Unnamed trigger"} +
      • + ); + })}
      ); }; -const NormList: React.FC<{ norms: Norm[] }> = ({ norms }) => { - if (norms.length === 0) return

      No norms defined.

      ; +const NormList: React.FC<{ norms: unknown[] }> = ({ norms }) => { + if (!norms.length) { + return

      No norms defined.

      ; + } return (
        - {norms.map((norm) => ( -
      • {norm.norm}
      • - ))} + {norms.map((n, idx) => { + const norm = n as { + id?: string; + norm?: string; + }; + + return
      • {norm.norm ?? "Unnamed norm"}
      • ; + })}
      ); }; /* ---------- Phase Grid ---------- */ -const PhaseGrid: React.FC<{ phase: Phase }> = ({ phase }) => { +type PhaseGridProps = { + norms: unknown[]; + goals: unknown[]; + triggers: unknown[]; +}; + +const PhaseGrid: React.FC = ({ + norms, + goals, + triggers, +}) => { return (
      - + - + - +

      No conditional norms defined.

      + {/* Let er dus op dat deze erbij moeten */}
      ); }; @@ -124,35 +130,50 @@ const PhaseGrid: React.FC<{ phase: Phase }> = ({ phase }) => { /* ---------- Main Component ---------- */ const SimpleProgram: React.FC = () => { - // Get the phases from the program store - const phases = useProgramStore((state) => state.currentProgram.phases) as Phase[]; + const getPhaseIds = useProgramStore((s) => s.getPhaseIds); + const getNormsInPhase = useProgramStore((s) => s.getNormsInPhase); + const getGoalsInPhase = useProgramStore((s) => s.getGoalsInPhase); + const getTriggersInPhase = useProgramStore((s) => s.getTriggersInPhase); + + const phaseIds = getPhaseIds(); const [phaseIndex, setPhaseIndex] = React.useState(0); - // If no phases are available, display a message - if (phases.length === 0) return

      No program loaded.

      ; + if (phaseIds.length === 0) { + return

      No program loaded.

      ; + } - const phase = phases[phaseIndex]; + const phaseId = phaseIds[phaseIndex]; return (

      - Phase {phaseIndex + 1} / {phases.length}: {phase.label} + Phase {phaseIndex + 1} / {phaseIds.length}

      - -
      - +
      ); -- 2.49.1 From 7b05c7344c23f8b0c71b7e6f8811422e969beac4 Mon Sep 17 00:00:00 2001 From: JobvAlewijk Date: Fri, 2 Jan 2026 21:06:41 +0100 Subject: [PATCH 6/7] feat: added tests ref: N25B-399 --- src/pages/SimpleProgram/SimpleProgram.tsx | 64 ++++++++++------- test/pages/simpleProgram/SimpleProgram.tsx | 83 ++++++++++++++++++++++ 2 files changed, 120 insertions(+), 27 deletions(-) create mode 100644 test/pages/simpleProgram/SimpleProgram.tsx diff --git a/src/pages/SimpleProgram/SimpleProgram.tsx b/src/pages/SimpleProgram/SimpleProgram.tsx index 56020a4..0f63653 100644 --- a/src/pages/SimpleProgram/SimpleProgram.tsx +++ b/src/pages/SimpleProgram/SimpleProgram.tsx @@ -2,8 +2,9 @@ import React from "react"; import styles from "./SimpleProgram.module.css"; import useProgramStore from "../../utils/programStore.ts"; -/* ---------- Reusable UI ---------- */ - +/** + * Generic container box with a header and content area. + */ type BoxProps = { title: string; children: React.ReactNode; @@ -16,8 +17,10 @@ const Box: React.FC = ({ title, children }) => (
); -/* ---------- Lists ---------- */ - +/** + * Renders a list of goals for a phase. + * Expects goal-like objects from the program store. + */ const GoalList: React.FC<{ goals: unknown[] }> = ({ goals }) => { if (!goals.length) { return

No goals defined.

; @@ -49,6 +52,9 @@ const GoalList: React.FC<{ goals: unknown[] }> = ({ goals }) => { ); }; +/** + * Renders a list of triggers for a phase. + */ const TriggerList: React.FC<{ triggers: unknown[] }> = ({ triggers }) => { if (!triggers.length) { return

No triggers defined.

; @@ -73,6 +79,9 @@ const TriggerList: React.FC<{ triggers: unknown[] }> = ({ triggers }) => { ); }; +/** + * Renders a list of norms for a phase. + */ const NormList: React.FC<{ norms: unknown[] }> = ({ norms }) => { if (!norms.length) { return

No norms defined.

; @@ -92,8 +101,9 @@ const NormList: React.FC<{ norms: unknown[] }> = ({ norms }) => { ); }; -/* ---------- Phase Grid ---------- */ - +/** + * Displays all phase-related information in a grid layout. + */ type PhaseGridProps = { norms: unknown[]; goals: unknown[]; @@ -104,31 +114,31 @@ const PhaseGrid: React.FC = ({ norms, goals, triggers, -}) => { - return ( -
- - - +}) => ( +
+ + + - - - + + + - - - + + + - -

No conditional norms defined.

-
- {/* Let er dus op dat deze erbij moeten */} -
- ); -}; - -/* ---------- Main Component ---------- */ + +

No conditional norms defined.

+
+
+); +/** + * Main program viewer. + * Reads all data from the program store and allows + * navigating between phases. + */ const SimpleProgram: React.FC = () => { const getPhaseIds = useProgramStore((s) => s.getPhaseIds); const getNormsInPhase = useProgramStore((s) => s.getNormsInPhase); diff --git a/test/pages/simpleProgram/SimpleProgram.tsx b/test/pages/simpleProgram/SimpleProgram.tsx new file mode 100644 index 0000000..22fcbbf --- /dev/null +++ b/test/pages/simpleProgram/SimpleProgram.tsx @@ -0,0 +1,83 @@ +import { render, screen, fireEvent } from "@testing-library/react"; +import SimpleProgram from "../../../src/pages/SimpleProgram/SimpleProgram"; +import useProgramStore from "../../../src/utils/programStore"; + +/** + * Helper to preload the program store before rendering. + */ +function loadProgram(phases: Record[]) { + useProgramStore.getState().setProgramState({ phases }); +} + +describe("SimpleProgram", () => { + beforeEach(() => { + loadProgram([]); + }); + + test("shows empty state when no program is loaded", () => { + render(); + expect(screen.getByText("No program loaded.")).toBeInTheDocument(); + }); + + test("renders first phase content", () => { + loadProgram([ + { + id: "phase-1", + norms: [{ id: "n1", norm: "Be polite" }], + goals: [{ id: "g1", description: "Finish task", achieved: true }], + triggers: [{ id: "t1", label: "Keyword trigger" }], + }, + ]); + + render(); + + expect(screen.getByText("Phase 1 / 1")).toBeInTheDocument(); + expect(screen.getByText("Be polite")).toBeInTheDocument(); + expect(screen.getByText("Finish task")).toBeInTheDocument(); + expect(screen.getByText("Keyword trigger")).toBeInTheDocument(); + }); + + test("allows navigating between phases", () => { + loadProgram([ + { + id: "phase-1", + norms: [], + goals: [], + triggers: [], + }, + { + id: "phase-2", + norms: [{ id: "n2", norm: "Be careful" }], + goals: [], + triggers: [], + }, + ]); + + render(); + + expect(screen.getByText("Phase 1 / 2")).toBeInTheDocument(); + + fireEvent.click(screen.getByText("Next ▶")); + + expect(screen.getByText("Phase 2 / 2")).toBeInTheDocument(); + expect(screen.getByText("Be careful")).toBeInTheDocument(); + }); + + test("prev button is disabled on first phase", () => { + loadProgram([ + { id: "phase-1", norms: [], goals: [], triggers: [] }, + ]); + + render(); + expect(screen.getByText("◀ Prev")).toBeDisabled(); + }); + + test("next button is disabled on last phase", () => { + loadProgram([ + { id: "phase-1", norms: [], goals: [], triggers: [] }, + ]); + + render(); + expect(screen.getByText("Next ▶")).toBeDisabled(); + }); +}); -- 2.49.1 From 2ecb33dcde88733bff681022c5b8712e94a3772c Mon Sep 17 00:00:00 2001 From: JobvAlewijk Date: Tue, 6 Jan 2026 15:19:49 +0100 Subject: [PATCH 7/7] chore: now actually runs tests --- src/pages/VisProgPage/VisProg.tsx | 8 +- .../simpleProgram/SimpleProgram.test.tsx | 176 ++++++++++++++++++ test/pages/simpleProgram/SimpleProgram.tsx | 83 --------- 3 files changed, 180 insertions(+), 87 deletions(-) create mode 100644 test/pages/simpleProgram/SimpleProgram.test.tsx delete mode 100644 test/pages/simpleProgram/SimpleProgram.tsx diff --git a/src/pages/VisProgPage/VisProg.tsx b/src/pages/VisProgPage/VisProg.tsx index 3db9a00..5311feb 100644 --- a/src/pages/VisProgPage/VisProg.tsx +++ b/src/pages/VisProgPage/VisProg.tsx @@ -140,7 +140,7 @@ function VisualProgrammingUI() { } // currently outputs the prepared program to the console -function runProgramm() { +function runProgram() { const phases = graphReducer(); const program = {phases} console.log(JSON.stringify(program, null, 2)); @@ -183,11 +183,11 @@ function VisProgPage() { const [showSimpleProgram, setShowSimpleProgram] = useState(false); const setProgramState = useProgramStore((state) => state.setProgramState); - const runProgram = () => { + const onClick = () => { const phases = graphReducer(); // reduce graph setProgramState({ phases }); // <-- save to store setShowSimpleProgram(true); // show SimpleProgram - runProgramm(); // send to backend if needed + runProgram(); // send to backend if needed }; if (showSimpleProgram) { @@ -204,7 +204,7 @@ function VisProgPage() { return ( <> - + ) } diff --git a/test/pages/simpleProgram/SimpleProgram.test.tsx b/test/pages/simpleProgram/SimpleProgram.test.tsx new file mode 100644 index 0000000..dcb56ba --- /dev/null +++ b/test/pages/simpleProgram/SimpleProgram.test.tsx @@ -0,0 +1,176 @@ +import { render, screen, fireEvent } from "@testing-library/react"; +import SimpleProgram from "../../../src/pages/SimpleProgram/SimpleProgram"; +import useProgramStore from "../../../src/utils/programStore"; + +/** + * Helper to preload the program store before rendering. + */ +function loadProgram(phases: Record[]) { + useProgramStore.getState().setProgramState({ phases }); +} + +describe("SimpleProgram", () => { + beforeEach(() => { + loadProgram([]); + }); + + test("shows empty state when no program is loaded", () => { + render(); + expect(screen.getByText("No program loaded.")).toBeInTheDocument(); + }); + + test("renders first phase content", () => { + loadProgram([ + { + id: "phase-1", + norms: [{ id: "n1", norm: "Be polite" }], + goals: [{ id: "g1", description: "Finish task", achieved: true }], + triggers: [{ id: "t1", label: "Keyword trigger" }], + }, + ]); + + render(); + + expect(screen.getByText("Phase 1 / 1")).toBeInTheDocument(); + expect(screen.getByText("Be polite")).toBeInTheDocument(); + expect(screen.getByText("Finish task")).toBeInTheDocument(); + expect(screen.getByText("Keyword trigger")).toBeInTheDocument(); + }); + + test("renders empty messages when phase has no data", () => { + loadProgram([ + { + id: "phase-1", + norms: [], + goals: [], + triggers: [], + }, + ]); + + render(); + + expect(screen.getAllByText("No norms defined.").length).toBeGreaterThanOrEqual(1); + expect(screen.getAllByText("No goals defined.").length).toBeGreaterThanOrEqual(1); + expect(screen.getAllByText("No triggers defined.").length).toBeGreaterThanOrEqual(1); + expect( + screen.getByText("No conditional norms defined.") + ).toBeInTheDocument(); + }); + + test("allows navigating between phases", () => { + loadProgram([ + { + id: "phase-1", + norms: [], + goals: [], + triggers: [], + }, + { + id: "phase-2", + norms: [{ id: "n2", norm: "Be careful" }], + goals: [], + triggers: [], + }, + ]); + + render(); + + expect(screen.getByText("Phase 1 / 2")).toBeInTheDocument(); + + fireEvent.click(screen.getByText("Next ▶")); + + expect(screen.getByText("Phase 2 / 2")).toBeInTheDocument(); + expect(screen.getByText("Be careful")).toBeInTheDocument(); + }); + + test("prev button is disabled on first phase", () => { + loadProgram([{ id: "phase-1", norms: [], goals: [], triggers: [] }]); + + render(); + expect(screen.getByText("◀ Prev")).toBeDisabled(); + }); + + test("next button is disabled on last phase", () => { + loadProgram([{ id: "phase-1", norms: [], goals: [], triggers: [] }]); + + render(); + expect(screen.getByText("Next ▶")).toBeDisabled(); + }); + + test("prev and next buttons enable/disable correctly when navigating", () => { + loadProgram([ + { id: "p1", norms: [], goals: [], triggers: [] }, + { id: "p2", norms: [], goals: [], triggers: [] }, + ]); + + render(); + + const prev = screen.getByText("◀ Prev"); + const next = screen.getByText("Next ▶"); + + expect(prev).toBeDisabled(); + expect(next).not.toBeDisabled(); + + fireEvent.click(next); + + expect(prev).not.toBeDisabled(); + expect(next).toBeDisabled(); + }); + + test("renders achieved and unachieved goals with correct icons", () => { + loadProgram([ + { + id: "phase-1", + norms: [], + goals: [ + { id: "g1", description: "Done goal", achieved: true }, + { id: "g2", description: "Failed goal", achieved: false }, + ], + triggers: [], + }, + ]); + + render(); + + expect(screen.getByText("✔")).toBeInTheDocument(); + expect(screen.getByText("✖")).toBeInTheDocument(); + expect(screen.getByText("Done goal")).toBeInTheDocument(); + expect(screen.getByText("Failed goal")).toBeInTheDocument(); + }); + + test("renders fallback labels when optional fields are missing", () => { + loadProgram([ + { + id: "phase-1", + norms: [{}], + goals: [{}], + triggers: [{}], + }, + ]); + + render(); + + expect(screen.getByText("Unnamed norm")).toBeInTheDocument(); + expect(screen.getByText("Unnamed goal")).toBeInTheDocument(); + expect(screen.getByText("Unnamed trigger")).toBeInTheDocument(); + }); + + test("does not crash when navigating beyond boundaries", () => { + loadProgram([ + { id: "p1", norms: [], goals: [], triggers: [] }, + { id: "p2", norms: [], goals: [], triggers: [] }, + ]); + + render(); + + fireEvent.click(screen.getByText("Next ▶")); + fireEvent.click(screen.getByText("Next ▶")); + + expect(screen.getByText("Phase 2 / 2")).toBeInTheDocument(); + + fireEvent.click(screen.getByText("◀ Prev")); + fireEvent.click(screen.getByText("◀ Prev")); + + expect(screen.getByText("Phase 1 / 2")).toBeInTheDocument(); + }); +}); diff --git a/test/pages/simpleProgram/SimpleProgram.tsx b/test/pages/simpleProgram/SimpleProgram.tsx deleted file mode 100644 index 22fcbbf..0000000 --- a/test/pages/simpleProgram/SimpleProgram.tsx +++ /dev/null @@ -1,83 +0,0 @@ -import { render, screen, fireEvent } from "@testing-library/react"; -import SimpleProgram from "../../../src/pages/SimpleProgram/SimpleProgram"; -import useProgramStore from "../../../src/utils/programStore"; - -/** - * Helper to preload the program store before rendering. - */ -function loadProgram(phases: Record[]) { - useProgramStore.getState().setProgramState({ phases }); -} - -describe("SimpleProgram", () => { - beforeEach(() => { - loadProgram([]); - }); - - test("shows empty state when no program is loaded", () => { - render(); - expect(screen.getByText("No program loaded.")).toBeInTheDocument(); - }); - - test("renders first phase content", () => { - loadProgram([ - { - id: "phase-1", - norms: [{ id: "n1", norm: "Be polite" }], - goals: [{ id: "g1", description: "Finish task", achieved: true }], - triggers: [{ id: "t1", label: "Keyword trigger" }], - }, - ]); - - render(); - - expect(screen.getByText("Phase 1 / 1")).toBeInTheDocument(); - expect(screen.getByText("Be polite")).toBeInTheDocument(); - expect(screen.getByText("Finish task")).toBeInTheDocument(); - expect(screen.getByText("Keyword trigger")).toBeInTheDocument(); - }); - - test("allows navigating between phases", () => { - loadProgram([ - { - id: "phase-1", - norms: [], - goals: [], - triggers: [], - }, - { - id: "phase-2", - norms: [{ id: "n2", norm: "Be careful" }], - goals: [], - triggers: [], - }, - ]); - - render(); - - expect(screen.getByText("Phase 1 / 2")).toBeInTheDocument(); - - fireEvent.click(screen.getByText("Next ▶")); - - expect(screen.getByText("Phase 2 / 2")).toBeInTheDocument(); - expect(screen.getByText("Be careful")).toBeInTheDocument(); - }); - - test("prev button is disabled on first phase", () => { - loadProgram([ - { id: "phase-1", norms: [], goals: [], triggers: [] }, - ]); - - render(); - expect(screen.getByText("◀ Prev")).toBeDisabled(); - }); - - test("next button is disabled on last phase", () => { - loadProgram([ - { id: "phase-1", norms: [], goals: [], triggers: [] }, - ]); - - render(); - expect(screen.getByText("Next ▶")).toBeDisabled(); - }); -}); -- 2.49.1