//go:build integration

package server

import (
	"context"
	"encoding/base64"
	"log"
	"os"
	"strings"
	"testing"
	"time"

	"github.com/jmorganca/ollama/api"
	"github.com/jmorganca/ollama/llm"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestIntegrationMultimodal(t *testing.T) {
	SkipIFNoTestData(t)
	image, err := base64.StdEncoding.DecodeString(imageEncoding)
	require.NoError(t, err)
	req := api.GenerateRequest{
		Model:   "llava:7b",
		Prompt:  "what does the text in this image say?",
		Options: map[string]interface{}{},
		Images: []api.ImageData{
			image,
		},
	}
	resp := "the ollamas"
	workDir, err := os.MkdirTemp("", "ollama")
	require.NoError(t, err)
	defer os.RemoveAll(workDir)
	require.NoError(t, llm.Init(workDir))
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
	defer cancel()
	opts := api.DefaultOptions()
	opts.Seed = 42
	opts.Temperature = 0.0
	model, llmRunner := PrepareModelForPrompts(t, req.Model, opts)
	defer llmRunner.Close()
	response := OneShotPromptResponse(t, ctx, req, model, llmRunner)
	log.Print(response)
	assert.Contains(t, strings.ToLower(response), resp)
}

const imageEncoding = `iVBORw0KGgoAAAANSUhEUgAAANIAAAB4CAYAAACHHqzKAAAAAXNSR0IArs4c6QAAAIRlWElmTU0AKgAAAAgABQESAAMAAAABAAEAAAEaAAUAAAABAAAASgEb
[encoded data, 59774 characters]
voQTefWxZ9CMIP9/0Wj1ClQSO/4AAAAASUVORK5CYII=`
