File size: 4,972 Bytes
7dc28be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { afterEach, beforeAll, describe, expect, it } from 'vitest';
import { getDocsClient, getDriveClient } from '../../clients.js';
import * as GDocsHelpers from '../../googleDocsApiHelpers.js';
import { buildInsertTableWithDataRequests } from './insertTableWithData.js';
import { extractTableSnapshot } from './structureHelpers.js';
import { register as registerCloneTable } from './cloneTable.js';
import { hexToRgbColor } from '../../types.js';

const runLive = process.env.GOOGLE_DOCS_LIVE_TESTS === '1';
const liveDescribe = runLive ? describe : describe.skip;

const mockLog = {
  info: () => {},
  warn: () => {},
  error: () => {},
};

let toolExecute: (args: any, context: any) => Promise<string>;

const TABLE_FIELDS =
  'body(content(startIndex,endIndex,table(rows,columns,tableStyle(tableColumnProperties(width,widthType)),tableRows(startIndex,endIndex,tableRowStyle(minRowHeight,preventOverflow,tableHeader),tableCells(startIndex,endIndex,tableCellStyle(backgroundColor,borderTop(color,width,dashStyle),borderBottom(color,width,dashStyle),borderLeft(color,width,dashStyle),borderRight(color,width,dashStyle),contentAlignment,paddingTop,paddingBottom,paddingLeft,paddingRight,rowSpan,columnSpan),content(paragraph(elements(startIndex,endIndex,textRun(content,textStyle(bold))))))))))';

liveDescribe('cloneTable live integration', () => {
  const createdDocumentIds: string[] = [];

  beforeAll(() => {
    const fakeServer = { addTool: (config: any) => (toolExecute = config.execute) };
    registerCloneTable(fakeServer as any);
  });

  afterEach(async () => {
    const drive = await getDriveClient();
    while (createdDocumentIds.length > 0) {
      const fileId = createdDocumentIds.pop()!;
      try {
        await drive.files.delete({ fileId, supportsAllDrives: true });
      } catch {
        // Best-effort cleanup only.
      }
    }
  });

  it('clones a formatted table into a target document and preserves core formatting', async () => {
    const drive = await getDriveClient();
    const docs = await getDocsClient();

    const sourceDoc = await drive.files.create({
      requestBody: {
        name: `cloneTable-source-${Date.now()}`,
        mimeType: 'application/vnd.google-apps.document',
      },
      fields: 'id',
      supportsAllDrives: true,
    });
    const targetDoc = await drive.files.create({
      requestBody: {
        name: `cloneTable-target-${Date.now()}`,
        mimeType: 'application/vnd.google-apps.document',
      },
      fields: 'id',
      supportsAllDrives: true,
    });

    const sourceDocumentId = sourceDoc.data.id!;
    const targetDocumentId = targetDoc.data.id!;
    createdDocumentIds.push(sourceDocumentId, targetDocumentId);

    const sourceData = [
      ['No.', '課題名'],
      ['1', 'SHIN-2870 調査'],
    ];

    await GDocsHelpers.executeBatchUpdateWithSplitting(
      docs,
      sourceDocumentId,
      buildInsertTableWithDataRequests(sourceData, 1, true),
      mockLog
    );

    const sourceTableStart = 2;
    const styleRequests = [
      GDocsHelpers.buildTableColumnWidthRequest(sourceTableStart, [0], 60),
      GDocsHelpers.buildTableColumnWidthRequest(sourceTableStart, [1], 180),
      GDocsHelpers.buildPinTableHeaderRowsRequest(sourceTableStart, 1),
      GDocsHelpers.buildTableRowStyleRequest(sourceTableStart, [0], 24, true),
    ].filter(Boolean);

    const headerBg = hexToRgbColor('#D9E2F3')!;
    const headerCellStyle = GDocsHelpers.buildTableCellStyleRequest(sourceTableStart, 0, 0, {
      rowSpan: 1,
      columnSpan: 2,
      backgroundColor: headerBg,
      contentAlignment: 'CENTER',
      paddingTopPt: 6,
      paddingBottomPt: 6,
    });
    if (headerCellStyle) styleRequests.push(headerCellStyle.request);

    await GDocsHelpers.executeBatchUpdateWithSplitting(
      docs,
      sourceDocumentId,
      styleRequests,
      mockLog
    );

    await toolExecute(
      {
        documentId: targetDocumentId,
        sourceDocumentId,
        sourceTableId: 'table:body:0',
        index: 1,
      },
      { log: mockLog }
    );

    const targetRes = await docs.documents.get({
      documentId: targetDocumentId,
      fields: TABLE_FIELDS,
    });

    const snapshot = extractTableSnapshot(targetRes.data, 'table:body:0');
    expect(snapshot).not.toBeNull();
    expect(snapshot!.data).toEqual(sourceData);
    expect(snapshot!.pinnedHeaderRowsCount).toBe(1);
    expect(snapshot!.columnStyles).toEqual([
      { columnIndex: 0, widthPt: 60, widthType: 'FIXED_WIDTH' },
      { columnIndex: 1, widthPt: 180, widthType: 'FIXED_WIDTH' },
    ]);
    expect(snapshot!.rowStyles[0]).toMatchObject({
      rowIndex: 0,
      minRowHeightPt: 24,
      preventOverflow: true,
      tableHeader: true,
    });
    expect(snapshot!.cellStyles[0]).toMatchObject({
      rowIndex: 0,
      columnIndex: 0,
      contentAlignment: 'CENTER',
      paddingTopPt: 6,
      paddingBottomPt: 6,
      hasBoldText: true,
    });
  }, 120000);
});