aboutsummaryrefslogtreecommitdiff
path: root/tests/session-io.test.ts
blob: 6de919bc75bc52b9d562c1b46f54d7cc649645a8 (plain) (blame)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*** IMPORT ------------------------------------------- ***/

import { expect, test } from "vitest";

/*** UTILITY ------------------------------------------ ***/

import {
  tabToExport,
  validateSessionExport,
  type SessionExport
} from "../source/library/state/session-io.ts";

type TabInput = Parameters<typeof tabToExport>[0];

function validExport(): SessionExport {
  return {
    exportedAt: "2026-04-24T00:00:00.000Z",
    tabs: [
      {
        headers: "{}",
        operationName: "MyOp",
        query: "query MyOp { hello }",
        title: "MyOp",
        variables: "{}"
      }
    ],
    version: 1
  };
}

function isError(result: unknown): result is { error: string } {
  return typeof result === "object" &&
    result !== null &&
    "error" in result;
}

/*** TESTS -------------------------------------------- ***/

test("validateSessionExport round-trips a valid payload unchanged", () => {
  const data = validExport();
  const result = validateSessionExport(data);

  expect(isError(result)).toBe(false);
  expect(result).toEqual(data);
});

test("validateSessionExport rejects non-object input", () => {
  expect(isError(validateSessionExport(null))).toBe(true);
  expect(isError(validateSessionExport("nope"))).toBe(true);
  expect(isError(validateSessionExport(42))).toBe(true);
  expect(isError(validateSessionExport([]))).toBe(true);
});

test("validateSessionExport rejects wrong version", () => {
  const zero = validateSessionExport({ ...validExport(), version: 0 });
  const two = validateSessionExport({ ...validExport(), version: 2 });
  const missing = validateSessionExport({
    exportedAt: "2026-04-24T00:00:00.000Z",
    tabs: []
  });

  expect(isError(zero)).toBe(true);
  expect(isError(two)).toBe(true);
  expect(isError(missing)).toBe(true);
});

test("validateSessionExport rejects missing or non-string exportedAt", () => {
  const missing = validateSessionExport({ tabs: [], version: 1 });
  const wrongType = validateSessionExport({ exportedAt: 0, tabs: [], version: 1 });

  expect(isError(missing)).toBe(true);
  expect(isError(wrongType)).toBe(true);
});

test("validateSessionExport rejects non-array tabs", () => {
  const result = validateSessionExport({
    exportedAt: "2026-04-24T00:00:00.000Z",
    tabs: "not-an-array",
    version: 1
  });

  expect(isError(result)).toBe(true);
});

test("validateSessionExport rejects tab with non-string query", () => {
  const result = validateSessionExport({
    exportedAt: "2026-04-24T00:00:00.000Z",
    tabs: [
      {
        headers: "{}",
        operationName: null,
        query: 42,
        title: "t",
        variables: "{}"
      }
    ],
    version: 1
  });

  expect(isError(result)).toBe(true);
});

test("validateSessionExport rejects tab with wrong-typed operationName", () => {
  const result = validateSessionExport({
    exportedAt: "2026-04-24T00:00:00.000Z",
    tabs: [
      {
        headers: "{}",
        operationName: 42,
        query: "",
        title: "t",
        variables: "{}"
      }
    ],
    version: 1
  });

  expect(isError(result)).toBe(true);
});

test("validateSessionExport accepts null operationName", () => {
  const result = validateSessionExport({
    exportedAt: "2026-04-24T00:00:00.000Z",
    tabs: [
      {
        headers: "{}",
        operationName: null,
        query: "",
        title: "t",
        variables: "{}"
      }
    ],
    version: 1
  });

  expect(isError(result)).toBe(false);
});

test("validateSessionExport rejects string field > 1 MB", () => {
  const big = "x".repeat(1024 * 1024 + 1);
  const result = validateSessionExport({
    exportedAt: "2026-04-24T00:00:00.000Z",
    tabs: [
      {
        headers: "{}",
        operationName: null,
        query: big,
        title: "t",
        variables: "{}"
      }
    ],
    version: 1
  });

  expect(isError(result)).toBe(true);
});

test("validateSessionExport rejects > 50 tabs", () => {
  const tabs = Array.from({ length: 51 }, () => ({
    headers: "{}",
    operationName: null,
    query: "",
    title: "t",
    variables: "{}"
  }));
  const result = validateSessionExport({
    exportedAt: "2026-04-24T00:00:00.000Z",
    tabs,
    version: 1
  });

  expect(isError(result)).toBe(true);
});

test("tabToExport strips id, result, operations, titleDirty", () => {
  const tab: TabInput = {
    headers: "{\"x\":1}",
    id: "abc-123",
    operationName: "MyOp",
    operations: [{ name: "MyOp", type: "query" }],
    query: "query MyOp { hello }",
    result: "{\"data\":{}}",
    streamIntervals: [],
    timing: null,
    title: "MyOp",
    titleDirty: true,
    variables: "{}"
  };

  expect(tabToExport(tab)).toEqual({
    headers: "{\"x\":1}",
    operationName: "MyOp",
    query: "query MyOp { hello }",
    title: "MyOp",
    variables: "{}"
  });
});