From ae7597a57d70b51fa2f66a1afd6464a2375f1d42 Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Mon, 18 Nov 2024 09:13:37 -0800 Subject: [PATCH] genai: test invalid text file encoding (#224) For #221 --- genai/client_test.go | 31 +++++++++++++++++++++++++++++-- genai/testdata/badencoding.txt | Bin 0 -> 1024 bytes 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 genai/testdata/badencoding.txt diff --git a/genai/client_test.go b/genai/client_test.go index 7172f9a..0717a03 100644 --- a/genai/client_test.go +++ b/genai/client_test.go @@ -465,7 +465,8 @@ func TestLive(t *testing.T) { // Use the uploaded file to generate content. model := client.GenerativeModel("gemini-1.5-pro-latest") - resp, err := model.GenerateContent(ctx, FileData{URI: file.URI}) + resp, err := model.GenerateContent(ctx, + Text("describe this image"), FileData{URI: file.URI}) if err != nil { t.Fatal(err) } @@ -476,7 +477,8 @@ func TestLive(t *testing.T) { if got, want := file.MIMEType, "audio/mpeg"; got != want { t.Errorf("got MIME type %q, want %q", got, want) } - resp, err = model.GenerateContent(ctx, FileData{URI: file.URI}) + resp, err = model.GenerateContent(ctx, + Text("describe this"), FileData{URI: file.URI}) if err != nil { t.Fatal(err) } @@ -502,6 +504,31 @@ func TestLive(t *testing.T) { // TODO(jba): verify metadata when it is populated. t.Logf("Metadata: %+v\n", file.Metadata) }) + + t.Run("txt-encoding-valid", func(t *testing.T) { + // Upload a file that has valid UTF-8 encoding (ASCII). + poem := uploadFile(t, ctx, client, filepath.Join("testdata", "poem.txt")) + model := client.GenerativeModel("gemini-1.5-flash") + _, err := model.GenerateContent(ctx, + Text("summarize this"), + FileData{URI: poem.URI}) + if err != nil { + t.Error(err) + } + }) + + t.Run("txt-encoding-invalid", func(t *testing.T) { + // Try uploading a "text" file with garbled encoding (this is just random + // data). + f := uploadFile(t, ctx, client, filepath.Join("testdata", "badencoding.txt")) + model := client.GenerativeModel("gemini-1.5-flash") + _, err := model.GenerateContent(ctx, + Text("summarize this"), + FileData{URI: f.URI}) + if err == nil { + t.Errorf("want encoding error") + } + }) }) t.Run("JSON", func(t *testing.T) { diff --git a/genai/testdata/badencoding.txt b/genai/testdata/badencoding.txt new file mode 100644 index 0000000000000000000000000000000000000000..061f29fc6681d5a597fabfca1a592048714ef731 GIT binary patch literal 1024 zcmV+b1poW5@y|phs~LSb2mEWOUw=2ohX_6!|FinU< z3+y*JfWL4c!d+?*lYwcFg)=>>^Hl*I%d^t;CJmXcqB!k8(s)JW6=Oi>oD^oTqH%u3 z#Gg7HT7sx?k#ofkMtc3NSh1e}ff8$1mla@$Q@!v$h63k@3&Bh%A~znM+c~@kCb%Cj z2%q+pY~TvvtAi0MXL86t406Ubm$;W_lQL)C?UJ)p?w)TQk&HOXxxksL5-tb_%8*NV zR&Pz&8u&Y3d>S!Pz`N1gby`a?;m1AJ`0*M?I=WP!r@^#cyM5GK+%p4?%_a`zu_{WO zItNA63quJwV6YqhPRGjU-QVvUM>ae+Ax)yq5+;@pWe=a+x_*(#*V)E@cnxj19?vuo z%?^-6EdHmsAZw?V4?rS_$2-E1bQIRoat(YBSRVwWF&q^NG1j^|{HWmgqED`0XgI1- z=U0h2kFRb>5Vv5UIMxoU_Cgh{fX;e*&^n#hV7`^M1cc8Fh89T2G@n?zn6D6DEYU0g z&W_J>z8=ScvS*W1-|Z>Q_p&9`Il@ZymFd^3uE)U0p8tP483Ws#XWvt`f$eFz9tqI! zm=-#~p~~VO0t?Y{hALf>2oEED-gq$hw|Bvgw=N>uG0NrcPcmh84!CwQ1x&JT&bDmW2|I6pV8(`=yaJw3pJ712LZgW%+dK5qjvX2RXt9M#U zYXZdJtU9NN=mHBLXava1wOH3vmQOBx1<25rYS~Soxe(N=dI*k57c8lV6M-=G$5X-M z74QVZ3$d^B1%E#WW^vTUdo6%)H#U~)sFa<9JW{Z|C5ri-#r5%#JL96KsOtWX`8|wI zH8t8U((MMN1>Zvb?9@?mL$7|*vl9(VbJE@+vYJU28xw_{i*gk;kZ|OPC1bASs)P7y u$~e*`SV1;10O{&)Z#1vTB4Ty4of%l2ac literal 0 HcmV?d00001