File size: 2,046 Bytes
05c5ed5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, expect, it, vi } from "vitest";
import { buildCsvIngestionPreviewParts } from "./csv-ingest";
import type { ChatAttachment } from "app-types/chat";

const attachmentFactory = (
  overrides: Partial<ChatAttachment> = {},
): ChatAttachment => ({
  type: "source-url",
  url: "https://example.com/uploads/data.csv",
  mediaType: "text/csv",
  filename: "data.csv",
  ...overrides,
});

describe("buildCsvIngestionPreviewParts", () => {
  it("returns preview text for csv attachments", async () => {
    const attachments = [attachmentFactory()];
    const download = vi.fn(async () =>
      Buffer.from("col1,col2\n1,2\n3,4\n", "utf8"),
    );

    const parts = await buildCsvIngestionPreviewParts(attachments, download);

    expect(parts).toHaveLength(1);
    expect(parts[0].type).toBe("text");
    expect(parts[0].ingestionPreview).toBe(true);
    expect(parts[0].text).toContain("rows: 3");
    expect(parts[0].text).toContain("| col1 | col2 |");
  });

  it("skips attachments that are not csv-like", async () => {
    const attachments = [
      attachmentFactory({
        mediaType: "application/json",
        filename: "data.json",
      }),
    ];
    const download = vi.fn();

    const parts = await buildCsvIngestionPreviewParts(attachments, download);

    expect(parts).toHaveLength(0);
    expect(download).not.toHaveBeenCalled();
  });

  it("ignores attachments with invalid URLs", async () => {
    const attachments = [attachmentFactory({ url: "not-a-url" })];
    const download = vi.fn();

    const parts = await buildCsvIngestionPreviewParts(attachments, download);

    expect(parts).toHaveLength(0);
    expect(download).not.toHaveBeenCalled();
  });

  it("continues when download fails", async () => {
    const attachments = [attachmentFactory()];
    const download = vi.fn(async () => {
      throw new Error("network");
    });

    const parts = await buildCsvIngestionPreviewParts(attachments, download);

    expect(parts).toHaveLength(0);
    expect(download).toHaveBeenCalled();
  });
});