Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 174 additions & 0 deletions workspace-server/src/__tests__/services/DocsService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,180 @@ describe('DocsService', () => {
index: 1,
});
});

it('should extract text from smart chips (date, person, rich link)', async () => {
const mockDoc = {
data: {
tabs: [
{
documentTab: {
body: {
content: [
{
paragraph: {
elements: [
{
textRun: { content: 'Meeting on ' },
},
{
dateElement: {
dateElementProperties: {
displayText: 'Jan 15, 2025',
timestamp: '1736899200',
},
},
},
{
textRun: { content: ' with ' },
},
{
person: {
personProperties: {
name: 'John Doe',
email: 'john@example.com',
},
},
},
{
textRun: { content: ' - see ' },
},
{
richLink: {
richLinkProperties: {
title: 'Project Plan',
uri: 'https://docs.google.com/document/d/abc123',
},
},
},
{
textRun: { content: '\n' },
},
],
},
},
],
},
},
},
],
},
};
mockDocsAPI.documents.get.mockResolvedValue(mockDoc);

const result = await docsService.getText({ documentId: 'test-doc-id' });

expect(result.content[0].text).toBe(
'Meeting on Jan 15, 2025 with [John Doe](mailto:john@example.com) - see [Project Plan](https://docs.google.com/document/d/abc123)\n',
);
});

it('should fall back to email when person name is not available', async () => {
const mockDoc = {
data: {
tabs: [
{
documentTab: {
body: {
content: [
{
paragraph: {
elements: [
{
person: {
personProperties: {
email: 'jane@example.com',
},
},
},
],
},
},
],
},
},
},
],
},
};
mockDocsAPI.documents.get.mockResolvedValue(mockDoc);

const result = await docsService.getText({ documentId: 'test-doc-id' });

expect(result.content[0].text).toBe('[jane@example.com](mailto:jane@example.com)');
});

it('should render rich link as markdown link', async () => {
const mockDoc = {
data: {
tabs: [
{
documentTab: {
body: {
content: [
{
paragraph: {
elements: [
{
richLink: {
richLinkProperties: {
title: 'Budget Spreadsheet',
uri: 'https://docs.google.com/spreadsheets/d/xyz',
},
},
},
],
},
},
],
},
},
},
],
},
};
mockDocsAPI.documents.get.mockResolvedValue(mockDoc);

const result = await docsService.getText({ documentId: 'test-doc-id' });

expect(result.content[0].text).toBe(
'[Budget Spreadsheet](https://docs.google.com/spreadsheets/d/xyz)',
);
});

it('should fall back to timestamp when date displayText is not available', async () => {
const mockDoc = {
data: {
tabs: [
{
documentTab: {
body: {
content: [
{
paragraph: {
elements: [
{
dateElement: {
dateElementProperties: {
timestamp: '1736899200',
},
},
},
],
},
},
],
},
},
},
],
},
};
mockDocsAPI.documents.get.mockResolvedValue(mockDoc);

const result = await docsService.getText({ documentId: 'test-doc-id' });

expect(result.content[0].text).toBe('1736899200');
});
Comment on lines +576 to +682

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The tests for fallback scenarios are quite similar and contain a lot of boilerplate code. To improve maintainability and reduce duplication, you could use Jest's it.each to parameterize these tests. This would make the test suite more concise and easier to extend with new fallback cases in the future.

    it.each([
      {
        description: 'should fall back to email when person name is not available',
        elements: [
          {
            person: {
              personProperties: {
                email: 'jane@example.com',
              },
            },
          },
        ],
        expectedText: 'jane@example.com',
      },
      {
        description: 'should fall back to uri when rich link title is not available',
        elements: [
          {
            richLink: {
              richLinkProperties: {
                uri: 'https://docs.google.com/spreadsheets/d/xyz',
              },
            },
          },
        ],
        expectedText: 'https://docs.google.com/spreadsheets/d/xyz',
      },
      {
        description: 'should fall back to timestamp when date displayText is not available',
        elements: [
          {
            dateElement: {
              dateElementProperties: {
                timestamp: '1736899200',
              },
            },
          },
        ],
        expectedText: '1736899200',
      },
    ])('$description', async ({ elements, expectedText }) => {
      const mockDoc = {
        data: {
          tabs: [
            {
              documentTab: {
                body: {
                  content: [
                    {
                      paragraph: {
                        elements,
                      },
                    },
                  ],
                },
              },
            },
          ],
        },
      };
      mockDocsAPI.documents.get.mockResolvedValue(mockDoc);

      const result = await docsService.getText({ documentId: 'test-doc-id' });

      expect(result.content[0].text).toBe(expectedText);
    });

});

describe('appendText', () => {
Expand Down
10 changes: 10 additions & 0 deletions workspace-server/src/services/DocsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,16 @@ export class DocsService {
element.paragraph.elements?.forEach((pElement) => {
if (pElement.textRun && pElement.textRun.content) {
text += pElement.textRun.content;
} else if (pElement.person?.personProperties) {
const { name, email } = pElement.person.personProperties;
text += `[${name || email}](mailto:${email})`;
} else if (pElement.richLink?.richLinkProperties) {
const { title, uri } = pElement.richLink.richLinkProperties;
text += `[${title}](${uri})`;
} else if (pElement.dateElement?.dateElementProperties) {
const { displayText, timestamp } =
pElement.dateElement.dateElementProperties;
text += displayText || timestamp || '';
}
Comment on lines 445 to 457

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For improved consistency and robustness, consider using optional chaining (?.) for pElement.textRun and the nullish coalescing operator (??) for fallbacks.

Using pElement.textRun?.content aligns with how other optional properties are checked in this block.

Using ?? instead of || is generally safer as it only falls back for null or undefined, not for other falsy values like an empty string (''). While || works in this specific case, ?? is more explicit about the intended behavior and prevents potential bugs if an empty string becomes a valid, distinct value in the future.

Suggested change
if (pElement.textRun && pElement.textRun.content) {
text += pElement.textRun.content;
} else if (pElement.person?.personProperties) {
const { name, email } = pElement.person.personProperties;
text += name || email || '';
} else if (pElement.richLink?.richLinkProperties) {
const { title, uri } = pElement.richLink.richLinkProperties;
text += title || uri || '';
} else if (pElement.dateElement?.dateElementProperties) {
const { displayText, timestamp } =
pElement.dateElement.dateElementProperties;
text += displayText || timestamp || '';
}
if (pElement.textRun?.content) {
text += pElement.textRun.content;
} else if (pElement.person?.personProperties) {
const { name, email } = pElement.person.personProperties;
text += name ?? email ?? '';
} else if (pElement.richLink?.richLinkProperties) {
const { title, uri } = pElement.richLink.richLinkProperties;
text += title ?? uri ?? '';
} else if (pElement.dateElement?.dateElementProperties) {
const { displayText, timestamp } =
pElement.dateElement.dateElementProperties;
text += displayText ?? timestamp ?? '';
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double check your understanding from here, most of these fields "always exist" according to the client library
https://github.com/googleapis/google-api-nodejs-client/blob/main/src/apis/docs/v1.ts#L2204

});
} else if (element.table) {
Expand Down
Loading