File size: 3,470 Bytes
10d1fd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { describe, expect, it } from "vitest";

describe("MainPage component logic", () => {
  it("should show results only when query is not empty", () => {
    const isQueryEmpty = (query: string) => query.length === 0;

    expect(isQueryEmpty("")).toBe(true);
    expect(isQueryEmpty("test query")).toBe(false);
    expect(isQueryEmpty("   ")).toBe(false);
  });

  it("should determine when to show search results section", () => {
    const shouldShowSearchResults = (
      textSearchState: string,
      imageSearchState: string,
    ) => textSearchState !== "idle" || imageSearchState !== "idle";

    expect(shouldShowSearchResults("idle", "idle")).toBe(false);
    expect(shouldShowSearchResults("loading", "idle")).toBe(true);
    expect(shouldShowSearchResults("idle", "loading")).toBe(true);
    expect(shouldShowSearchResults("success", "success")).toBe(true);
  });

  it("should determine when to show AI response section", () => {
    const shouldShowAiResponse = (
      textGenerationState: string,
      showEnableAiResponsePrompt: boolean,
    ) => !showEnableAiResponsePrompt && textGenerationState !== "idle";

    expect(shouldShowAiResponse("idle", false)).toBe(false);
    expect(shouldShowAiResponse("generating", false)).toBe(true);
    expect(shouldShowAiResponse("generating", true)).toBe(false);
    expect(shouldShowAiResponse("complete", false)).toBe(true);
  });

  it("should determine when to show enable AI prompt", () => {
    const shouldShowEnablePrompt = (showEnableAiResponsePrompt: boolean) =>
      showEnableAiResponsePrompt;

    expect(shouldShowEnablePrompt(true)).toBe(true);
    expect(shouldShowEnablePrompt(false)).toBe(false);
  });

  it("should combine conditions correctly for full page state", () => {
    interface PageState {
      query: string;
      textSearchState: string;
      imageSearchState: string;
      textGenerationState: string;
      showEnableAiResponsePrompt: boolean;
    }

    const getVisibleSections = (state: PageState) => {
      const isQueryEmpty = state.query.length === 0;
      return {
        showResults:
          !isQueryEmpty &&
          (state.textSearchState !== "idle" ||
            state.imageSearchState !== "idle"),
        showAiResponse:
          !isQueryEmpty &&
          !state.showEnableAiResponsePrompt &&
          state.textGenerationState !== "idle",
        showEnablePrompt: !isQueryEmpty && state.showEnableAiResponsePrompt,
      };
    };

    expect(
      getVisibleSections({
        query: "",
        textSearchState: "success",
        imageSearchState: "idle",
        textGenerationState: "generating",
        showEnableAiResponsePrompt: false,
      }),
    ).toEqual({
      showResults: false,
      showAiResponse: false,
      showEnablePrompt: false,
    });

    expect(
      getVisibleSections({
        query: "test",
        textSearchState: "success",
        imageSearchState: "idle",
        textGenerationState: "generating",
        showEnableAiResponsePrompt: false,
      }),
    ).toEqual({
      showResults: true,
      showAiResponse: true,
      showEnablePrompt: false,
    });

    expect(
      getVisibleSections({
        query: "test",
        textSearchState: "idle",
        imageSearchState: "idle",
        textGenerationState: "idle",
        showEnableAiResponsePrompt: true,
      }),
    ).toEqual({
      showResults: false,
      showAiResponse: false,
      showEnablePrompt: true,
    });
  });
});