# Copyright (c) 2023 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# !!!!!!!!!! TEST INFORMATION !!!!!!!!!!!!!!!!!!
# This test file tests the FabricSceneInfo attribute of the scenes cluster

name: Scenes FabricSceneInfo testing

PICS:
    - MCORE.ROLE.COMMISSIONEE
    - APPDEVICE.S
    - S.S
    - S.S.F03
    - S.S.A0007

config:
    nodeId: 0x12344321
    cluster: "Scenes Management"
    endpoint: 1
    payload:
        type: char_string
        defaultValue: "MT:-24J0AFN00KA0648G00"
    discriminator:
        type: int16u
        defaultValue: 3840
    waitAfterCommissioning:
        type: int16u
        defaultValue: 5000
    PakeVerifier:
        type: octet_string
        defaultValue: "hex:b96170aae803346884724fe9a3b287c30330c2a660375d17bb205a8cf1aecb350457f8ab79ee253ab6a8e46bb09e543ae422736de501e3db37d441fe344920d09548e4c18240630c4ff4913c53513839b7c07fcc0627a1b8573a149fcd1fa466cf"
    G1:
        type: group_id
        defaultValue: 0x0001
    G2:
        type: group_id
        defaultValue: 0x0002

tests:
    - label: "Commission DUT to TH1"
      cluster: "DelayCommands"
      command: "WaitForCommissionee"
      arguments:
          values:
              - name: "nodeId"
                value: nodeId

    - label: "TH1 reads the fabric index"
      cluster: "Operational Credentials"
      endpoint: 0
      command: "readAttribute"
      attribute: "CurrentFabricIndex"
      response:
          saveAs: th1FabricIndex

    - label: "Read the FabricSceneInfo attribute (0x0007) "
      command: "readAttribute"
      attribute: "FabricSceneInfo"
      response:
          constraints:
              type: list

    - label: "TH reads from the DUT the (0x0006) SceneTableSize attribute"
      command: "readAttribute"
      attribute: "SceneTableSize"
      response:
          values:
              - name: "SceneTableSize"
                saveAs: maxScenes

    - label: "Arithmetic operation to get the maxScenes - 1"
      cluster: "Unit Testing"
      command: "TestAddArguments"
      arguments:
          values:
              - name: "arg1"
                value: maxScenes - 1
              - name: "arg2"
                value: 0
      response:
          values:
              - name: "returnValue"
                saveAs: maxScenesMinusOne
                value: maxScenes - 1

    - label: "Arithmetic operation to get the fabric Capacity"
      cluster: "Unit Testing"
      command: "TestAddArguments"
      arguments:
          values:
              - name: "arg1"
                value: maxScenesMinusOne / 2
              - name: "arg2"
                value: 0
      response:
          values:
              - name: "returnValue"
                saveAs: fabricCapacity
                value: maxScenesMinusOne / 2
    - label: "Preparation step :Add Group KeySet."
      cluster: "Group Key Management"
      endpoint: 0
      command: "KeySetWrite"
      arguments:
          values:
              - name: "GroupKeySet"
                value:
                    {
                        GroupKeySetID: 0x01a1,
                        GroupKeySecurityPolicy: 0,
                        EpochKey0: "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf",
                        EpochStartTime0: 1110000,
                        EpochKey1: "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf",
                        EpochStartTime1: 1110001,
                        EpochKey2: "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf",
                        EpochStartTime2: 1110002,
                    }

    - label: "Preparation step for using the Write Group Keys command."
      cluster: "Group Key Management"
      endpoint: 0
      command: "writeAttribute"
      attribute: "GroupKeyMap"
      arguments:
          value:
              [
                  { FabricIndex: 0, GroupId: G1, GroupKeySetID: 0x01a1 },
                  { FabricIndex: 0, GroupId: G2, GroupKeySetID: 0x01a1 },
              ]

    - label: "TH sends a RemoveAllGroups command to DUT."
      cluster: "Groups"
      command: "RemoveAllGroups"

    - label: "TH sends a AddGroup command to DUT for G1."
      cluster: "Groups"
      command: "AddGroup"
      arguments:
          values:
              - name: "GroupID"
                value: G1
              - name: "GroupName"
                value: "Group1"
      response:
          values:
              - name: "Status"
                value: 0
              - name: "GroupID"
                value: G1

    - label: "TH sends a AddGroup command to DUT for G2."
      cluster: "Groups"
      command: "AddGroup"
      arguments:
          values:
              - name: "GroupID"
                value: G2
              - name: "GroupName"
                value: "Group2"
      response:
          values:
              - name: "Status"
                value: 0
              - name: "GroupID"
                value: G2

    - label: "TH confirms the Fabric Capacity is Maximum"
      command: "GetSceneMembership"
      arguments:
          values:
              - name: "GroupID"
                value: G1
      response:
          values:
              - name: "Status"
                value: 0x00
              - name: "Capacity"
                value: fabricCapacity
              - name: "GroupID"
                value: G1

    - label: "TH sends an AddScene command to DUT for Scene 1 G1"
      command: "AddScene"
      arguments:
          values:
              - name: "GroupID"
                value: G1
              - name: "SceneID"
                value: 0x01
              - name: "TransitionTime"
                value: 1000
              - name: "SceneName"
                value: "Scene1"
              - name: "ExtensionFieldSets"
                value:
                    [
                        {
                            ClusterID: 0x0006,
                            AttributeValueList:
                                [{ AttributeID: 0x0000, ValueUnsigned8: 0x01 }],
                        },
                        {
                            ClusterID: 0x0008,
                            AttributeValueList:
                                [{ AttributeID: 0x0000, ValueUnsigned8: 0x64 }],
                        },
                        {
                            ClusterID: 0x0300,
                            AttributeValueList:
                                [
                                    {
                                        AttributeID: 0x0001,
                                        ValueUnsigned8: 0x64,
                                    },
                                    {
                                        AttributeID: 0x0003,
                                        ValueUnsigned16: 0x64,
                                    },
                                    {
                                        AttributeID: 0x0004,
                                        ValueUnsigned16: 0x64,
                                    },
                                    {
                                        AttributeID: 0x0007,
                                        ValueUnsigned16: 0x64,
                                    },
                                    {
                                        AttributeID: 0x4000,
                                        ValueUnsigned16: 0x64,
                                    },
                                    {
                                        AttributeID: 0x4001,
                                        ValueUnsigned8: 0x64,
                                    },
                                    {
                                        AttributeID: 0x4002,
                                        ValueUnsigned8: 0x64,
                                    },
                                    {
                                        AttributeID: 0x4003,
                                        ValueUnsigned8: 0x64,
                                    },
                                ],
                        },
                        {
                            ClusterID: 0x0050,
                            AttributeValueList:
                                [{ AttributeID: 0x0003, ValueUnsigned8: 0x01 }],
                        },
                    ]
      response:
          values:
              - name: "Status"
                value: 0x00
              - name: "GroupID"
                value: G1
              - name: "SceneID"
                value: 0x01

    - label:
          "TH confirms the Scene was added to the Scene table by
          GetSceneMembership"
      command: "GetSceneMembership"
      arguments:
          values:
              - name: "GroupID"
                value: G1
      response:
          values:
              - name: "Status"
                value: 0x00
              - name: "Capacity"
                value: fabricCapacity -1
              - name: "GroupID"
                value: G1

    - label: "TH confirms the Scene Was properly added with ViewScene"
      command: "ViewScene"
      arguments:
          values:
              - name: "GroupID"
                value: G1
              - name: "SceneID"
                value: 0x01
      response:
          values:
              - name: "Status"
                value: 0x00
              - name: "GroupID"
                value: G1
              - name: "SceneID"
                value: 0x01
              - name: "TransitionTime"
                value: 1000
              - name: "ExtensionFieldSets"
                value:
                    [
                        {
                            ClusterID: 0x0006,
                            AttributeValueList:
                                [{ AttributeID: 0x0000, ValueUnsigned8: 0x01 }],
                        },
                        {
                            ClusterID: 0x0008,
                            AttributeValueList:
                                [{ AttributeID: 0x0000, ValueUnsigned8: 0x64 }],
                        },
                        {
                            ClusterID: 0x0300,
                            AttributeValueList:
                                [
                                    {
                                        AttributeID: 0x0001,
                                        ValueUnsigned8: 0x64,
                                    },
                                    {
                                        AttributeID: 0x0003,
                                        ValueUnsigned16: 0x64,
                                    },
                                    {
                                        AttributeID: 0x0004,
                                        ValueUnsigned16: 0x64,
                                    },
                                    {
                                        AttributeID: 0x0007,
                                        ValueUnsigned16: 0x64,
                                    },
                                    {
                                        AttributeID: 0x4000,
                                        ValueUnsigned16: 0x64,
                                    },
                                    {
                                        AttributeID: 0x4001,
                                        ValueUnsigned8: 0x64,
                                    },
                                    {
                                        AttributeID: 0x4002,
                                        ValueUnsigned8: 0x64,
                                    },
                                    {
                                        AttributeID: 0x4003,
                                        ValueUnsigned8: 0x64,
                                    },
                                ],
                        },
                        {
                            ClusterID: 0x0050,
                            AttributeValueList:
                                [{ AttributeID: 0x0003, ValueUnsigned8: 0x01 }],
                        },
                    ]

    - label: "Read the FabricSceneInfo attribute (0x0007) "
      command: "readAttribute"
      attribute: "FabricSceneInfo"
      response:
          value:
              [
                  {
                      SceneCount: 1,
                      CurrentScene: 0x00,
                      CurrentGroup: 0x00,
                      FabricIndex: th1FabricIndex,
                      SceneValid: false,
                      RemainingCapacity: fabricCapacity - 1,
                  },
              ]

    - label: "TH sends a RecallScene command for Scene 1 Group 1"
      command: "RecallScene"
      arguments:
          values:
              - name: "GroupID"
                value: G1
              - name: "SceneID"
                value: 0x01

    - label: "Confirm the FabricSceneInfo attribute was properly updated"
      command: "readAttribute"
      attribute: "FabricSceneInfo"
      response:
          value:
              [
                  {
                      SceneCount: 1,
                      CurrentScene: 0x01,
                      CurrentGroup: G1,
                      FabricIndex: th1FabricIndex,
                      SceneValid: true,
                      RemainingCapacity: fabricCapacity - 1,
                  },
              ]

    - label: "TH sends an AddScene command to DUT for Scene 1 G1"
      command: "AddScene"
      arguments:
          values:
              - name: "GroupID"
                value: G2
              - name: "SceneID"
                value: 0x01
              - name: "TransitionTime"
                value: 2000
              - name: "SceneName"
                value: "Scene1"
              - name: "ExtensionFieldSets"
                value:
                    [
                        {
                            ClusterID: 0x0006,
                            AttributeValueList:
                                [{ AttributeID: 0x0000, ValueUnsigned8: 0x00 }],
                        },
                        {
                            ClusterID: 0x0008,
                            AttributeValueList:
                                [{ AttributeID: 0x0000, ValueUnsigned8: 0xFA }],
                        },
                    ]

    - label: "TH confirms the Scene Was properly added with ViewScene"
      command: "ViewScene"
      arguments:
          values:
              - name: "GroupID"
                value: G2
              - name: "SceneID"
                value: 0x01
      response:
          values:
              - name: "Status"
                value: 0x00
              - name: "GroupID"
                value: G2
              - name: "SceneID"
                value: 0x01
              - name: "TransitionTime"
                value: 2000
              - name: "ExtensionFieldSets"
                value:
                    [
                        {
                            ClusterID: 0x0006,
                            AttributeValueList:
                                [{ AttributeID: 0x0000, ValueUnsigned8: 0x00 }],
                        },
                        {
                            ClusterID: 0x0008,
                            AttributeValueList:
                                [{ AttributeID: 0x0000, ValueUnsigned8: 0xFA }],
                        },
                    ]

    - label: "Confirm the FabricSceneInfo attribute was properly updated"
      command: "readAttribute"
      attribute: "FabricSceneInfo"
      response:
          value:
              [
                  {
                      SceneCount: 2,
                      CurrentScene: 0x01,
                      CurrentGroup: G1,
                      FabricIndex: th1FabricIndex,
                      SceneValid: true,
                      RemainingCapacity: fabricCapacity - 2,
                  },
              ]

    - label: "TH sends a RecallScene command for Scene 1 Group 2"
      command: "RecallScene"
      arguments:
          values:
              - name: "GroupID"
                value: G2
              - name: "SceneID"
                value: 0x01

    - label: "Confirm the FabricSceneInfo attribute was properly updated"
      command: "readAttribute"
      attribute: "FabricSceneInfo"
      response:
          value:
              [
                  {
                      SceneCount: 2,
                      CurrentScene: 0x01,
                      CurrentGroup: G2,
                      FabricIndex: th1FabricIndex,
                      SceneValid: true,
                      RemainingCapacity: fabricCapacity - 2,
                  },
              ]

    - label:
          "Configures the DUT to a different state, should invalidate the scene"
      cluster: "Level Control"
      command: "MoveToLevelWithOnOff"
      arguments:
          values:
              - name: "Level"
                value: 200
              - name: "TransitionTime"
                value: 0
              - name: "OptionsMask"
                value: 1
              - name: "OptionsOverride"
                value: 1

    - label: "Confirm the FabricSceneInfo attribute was properly updated"
      command: "readAttribute"
      attribute: "FabricSceneInfo"
      response:
          value:
              [
                  {
                      SceneCount: 2,
                      CurrentScene: 0x01,
                      CurrentGroup: G2,
                      FabricIndex: th1FabricIndex,
                      SceneValid: false,
                      RemainingCapacity: fabricCapacity - 2,
                  },
              ]

    - label: "Store the current DUT state to a new scene"
      command: "StoreScene"
      arguments:
          values:
              - name: "GroupID"
                value: G1
              - name: "SceneID"
                value: 0x02
      response:
          values:
              - name: "Status"
                value: 0x00
              - name: "GroupID"
                value: G1
              - name: "SceneID"
                value: 0x02

    - label: "Confirm the FabricSceneInfo attribute was properly updated"
      command: "readAttribute"
      attribute: "FabricSceneInfo"
      response:
          value:
              [
                  {
                      SceneCount: 3,
                      CurrentScene: 0x02,
                      CurrentGroup: G1,
                      FabricIndex: th1FabricIndex,
                      SceneValid: true,
                      RemainingCapacity: fabricCapacity - 3,
                  },
              ]

    - label: "TH sends a RecallScene command for Scene 1 Group 2"
      command: "RecallScene"
      arguments:
          values:
              - name: "GroupID"
                value: G2
              - name: "SceneID"
                value: 0x01

    - label: "Confirm the FabricSceneInfo attribute was properly updated"
      command: "readAttribute"
      attribute: "FabricSceneInfo"
      response:
          value:
              [
                  {
                      SceneCount: 3,
                      CurrentScene: 0x01,
                      CurrentGroup: G2,
                      FabricIndex: th1FabricIndex,
                      SceneValid: true,
                      RemainingCapacity: fabricCapacity - 3,
                  },
              ]

    - label: "Remove Active scene"
      command: "RemoveScene"
      arguments:
          values:
              - name: "GroupID"
                value: G2
              - name: "SceneID"
                value: 0x01
      response:
          values:
              - name: "Status"
                value: 0x00
              - name: "GroupID"
                value: G2
              - name: "SceneID"
                value: 0x01

    - label: "Confirm the FabricSceneInfo attribute was properly updated"
      command: "readAttribute"
      attribute: "FabricSceneInfo"
      response:
          value:
              [
                  {
                      SceneCount: 2,
                      CurrentScene: 0x01,
                      CurrentGroup: G2,
                      FabricIndex: th1FabricIndex,
                      SceneValid: false,
                      RemainingCapacity: fabricCapacity - 2,
                  },
              ]

    - label: "Remove all scenes from G1"
      command: "RemoveAllScenes"
      arguments:
          values:
              - name: "GroupID"
                value: G1
      response:
          values:
              - name: "Status"
                value: 0
              - name: "GroupID"
                value: G1

    - label: "Confirm the FabricSceneInfo attribute was properly updated"
      command: "readAttribute"
      attribute: "FabricSceneInfo"
      response:
          value:
              [
                  {
                      SceneCount: 0,
                      CurrentScene: 0x01,
                      CurrentGroup: G2,
                      FabricIndex: th1FabricIndex,
                      SceneValid: false,
                      RemainingCapacity: fabricCapacity,
                  },
              ]
