gogcli-mcp 2.28.0 → 2.29.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -123,6 +123,147 @@ describe('runOrDiagnose', () => {
123
123
  expect(parsed.internalDateDisplay).toBeDefined();
124
124
  });
125
125
 
126
+ // Formatting whitespace is roughly a fifth of a large gog response and
127
+ // carries no information: gog pretty-prints its --json output, and nothing
128
+ // downstream reads the indent. Measured at 18.6% of a 19 KB `drive ls`.
129
+ it('minifies gog\'s pretty-printed JSON on the normal path', async () => {
130
+ vi.mocked(runner.run).mockResolvedValue('{\n "a": 1,\n "b": [\n 2,\n 3\n ]\n}');
131
+ const result = await runOrDiagnose(['drive', 'ls'], {});
132
+ expect(result.content[0].text).toBe('{"a":1,"b":[2,3]}');
133
+ });
134
+
135
+ // Only FORMATTING whitespace goes. Whitespace inside a value is content —
136
+ // the blank line between paragraphs of a mail body — and JSON.stringify
137
+ // leaves every byte of it alone. A regex over the serialised text would
138
+ // corrupt exactly the payloads this is meant to shrink.
139
+ it('preserves whitespace INSIDE string values', async () => {
140
+ const body = 'Hi,\n\n indented quote\n\nthanks';
141
+ vi.mocked(runner.run).mockResolvedValue(JSON.stringify({ body }, null, 2));
142
+ const result = await runOrDiagnose(['gmail', 'get'], {});
143
+ expect(JSON.parse(result.content[0].text as string).body).toBe(body);
144
+ });
145
+
146
+ // ofw-mcp emits paging state before its data array so a truncated read still
147
+ // sees it; the same reasoning applies to gog's nextPageToken. JSON.stringify
148
+ // preserves insertion order, so minifying must not reorder anything.
149
+ it('preserves key order', async () => {
150
+ vi.mocked(runner.run).mockResolvedValue('{\n "nextPageToken": "t",\n "files": []\n}');
151
+ const result = await runOrDiagnose(['drive', 'ls'], {});
152
+ expect(result.content[0].text).toBe('{"nextPageToken":"t","files":[]}');
153
+ });
154
+
155
+ // gog does not always answer in JSON — `gog auth list` is plain text, and an
156
+ // empty body is legal. Minification must pass anything unparseable through
157
+ // untouched rather than mangling it or throwing.
158
+ it('passes non-JSON output through untouched', async () => {
159
+ for (const text of ['user@gmail.com\nother@gmail.com', '', ' ', 'not json {']) {
160
+ vi.mocked(runner.run).mockResolvedValue(text);
161
+ const result = await runOrDiagnose(['auth', 'list'], {});
162
+ expect(result.content[0].text).toBe(text);
163
+ }
164
+ });
165
+
166
+ // Brace-prefixed but unparseable — a truncated response from a killed gog, or
167
+ // a JSON prelude followed by garbage. It passes the `^[[{]` guard and then
168
+ // fails JSON.parse, and the only safe answer is the original bytes: a caller
169
+ // debugging a malformed payload needs to see what actually arrived.
170
+ //
171
+ // This path IS reachable through other suites today (gog_docs_structure feeds
172
+ // exactly this shape), but incidental coverage is not the same as a pinned
173
+ // behaviour — change that unrelated fixture and this branch goes dark, and
174
+ // the failure surfaces on whatever PR touched the fixture.
175
+ it('passes brace-prefixed but unparseable output through untouched', async () => {
176
+ for (const text of ['{"truncated": ', '[{"a":1},', '{not: json}']) {
177
+ vi.mocked(runner.run).mockResolvedValue(text);
178
+ const result = await runOrDiagnose(['drive', 'ls'], {});
179
+ expect(result.content[0].text).toBe(text);
180
+ expect(result.isError).toBeUndefined();
181
+ }
182
+ });
183
+
184
+ // The lossless dumps are the rung a person reaches for when a payload is not
185
+ // what they expected, and indentation is most of what makes an unfamiliar
186
+ // shape legible — the same asymmetry mcp-utils' viewResult applies to `raw`.
187
+ it('does NOT minify a lossless response', async () => {
188
+ const raw = '{\n "id": "m1"\n}';
189
+ vi.mocked(runner.run).mockResolvedValue(raw);
190
+ const result = await runOrDiagnose(['gmail', 'raw', 'm1'], { lossless: true });
191
+ expect(result.content[0].text).toBe(raw);
192
+ });
193
+
194
+ // A Google field mask is applied UPSTREAM, inside the API, so a mask this
195
+ // wrapper gets wrong is a hard 400 rather than a thin record. That makes the
196
+ // fallback the thing that keeps compact-by-default survivable: the same role
197
+ // mcp-utils' projectOrRaw plays for a projection done locally.
198
+ it('applies a compact field mask when one is given', async () => {
199
+ vi.mocked(runner.run).mockResolvedValue('{"files":[]}');
200
+ await runOrDiagnose(['drive', 'ls'], { fieldsMask: 'nextPageToken,files(id)' });
201
+ expect(runner.run).toHaveBeenCalledWith(
202
+ ['drive', 'ls', '--fields=nextPageToken,files(id)'],
203
+ expect.objectContaining({ fieldsMask: 'nextPageToken,files(id)' }),
204
+ );
205
+ });
206
+
207
+ it('retries UNPROJECTED when Google rejects the mask', async () => {
208
+ vi.mocked(runner.run)
209
+ .mockRejectedValueOnce(new Error('Google API error (400 invalidParameter): Invalid field selection id'))
210
+ .mockResolvedValueOnce('{"files":[{"id":"f1"}]}');
211
+ const result = await runOrDiagnose(['drive', 'ls'], { fieldsMask: 'files(bogus)' });
212
+ expect(runner.run).toHaveBeenNthCalledWith(1, ['drive', 'ls', '--fields=files(bogus)'], expect.anything());
213
+ expect(runner.run).toHaveBeenNthCalledWith(2, ['drive', 'ls'], expect.anything());
214
+ // The caller gets the whole payload, not an error: a projection that trips
215
+ // returns everything rather than taking the tool call down.
216
+ expect(result.isError).toBeUndefined();
217
+ expect(result.content[0].text).toBe('{"files":[{"id":"f1"}]}');
218
+ });
219
+
220
+ // The fallback must not swallow real failures — a missing file is not a bad
221
+ // mask, and retrying it would just spend a second call to fail identically.
222
+ it('does NOT retry an error that is not a rejected mask', async () => {
223
+ vi.mocked(runner.run)
224
+ .mockRejectedValueOnce(new Error('File not found'))
225
+ .mockResolvedValueOnce('user@gmail.com');
226
+ const result = await runOrDiagnose(['drive', 'ls'], { fieldsMask: 'files(id)' });
227
+ expect(result.isError).toBe(true);
228
+ expect(runner.run).not.toHaveBeenNthCalledWith(2, ['drive', 'ls'], expect.anything());
229
+ });
230
+
231
+ // The second compact mechanism. `--fields` is a projection Google performs;
232
+ // this one is performed here, for the tools whose gog subcommand accepts no
233
+ // mask at all. Both surface through the same `view` vocabulary.
234
+ it('strips media keys when asked, and minifies the result', async () => {
235
+ vi.mocked(runner.run).mockResolvedValue(
236
+ '{\n "id": "f1",\n "thumbnailLink": "https://lh3.googleusercontent.com/x=s220",\n "webViewLink": "https://docs.google.com/d/f1"\n}',
237
+ );
238
+ const result = await runOrDiagnose(['drive', 'get', 'f1'], { stripMedia: true });
239
+ expect(result.content[0].text).toBe('{"id":"f1","webViewLink":"https://docs.google.com/d/f1"}');
240
+ });
241
+
242
+ // webViewLink is the one URL a caller acts on and it sits in the same object
243
+ // as thumbnailLink. Stripping it would empty the response of the useful half.
244
+ it('keeps webViewLink and hasThumbnail while stripping the thumbnail', async () => {
245
+ vi.mocked(runner.run).mockResolvedValue(
246
+ JSON.stringify({ files: [{ id: 'f1', hasThumbnail: false, thumbnailLink: 'https://x/y=s220', webViewLink: 'https://docs/f1' }] }),
247
+ );
248
+ const result = await runOrDiagnose(['drive', 'search', 'q'], { stripMedia: true });
249
+ const parsed = JSON.parse(result.content[0].text as string);
250
+ expect(parsed.files[0]).toEqual({ id: 'f1', hasThumbnail: false, webViewLink: 'https://docs/f1' });
251
+ });
252
+
253
+ it('does not strip media unless asked', async () => {
254
+ vi.mocked(runner.run).mockResolvedValue('{"thumbnailLink":"https://x/y=s220"}');
255
+ const result = await runOrDiagnose(['drive', 'get', 'f1'], {});
256
+ expect(result.content[0].text).toBe('{"thumbnailLink":"https://x/y=s220"}');
257
+ });
258
+
259
+ // Non-JSON must survive the strip path exactly as it survives minification —
260
+ // the guard is shared, so a regression here would be silent.
261
+ it('passes non-JSON through untouched even when stripping is on', async () => {
262
+ vi.mocked(runner.run).mockResolvedValue('user@gmail.com');
263
+ const result = await runOrDiagnose(['auth', 'list'], { stripMedia: true });
264
+ expect(result.content[0].text).toBe('user@gmail.com');
265
+ });
266
+
126
267
  it('appends auth list on non-auth failure', async () => {
127
268
  vi.mocked(runner.run)
128
269
  .mockRejectedValueOnce(new Error('Doc not found'))