Skip to content

Commit 0793a5f

Browse files
fix(docs): include document title in getText and getSuggestions (#309)
* fix(docs): include document title in getText and getSuggestions responses docs.getText and docs.getSuggestions were not returning the document title, unlike slides.getText and sheets.getText which already did. - getText: prepends "Document Title: ..." for single-tab plain text, wraps multi-tab JSON in { title, tabs } object - getSuggestions: returns { title, suggestions } instead of bare array * test: add title assertions to getText and getSuggestions tests Add mock title data and explicit assertions verifying the document title is present in responses, not just the tab/suggestion content.
1 parent 48ebe1e commit 0793a5f

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed

workspace-server/src/__tests__/services/DocsService.comments.test.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ describe('DocsService Comments and Suggestions', () => {
5858
it('should return suggestions as type text with JSON-stringified array', async () => {
5959
mockDocsAPI.documents.get.mockResolvedValue({
6060
data: {
61+
title: 'Test Document',
6162
body: {
6263
content: [
6364
{
@@ -84,7 +85,9 @@ describe('DocsService Comments and Suggestions', () => {
8485
});
8586

8687
expect(result.content[0].type).toBe('text');
87-
const suggestions = JSON.parse(result.content[0].text);
88+
const parsed = JSON.parse(result.content[0].text);
89+
expect(parsed.title).toBe('Test Document');
90+
const { suggestions } = parsed;
8891
expect(suggestions).toHaveLength(1);
8992
expect(suggestions[0]).toEqual({
9093
type: 'insertion',
@@ -123,7 +126,7 @@ describe('DocsService Comments and Suggestions', () => {
123126
documentId: 'test-doc-id',
124127
});
125128

126-
const suggestions = JSON.parse(result.content[0].text);
129+
const { suggestions } = JSON.parse(result.content[0].text);
127130
expect(suggestions).toHaveLength(1);
128131
expect(suggestions[0].type).toBe('insertion');
129132
expect(suggestions[0].suggestionIds).toEqual(['sug-1', 'sug-2']);
@@ -157,7 +160,7 @@ describe('DocsService Comments and Suggestions', () => {
157160
documentId: 'test-doc-id',
158161
});
159162

160-
const suggestions = JSON.parse(result.content[0].text);
163+
const { suggestions } = JSON.parse(result.content[0].text);
161164
expect(suggestions).toHaveLength(1);
162165
expect(suggestions[0].type).toBe('deletion');
163166
expect(suggestions[0].text).toBe('deleted text');
@@ -194,7 +197,7 @@ describe('DocsService Comments and Suggestions', () => {
194197
documentId: 'test-doc-id',
195198
});
196199

197-
const suggestions = JSON.parse(result.content[0].text);
200+
const { suggestions } = JSON.parse(result.content[0].text);
198201
expect(suggestions).toHaveLength(1);
199202
expect(suggestions[0].type).toBe('styleChange');
200203
expect(suggestions[0].suggestionIds).toEqual(['style-1']);
@@ -234,7 +237,7 @@ describe('DocsService Comments and Suggestions', () => {
234237
documentId: 'test-doc-id',
235238
});
236239

237-
const suggestions = JSON.parse(result.content[0].text);
240+
const { suggestions } = JSON.parse(result.content[0].text);
238241
expect(suggestions).toHaveLength(1);
239242
expect(suggestions[0].type).toBe('paragraphStyleChange');
240243
expect(suggestions[0].suggestionIds).toEqual(['sug-para-1']);
@@ -284,7 +287,7 @@ describe('DocsService Comments and Suggestions', () => {
284287
documentId: 'test-doc-id',
285288
});
286289

287-
const suggestions = JSON.parse(result.content[0].text);
290+
const { suggestions } = JSON.parse(result.content[0].text);
288291
expect(suggestions).toHaveLength(1);
289292
expect(suggestions[0].type).toBe('insertion');
290293
expect(suggestions[0].text).toBe('cell text');
@@ -299,8 +302,8 @@ describe('DocsService Comments and Suggestions', () => {
299302
documentId: 'test-doc-id',
300303
});
301304

302-
const suggestions = JSON.parse(result.content[0].text);
303-
expect(suggestions).toEqual([]);
305+
const parsed = JSON.parse(result.content[0].text);
306+
expect(parsed.suggestions).toEqual([]);
304307
});
305308

306309
it('should handle API errors gracefully', async () => {
@@ -351,7 +354,7 @@ describe('DocsService Comments and Suggestions', () => {
351354
documentId: 'test-doc-id',
352355
});
353356

354-
const suggestions = JSON.parse(result.content[0].text);
357+
const { suggestions } = JSON.parse(result.content[0].text);
355358
expect(suggestions).toHaveLength(2);
356359
const types = suggestions.map((s: any) => s.type);
357360
expect(types).toEqual(['paragraphStyleChange', 'paragraphStyleChange']);
@@ -391,7 +394,7 @@ describe('DocsService Comments and Suggestions', () => {
391394
documentId: 'test-doc-id',
392395
});
393396

394-
const suggestions = JSON.parse(result.content[0].text);
397+
const { suggestions } = JSON.parse(result.content[0].text);
395398
expect(suggestions).toHaveLength(1);
396399
expect(suggestions[0].text).toBe('');
397400
});

workspace-server/src/__tests__/services/DocsService.test.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ describe('DocsService', () => {
513513
it('should return all tabs if no tabId provided and tabs exist', async () => {
514514
const mockDoc = {
515515
data: {
516+
title: 'Multi-Tab Document',
516517
tabs: [
517518
{
518519
tabProperties: { tabId: 'tab-1', title: 'Tab 1' },
@@ -550,14 +551,15 @@ describe('DocsService', () => {
550551
const result = await docsService.getText({ documentId: 'test-doc-id' });
551552
const parsed = JSON.parse(result.content[0].text);
552553

553-
expect(parsed).toHaveLength(2);
554-
expect(parsed[0]).toEqual({
554+
expect(parsed.title).toBe('Multi-Tab Document');
555+
expect(parsed.tabs).toHaveLength(2);
556+
expect(parsed.tabs[0]).toEqual({
555557
tabId: 'tab-1',
556558
title: 'Tab 1',
557559
content: 'Tab 1 Content',
558560
index: 0,
559561
});
560-
expect(parsed[1]).toEqual({
562+
expect(parsed.tabs[1]).toEqual({
561563
tabId: 'tab-2',
562564
title: 'Tab 2',
563565
content: 'Tab 2 Content',
@@ -695,6 +697,7 @@ describe('DocsService', () => {
695697
it('should include text from nested child tabs', async () => {
696698
const mockDoc = {
697699
data: {
700+
title: 'Nested Tabs Doc',
698701
tabs: [
699702
{
700703
tabProperties: { tabId: 'parent-tab', title: 'Parent' },
@@ -736,14 +739,15 @@ describe('DocsService', () => {
736739
const result = await docsService.getText({ documentId: 'test-doc-id' });
737740
const parsed = JSON.parse(result.content[0].text);
738741

739-
expect(parsed).toHaveLength(2);
740-
expect(parsed[0]).toEqual({
742+
expect(parsed.title).toBe('Nested Tabs Doc');
743+
expect(parsed.tabs).toHaveLength(2);
744+
expect(parsed.tabs[0]).toEqual({
741745
tabId: 'parent-tab',
742746
title: 'Parent',
743747
content: 'Parent Content',
744748
index: 0,
745749
});
746-
expect(parsed[1]).toEqual({
750+
expect(parsed.tabs[1]).toEqual({
747751
tabId: 'child-tab',
748752
title: 'Child',
749753
content: 'Child Content',

workspace-server/src/services/DocsService.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class DocsService {
7575
const res = await docs.documents.get({
7676
documentId: id,
7777
suggestionsViewMode: 'SUGGESTIONS_INLINE',
78-
fields: 'body',
78+
fields: 'title,body',
7979
});
8080

8181
const suggestions: DocsSuggestion[] = this._extractSuggestions(
@@ -90,7 +90,11 @@ export class DocsService {
9090
content: [
9191
{
9292
type: 'text' as const,
93-
text: JSON.stringify(suggestions, null, 2),
93+
text: JSON.stringify(
94+
{ title: res.data.title, suggestions },
95+
null,
96+
2,
97+
),
9498
},
9599
],
96100
};
@@ -539,10 +543,11 @@ export class DocsService {
539543
const docs = await this.getDocsClient();
540544
const res = await docs.documents.get({
541545
documentId: id,
542-
fields: 'tabs', // Request tabs only (body is legacy and mutually exclusive with tabs in mask)
546+
fields: 'title,tabs', // Request title and tabs (body is legacy and mutually exclusive with tabs in mask)
543547
includeTabsContent: true,
544548
});
545549

550+
const docTitle = res.data.title;
546551
const tabs = this._flattenTabs(res.data.tabs || []);
547552

548553
// If tabId is provided, try to find it
@@ -565,6 +570,9 @@ export class DocsService {
565570
}
566571

567572
let text = '';
573+
if (docTitle) {
574+
text += `Document Title: ${docTitle}\n\n`;
575+
}
568576
content.forEach((element) => {
569577
text += this._readStructuralElement(element);
570578
});
@@ -595,6 +603,9 @@ export class DocsService {
595603
if (tabs.length === 1) {
596604
const tab = tabs[0];
597605
let text = '';
606+
if (docTitle) {
607+
text += `Document Title: ${docTitle}\n\n`;
608+
}
598609
if (tab.documentTab?.body?.content) {
599610
tab.documentTab.body.content.forEach((element) => {
600611
text += this._readStructuralElement(element);
@@ -630,7 +641,7 @@ export class DocsService {
630641
content: [
631642
{
632643
type: 'text' as const,
633-
text: JSON.stringify(tabsData, null, 2),
644+
text: JSON.stringify({ title: docTitle, tabs: tabsData }, null, 2),
634645
},
635646
],
636647
};

0 commit comments

Comments
 (0)