code: def test_app_state_state_access_under_affiliation(*_):
      """Test the access to attributes when the state is restricted under the given affiliation."""
      mocked_state = {
          "vars": {"root_var": "root"},
          "flows": {
              "child0": {
                  "vars": {"child0_var": 0},
                  "flows": {
                      "child1": {
                          "vars": {"child1_var": 1},
                          "flows": {
                              "child2": {
                                  "vars": {"child2_var": 2},
                                  "flows": {},
                                  "works": {},
                              },
                          },
                          "works": {},
                      },
                  },
                  "works": {
                      "work1": {
                          "vars": {"work1_var": 11},
                      },
                  },
              },
          },
          "works": {},
      }
  
      # root-level affiliation
      state = AppState(my_affiliation=())
      state._store_state(mocked_state)
      assert isinstance(state.child0, AppState)
      assert state.child0.child0_var == 0
      assert state.child0.child1.child1_var == 1
      assert state.child0.child1.child2.child2_var == 2
  
      # one child deep
      state = AppState(my_affiliation=("child0",))
      state._store_state(mocked_state)
      assert state._state == mocked_state["flows"]["child0"]
      with pytest.raises(
          AttributeError,
          match="Failed to access 'child0' through `AppState`",
      ):
          _ = state.child0
      assert state.child0_var == 0
      assert state.child1.child1_var == 1
      assert state.child1.child2.child2_var == 2
  
      # two flows deep
      state = AppState(my_affiliation=("child0", "child1"))
      state._store_state(mocked_state)
      assert (
          state._state
          == mocked_state["flows"]["child0"]["flows"]["child1"]
      )
      with pytest.raises(
          AttributeError,
          match="Failed to access 'child1' through `AppState`",
      ):
          _ = state.child1
      state.child1_var = 111
      assert state.child1_var == 111
      assert state.child2.child2_var == 2
  
      # access to work
      state = AppState(my_affiliation=("child0", "work1"))
      state._store_state(mocked_state)
      assert (
          state._state
          == mocked_state["flows"]["child0"]["works"]["work1"]
      )
      with pytest.raises(
          AttributeError,
          match="Failed to access 'child1' through `AppState`",
      ):
          _ = state.child1
      assert state.work1_var == 11
      state.work1_var = 111
      assert state.work1_var == 111
  
      # affiliation does not match state
      state = AppState(my_affiliation=("child1", "child0"))
      with pytest.raises(
          ValueError,
          match=escape(
              "Failed to extract the state under the affiliation '('child1', 'child0')'"
          ),
      ):
          state._store_state(mocked_state)
  
imports: import os
  from re import escape
  from unittest import mock
  import lightning.app
  import pytest
  import requests
  from lightning.app import LightningApp, LightningFlow, LightningWork
  from lightning.app.structures import Dict, List
  from lightning.app.utilities.app_helpers import AppStatePlugin, BaseStatePlugin
  from lightning.app.utilities.state import AppState
  from lightning_cloud.openapi import Externalv1LightningappInstance, V1LightningappInstanceStatus
label_desc: Use image node_id 4 for calling a global function with example usage: test_app_state_state_access_under_affiliation() without return types