Skip to content

test: add unit tests for key server/models validation types#230

Merged
asdek merged 3 commits intovxcontrol:mainfrom
mason5052:test/server-models-validation
Apr 7, 2026
Merged

test: add unit tests for key server/models validation types#230
asdek merged 3 commits intovxcontrol:mainfrom
mason5052:test/server-models-validation

Conversation

@mason5052
Copy link
Copy Markdown
Contributor

@mason5052 mason5052 commented Apr 1, 2026

What

Add unit tests for validation logic across key server/models types: users, providers, API tokens, flows, tasks, containers, assistants, prompts, roles, and custom validators.

This does not cover every exported validator in the package (log models, analytics, msgchains, screenshots, and vecstore models are not included).

Why

These model types contain security-sensitive validation functions (password strength, OAuth scope, JWT claims, API token TTL bounds) and core domain validation (enum status types, struct field requirements, nested model validation). None had test coverage.

Test Files

File Tested Types
users_test.go UserStatus, UserType, Login, Password, User, UserPassword, AuthCallback, UserRole, UserRolePrivileges, UserPreferences
providers_test.go ProviderType (10 types), Provider, CreateProvider, PatchProvider, ProviderInfo
api_tokens_test.go TokenStatus, APIToken, APITokenWithSecret, CreateAPITokenRequest, UpdateAPITokenRequest, APITokenClaims
flows_test.go FlowStatus, TaskStatus, SubtaskStatus, ContainerStatus, ContainerType, Flow, FlowTasksSubtasks, FlowContainers, Task, TaskSubtasks, Subtask, Container, Role, Privilege, RolePrivileges, PatchFlow
init_test.go Custom validators (stpass, vmail, oauth_min_scope, solid, semver, semverex), scanFromJSON
assistants_test.go AssistantStatus, Assistant, CreateAssistant, PatchAssistant, AssistantFlow
prompts_test.go PromptType, Prompt, PatchPrompt

Test Approach

  • All tests use t.Parallel() for fast execution
  • Table-driven tests for enum validation with valid and invalid cases
  • Boundary testing for numeric constraints (TTL min=60, max=94608000)
  • Cross-field validation (Password confirm mismatch, current != new)
  • Nested struct validation (AssistantFlow, FlowTasksSubtasks, FlowContainers, TaskSubtasks, UserRolePrivileges)
  • Same-package testing for access to unexported validate and scanFromJSON

Verification

cd backend && go test ./pkg/server/models/ -count=1 -v
# 347 test cases pass

Add comprehensive test coverage for the server/models package validation
logic including enum types, struct validators, and custom validators.

Test files added:
- users_test.go: UserStatus, UserType, Login, Password, User,
  UserPassword, AuthCallback, UserRole, UserPreferences validation
- providers_test.go: ProviderType, Provider, CreateProvider,
  PatchProvider, ProviderInfo validation
- api_tokens_test.go: TokenStatus, APIToken, CreateAPITokenRequest,
  UpdateAPITokenRequest, APITokenClaims validation
- flows_test.go: FlowStatus, TaskStatus, SubtaskStatus, ContainerStatus,
  ContainerType, Role, Privilege, RolePrivileges, PatchFlow validation
- init_test.go: Custom validators (stpass, vmail, oauth_min_scope,
  solid, semver, semverex) and scanFromJSON helper
Copilot AI review requested due to automatic review settings April 1, 2026 00:38
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds unit test coverage for backend/pkg/server/models validation logic to ensure core model validators (users, providers, flows, API tokens, and custom validators) are exercised and regressions are caught early.

Changes:

  • Introduces table-driven unit tests for enum/string validators and Valid() methods across key models.
  • Adds boundary/negative-case tests for security-sensitive validation rules (password strength, OAuth scopes, JWT-related claims).
  • Adds tests for helper/validator initialization and JSON scan helpers.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 19 comments.

Show a summary per file
File Description
backend/pkg/server/models/users_test.go Adds tests for user/login/password models, role linking, and preferences JSON scan/value behavior.
backend/pkg/server/models/providers_test.go Adds tests for provider type validation and provider create/patch/info payload validation.
backend/pkg/server/models/api_tokens_test.go Adds tests for API token status/struct validation, TTL boundaries, and claims validation.
backend/pkg/server/models/flows_test.go Adds tests for flow/task/subtask/container status/type validation and patch action validation.
backend/pkg/server/models/init_test.go Adds tests for custom validators (stpass, vmail, oauth_min_scope, solid, semver, semverex) and scanFromJSON.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread backend/pkg/server/models/users_test.go
Comment thread backend/pkg/server/models/users_test.go
Comment thread backend/pkg/server/models/users_test.go
Comment thread backend/pkg/server/models/users_test.go
Comment thread backend/pkg/server/models/users_test.go Outdated
Comment on lines +401 to +402
assert.Error(t, up.Valid())
assert.Contains(t, up.Valid().Error(), "user_id is required")
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion calls up.Valid() twice; the second call is unnecessary and can hide issues if Valid() becomes stateful. Store the error once (e.g., err := up.Valid()) and assert on err/err.Error().

Suggested change
assert.Error(t, up.Valid())
assert.Contains(t, up.Valid().Error(), "user_id is required")
err := up.Valid()
assert.Error(t, err)
assert.Contains(t, err.Error(), "user_id is required")

Copilot uses AI. Check for mistakes.
Comment thread backend/pkg/server/models/flows_test.go
Comment thread backend/pkg/server/models/flows_test.go
Comment thread backend/pkg/server/models/flows_test.go
Comment thread backend/pkg/server/models/flows_test.go
Comment thread backend/pkg/server/models/api_tokens_test.go
Add missing test coverage identified by cross-review:

New files:
- assistants_test.go: AssistantStatus enum, Assistant struct, CreateAssistant,
  PatchAssistant (stop/input actions), AssistantFlow nested validation
- prompts_test.go: PromptType enum (8 valid constants from templates pkg),
  Prompt struct, PatchPrompt validation

Extended existing files:
- users_test.go: add AuthCallback positive-path test, UserRolePrivileges
  validation, fix brittle double-call assertion in UserPreferences test
- api_tokens_test.go: add APITokenWithSecret validation (valid, invalid
  embedded token, invalid JWT)
- flows_test.go: add Flow.Valid, FlowTasksSubtasks.Valid,
  FlowContainers.Valid, Task.Valid, TaskSubtasks.Valid, Subtask.Valid,
  Container.Valid with valid/invalid/missing-field cases
@mason5052 mason5052 changed the title test: add unit tests for server/models validation functions test: add unit tests for key server/models validation types Apr 1, 2026
@asdek
Copy link
Copy Markdown
Contributor

asdek commented Apr 2, 2026

hey @mason5052

there are correct notice from Copilot side, you need to change these syntax constructions to make immutable tt variable in the loops.

Add tt := tt inside each for-range loop before t.Run to ensure
parallel subtests capture their own immutable copy of the test case.
@mason5052
Copy link
Copy Markdown
Contributor Author

Fixed -- added tt := tt inside every for _, tt := range loop across all 7 test files. All tests still pass (go test ./pkg/server/models/... ok). Thanks for the Copilot catch!

Copy link
Copy Markdown
Contributor

@asdek asdek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for PR and fixes!

@asdek asdek merged commit 51cb312 into vxcontrol:main Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants