test_that("valid fields pass and are returned unchanged", { fields = list(fld("manufacturer"), fld("units", type = "number")) checked = validate_fields(fields) expect_true(checked$ok) expect_length(checked$errors, 0) expect_length(checked$notes, 0) expect_equal(checked$fields, fields) }) test_that("no fields and empty labels are errors", { expect_false(validate_fields(list())$ok) checked = validate_fields(list(fld(""), fld(" "))) expect_false(checked$ok) expect_match(checked$errors, "has no label", all = TRUE) expect_length(checked$errors, 2) one_empty = validate_fields(list(fld("a"), fld(""))) expect_false(one_empty$ok) expect_match(one_empty$errors, "Field 2 has no label") }) test_that("duplicate labels are errors, also when they differ only in case or after sanitizing", { expect_false(validate_fields(list(fld("part"), fld("part")))$ok) expect_false(validate_fields(list(fld("Part"), fld("part")))$ok) checked = validate_fields(list(fld("part number"), fld("part_number"))) expect_false(checked$ok) expect_match(paste(checked$errors, collapse = " "), "more than one field") }) test_that("labels reserved for the app's own columns are errors", { for (label in c("source_id", "status", "status_message", "Source_ID", "source id")) { checked = validate_fields(list(fld(label))) expect_false(checked$ok, info = label) expect_match(paste(checked$errors, collapse = " "), "used by the app", info = label) } }) test_that("unsafe characters in labels are replaced and reported", { checked = validate_fields(list(fld("Part Number (OEM)"), fld("2nd-cause"), fld("défaut"))) expect_true(checked$ok) labels = vapply(checked$fields, function(f) f$label, character(1)) expect_equal(labels, c("Part_Number_OEM", "field_2nd_cause", "d_faut")) expect_true(all(grepl("^[A-Za-z_][A-Za-z0-9_]*$", labels))) expect_length(checked$notes, 3) only_punctuation = validate_fields(list(fld("!!!"))) expect_false(only_punctuation$ok) expect_match(only_punctuation$errors, "no letters or digits") }) test_that("sanitize_label trims, truncates and never returns an unsafe name", { expect_equal(sanitize_label(" root cause "), "root_cause") expect_equal(sanitize_label("a\"b\\c"), "a_b_c") expect_equal(sanitize_label("__x__"), "x") expect_equal(nchar(sanitize_label(strrep("a", 200))), MAX_LABEL_CHARS) }) test_that("more fields than the maximum is an error", { too_many = lapply(seq_len(MAX_FIELDS + 1), function(i) fld(paste0("f", i))) checked = validate_fields(too_many) expect_false(checked$ok) expect_match(paste(checked$errors, collapse = " "), paste("maximum is", MAX_FIELDS)) at_limit = lapply(seq_len(MAX_FIELDS), function(i) fld(paste0("f", i))) expect_true(validate_fields(at_limit)$ok) }) test_that("unknown types are errors and empty descriptions are notes", { expect_false(validate_fields(list(fld("a", type = "date")))$ok) checked = validate_fields(list(fld("a", description = ""))) expect_true(checked$ok) expect_match(checked$notes, "no description") }) test_that("clamp_num_fields enforces the range on the server", { expect_equal(clamp_num_fields(3), 3L) expect_equal(clamp_num_fields(0), 1L) expect_equal(clamp_num_fields(-5), 1L) expect_equal(clamp_num_fields(500), as.integer(MAX_FIELDS)) expect_equal(clamp_num_fields(2.6), 3L) expect_true(is.na(clamp_num_fields(NA))) expect_true(is.na(clamp_num_fields(NULL))) expect_true(is.na(clamp_num_fields("abc"))) }) test_that("resize_fields keeps existing fields and adds unused default labels", { fields = list(fld("manufacturer", "who"), fld("field_1", "taken")) grown = resize_fields(fields, 4) expect_length(grown, 4) expect_equal(grown[1:2], fields) labels = vapply(grown, function(f) f$label, character(1)) expect_equal(labels, c("manufacturer", "field_1", "field_3", "field_4")) # When the label for the position is taken, the first free one is used clash = resize_fields(list(fld("field_2")), 2) expect_equal(vapply(clash, function(f) f$label, character(1)), c("field_2", "field_1")) expect_equal(grown[[3]]$type, "text") expect_equal(grown[[3]]$description, "") expect_equal(resize_fields(grown, 1), fields[1]) expect_equal(resize_fields(fields, NA), fields) }) test_that("merge_field_inputs overlays typed values on the stored fields", { fields = list(fld("a", "first"), fld("b", "second")) typed = list(field_label_3_1 = "edited", field_type_3_2 = "number") merged = merge_field_inputs(fields, function(id) typed[[id]], gen = 3) expect_equal(merged[[1]], new_field("edited", "first", "text")) expect_equal(merged[[2]], new_field("b", "second", "number")) # Values typed into an earlier generation of inputs are ignored stale = list(field_label_2_1 = "old") expect_equal(merge_field_inputs(fields, function(id) stale[[id]], gen = 3), fields) }) test_that("each field type maps to the matching ellmer type", { fields = list( fld("t", "text field", "text"), fld("n", "number field", "number"), fld("b", "yes or no field", "yes_no"), fld("l", "list field", "list") ) type = build_type_object(fields) expect_s3_class(type, "ellmer::TypeObject") props = type@properties expect_equal(names(props), c("t", "n", "b", "l")) expect_s3_class(props$t, "ellmer::TypeBasic") expect_equal(props$t@type, "string") expect_equal(props$n@type, "number") expect_equal(props$b@type, "boolean") expect_s3_class(props$l, "ellmer::TypeArray") expect_equal(props$l@items@type, "string") for (label in names(props)) { expect_false(props[[label]]@required, info = label) } expect_equal(props$t@description, "text field") expect_equal(props$l@description, "list field") }) test_that("build_type_object refuses an empty field list and unknown types", { expect_error(build_type_object(list()), "At least one field") expect_error(build_type_object(list(fld("a", type = "date"))), "Unknown field type") }) test_that("field definitions survive a JSON round trip", { fields = list( fld("manufacturer", "Name of the \"company\".\nSecond line with a \\ backslash.", "text"), fld("models", "Modèles concernés; one entry per model.", "list"), fld("units", "", "number"), fld("safety", "True or false.", "yes_no") ) json = fields_to_json(fields) restored = fields_from_json(json) expect_true(restored$ok) expect_equal(restored$fields, fields) path = tempfile(fileext = ".json") writeLines(json, path, useBytes = TRUE) from_file = read_fields_json(path) expect_true(from_file$ok) expect_equal(from_file$fields, fields) }) test_that("a bare array of fields and missing optional keys are accepted", { restored = fields_from_json('[{"label": "a"}, {"label": "b", "type": "number"}]') expect_true(restored$ok) expect_equal(restored$fields, list(new_field("a", "", "text"), new_field("b", "", "number"))) }) test_that("malformed uploads are rejected with a clear message", { bad = list( "not json" = "{this is not json", "empty" = "", "no fields" = '{"format": "x"}', "fields not a list" = '{"fields": "manufacturer"}', "empty fields" = '{"fields": []}', "field not an object" = '{"fields": ["manufacturer"]}', "missing label" = '{"fields": [{"description": "d"}]}', "label not text" = '{"fields": [{"label": 5}]}', "description not text" = '{"fields": [{"label": "a", "description": ["x", "y"]}]}', "unknown type" = '{"fields": [{"label": "a", "type": "date"}]}', "duplicate labels" = '{"fields": [{"label": "a"}, {"label": "a"}]}', "reserved label" = '{"fields": [{"label": "source_id"}]}', "blank label" = '{"fields": [{"label": " "}]}' ) for (case in names(bad)) { result = fields_from_json(bad[[case]]) expect_false(result$ok, info = case) expect_true(nchar(result$error) > 0, info = case) expect_length(result$fields, 0) } too_many = jsonlite::toJSON( list(fields = lapply(seq_len(MAX_FIELDS + 1), function(i) list(label = paste0("f", i)))), auto_unbox = TRUE ) expect_match(fields_from_json(too_many)$error, "maximum") expect_false(read_fields_json(file.path(tempdir(), "does_not_exist.json"))$ok) binary = write_bytes(c(0x89, 0x50, 0x4E, 0x47, 0x00, 0xFF), ".json") expect_false(read_fields_json(binary)$ok) })