Spaces:
Running
Running
File size: 5,686 Bytes
5f923cd | 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 146 147 148 149 | // Copyright 2025 The ODML Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "runtime/conversation/model_data_processor/data_utils.h"
#include <filesystem> // NOLINT
#include <fstream>
#include <ios>
#include <memory>
#include <string>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "absl/status/status.h" // from @com_google_absl
#include "absl/strings/string_view.h" // from @com_google_absl
#include "nlohmann/json.hpp" // from @nlohmann_json
#include "runtime/util/memory_mapped_file.h"
#include "runtime/util/test_utils.h" // NOLINT
namespace litert::lm {
namespace {
using ::nlohmann::ordered_json;
void WriteFile(absl::string_view path, absl::string_view contents) {
std::ofstream ofstr(std::string(path), std::ios::out);
ofstr << contents;
}
TEST(DataUtilsTest, LoadItemData_TextItem) {
ASSERT_OK_AND_ASSIGN(std::unique_ptr<MemoryMappedFile> memory_mapped_file,
LoadItemData({
{"type", "text"},
{"text", "some text"},
}));
EXPECT_EQ(std::string(static_cast<const char*>(memory_mapped_file->data()),
memory_mapped_file->length()),
"some text");
}
TEST(DataUtilsTest, LoadItemData_ImageItemWithPath) {
auto path = std::filesystem::path(::testing::TempDir()) / "test_image.jpg";
WriteFile(path.string(), "image_contents");
ASSERT_OK_AND_ASSIGN(std::unique_ptr<MemoryMappedFile> memory_mapped_file,
LoadItemData({
{"type", "image"},
{"path", path.string()},
}));
EXPECT_EQ(std::string(static_cast<const char*>(memory_mapped_file->data()),
memory_mapped_file->length()),
"image_contents");
}
TEST(DataUtilsTest, LoadItemData_ImageItemWithBlob) {
ASSERT_OK_AND_ASSIGN(std::unique_ptr<MemoryMappedFile> memory_mapped_file,
LoadItemData({
{"type", "image"},
{"blob", "aW1hZ2VfY29udGVudHM="},
}));
EXPECT_EQ(std::string(static_cast<const char*>(memory_mapped_file->data()),
memory_mapped_file->length()),
"image_contents");
}
TEST(DataUtilsTest, LoadItemData_AudioItemWithPath) {
auto path = std::filesystem::path(::testing::TempDir()) / "test_audio.wav";
WriteFile(path.string(), "audio_contents");
ASSERT_OK_AND_ASSIGN(std::unique_ptr<MemoryMappedFile> memory_mapped_file,
LoadItemData({
{"type", "audio"},
{"path", path.string()},
}));
EXPECT_EQ(std::string(static_cast<const char*>(memory_mapped_file->data()),
memory_mapped_file->length()),
"audio_contents");
}
TEST(DataUtilsTest, LoadItemData_AudioItemWithBlob) {
ASSERT_OK_AND_ASSIGN(std::unique_ptr<MemoryMappedFile> memory_mapped_file,
LoadItemData({
{"type", "audio"},
{"blob", "YXVkaW9fY29udGVudHM="},
}));
EXPECT_EQ(std::string(static_cast<const char*>(memory_mapped_file->data()),
memory_mapped_file->length()),
"audio_contents");
}
TEST(DataUtilsTest, LoadItemData_UnsupportedItemType) {
auto status = LoadItemData({
{"type", "unsupported_type"},
})
.status();
EXPECT_THAT(status,
testing::status::StatusIs(absl::StatusCode::kUnimplemented));
EXPECT_THAT(status.message(),
testing::HasSubstr("Unsupported item type: unsupported_type"));
}
TEST(DataUtilsTest, LoadItemData_InvalidItem) {
auto status = LoadItemData({
{"type", "image"},
})
.status();
EXPECT_THAT(status,
testing::status::StatusIs(absl::StatusCode::kInvalidArgument));
EXPECT_THAT(
status.message(),
testing::HasSubstr("Audio or image item must contain a path or blob."));
}
TEST(DataUtilsTest, LoadItemData_ImageItemWithInvalidBase64Blob) {
auto result = LoadItemData({
{"type", "image"},
{"blob", "invalid base64"},
});
EXPECT_THAT(result.status(),
testing::status::StatusIs(absl::StatusCode::kInvalidArgument));
EXPECT_THAT(result.status().message(),
testing::HasSubstr("Failed to decode base64 blob."));
}
TEST(DataUtilsTest, LoadItemData_ToolResponseItem) {
ASSERT_OK_AND_ASSIGN(std::unique_ptr<MemoryMappedFile> memory_mapped_file,
LoadItemData({{"type", "tool_response"},
{"tool_response", "some response"}}));
EXPECT_EQ(memory_mapped_file, nullptr);
}
TEST(DataUtilsTest, LoadItemData_MissingType) {
ASSERT_OK_AND_ASSIGN(std::unique_ptr<MemoryMappedFile> memory_mapped_file,
LoadItemData({{"not_type", "some value"}}));
EXPECT_EQ(memory_mapped_file, nullptr);
}
} // namespace
} // namespace litert::lm
|