Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions pkg/types/nutrition_writable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2026 Google LLC
//
// 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.

package types

import "testing"

func has(ops []string, op string) bool {
for _, o := range ops {
if o == op {
return true
}
}
return false
}

// nutrition-log and hydration-log accept create and batchDelete (verified
// against the live v4 API), but PATCH on either returns 500 INTERNAL — with a
// field mask and without one. They are therefore create/delete-only: "update"
// must stay absent so the CLI does not expose a command that always fails.
func TestNutritionLogsAreCreateDeleteOnly(t *testing.T) {
for _, id := range []string{"nutrition-log", "hydration-log"} {
dt := Get(id)
if dt == nil {
t.Fatalf("%s: not registered", id)
}
if !dt.Writable {
t.Errorf("%s: Writable = false, want true", id)
}
for _, op := range []string{"create", "delete"} {
if !has(dt.Operations, op) {
t.Errorf("%s: missing %q in Operations %v", id, op, dt.Operations)
}
}
if has(dt.Operations, "update") {
t.Errorf("%s: has \"update\", but PATCH returns 500 INTERNAL on this type", id)
}
}
}

// The food catalog is reference data: read-only for API clients. A custom food
// is expressed as an anonymous nutrition-log entry (inline nutrients), not as a
// new catalog entry.
func TestFoodCatalogStaysReadOnly(t *testing.T) {
for _, id := range []string{"food", "food-measurement-unit"} {
dt := Get(id)
if dt == nil {
t.Fatalf("%s: not registered", id)
}
if dt.Writable {
t.Errorf("%s: Writable = true, want false (catalog is read-only)", id)
}
for _, op := range []string{"create", "update", "delete"} {
if has(dt.Operations, op) {
t.Errorf("%s: unexpected %q in Operations %v", id, op, dt.Operations)
}
}
}
}
10 changes: 8 additions & 2 deletions pkg/types/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,9 @@ func init() {
ID: "hydration-log", FilterName: "hydration_log", TimeField: interval,
Category: "nutrition",
Description: "Hydration log entries",
Operations: []string{"list", "get", "daily-rollup", "reconcile"},
// create/delete only, same as nutrition-log: PATCH returns 500 INTERNAL.
Operations: []string{"list", "get", "create", "delete", "daily-rollup", "reconcile"},
Writable: true,
})

// --- Rollup-only types (no list) ---
Expand Down Expand Up @@ -509,7 +511,11 @@ func init() {
ID: "nutrition-log", FilterName: "nutrition_log", TimeField: interval,
Category: "nutrition",
Description: "Logged food/nutrition entries with nutrient and energy breakdown",
Operations: []string{"list", "get", "rollup", "daily-rollup", "reconcile"},
// create/delete only: PATCH on an existing nutrition-log point returns
// 500 INTERNAL (with or without a field mask), so "update" is omitted
// deliberately — edit by delete + create.
Operations: []string{"list", "get", "create", "delete", "rollup", "daily-rollup", "reconcile"},
Writable: true,
})
register(&DataType{
ID: "food", FilterName: "food", TimeField: TimeFieldNone,
Expand Down