Files
copilotkit__copilotkit/showcase/bin/spec/test_snapshot_graphql.rb
Jordan Ritter 8114eda52c feat(showcase): extend snapshot schema (v2) with healthcheck/region/replicas/restartPolicy + add P6 parity matrix
Adds the P6 staging/prod parity matrix to promote:
- REFUSE on startCommand, healthcheckPath, or image-shape divergence
  (staging is expected :tag/mutable, prod is expected :digest/pinned).
- WARN on region, replicas, restartPolicy, or env-var KEY-set divergence.
  WARN findings refuse the promote unless --confirm-divergence is set,
  at which point they print "[--confirm-divergence set] proceeding past
  N WARN finding(s)" and continue.
- Env var VALUES are never compared (staging/prod hold different
  secrets/URLs by design); the NOTE wired in commit #2's
  run_with_preflight_only is preserved.

Snapshot schema bumped to v2:
- SERVICE_INSTANCE_QUERY adds healthcheckPath, region, numReplicas,
  restartPolicyType.
- SnapshotCommand#build_snapshot maps them onto healthcheck_path,
  region, replicas, restart_policy in the snapshot hash.
- SnapshotIO.SCHEMA_VERSION = 2 with SUPPORTED_VERSIONS = [1, 2] so
  rollback-commit can still replay v1 snapshots from historical SHAs.

PromoteCommand.image_shape classifies a ref as :digest / :tag /
:missing / :other.

New tests: 6 P6 cases (startCommand REFUSE, healthcheckPath REFUSE,
image-shape REFUSE, WARN-without-confirm refusal, WARN-with-confirm
proceed, every-run NOTE). Two new snapshot tests: v2 captures new
fields end-to-end, and SnapshotIO.read accepts both v1 and v2.
2026-05-29 11:45:11 -07:00

249 lines
11 KiB
Ruby

# frozen_string_literal: true
require_relative "spec_helper"
# Verifies the GraphQL queries used by SnapshotCommand match Railway's
# public schema shape (as of 2026-05). The mocks here mirror real Railway
# responses; any drift between this file and the live schema means the
# tool will fail at runtime — which is exactly the bug this PR fixes.
class SnapshotGraphqlTest < Minitest::Test
# Fake GraphQL client that returns canned responses keyed by query name.
class FakeGQL
def initialize(responses)
@responses = responses
@calls = []
end
attr_reader :calls
def query(query_str, variables = {})
@calls << [query_str, variables]
# Match on the operation name (the line after `query `) so we can
# serve different mocks for SERVICES_LIST_QUERY,
# SERVICE_INSTANCE_QUERY, ENVIRONMENT_VARIABLES_QUERY.
op = query_str[/query\s+(\w+)/, 1]
response = @responses[op] || @responses[:default]
raise "no fake response for op=#{op.inspect}" unless response
response.respond_to?(:call) ? response.call(variables) : response
end
end
def services_list_response
{
"project" => {
"id" => Railway::PROJECT_ID,
"name" => "showcase",
"services" => {
"edges" => [
{ "node" => { "id" => "svc-aimock", "name" => "aimock" } },
{ "node" => { "id" => "svc-shell", "name" => "shell" } },
],
},
},
}
end
def env_vars_response_with_per_service_keys
{
"environment" => {
"id" => Railway::PRODUCTION_ENV_ID,
"name" => "production",
"variables" => {
"edges" => [
{ "node" => { "name" => "PORT", "serviceId" => "svc-aimock", "isSealed" => false } },
{ "node" => { "name" => "NODE_OPTIONS","serviceId" => "svc-aimock", "isSealed" => false } },
{ "node" => { "name" => "API_KEY", "serviceId" => "svc-shell", "isSealed" => true } },
],
},
},
}
end
def service_instance_response(image:, start_cmd: nil, domains: [],
healthcheck_path: nil, region: nil,
num_replicas: nil, restart_policy: nil)
{
"serviceInstance" => {
"id" => "inst-#{image[/sha256:[a-f0-9]+/] || 'tag'}",
"serviceId" => "svc-aimock",
"environmentId" => Railway::PRODUCTION_ENV_ID,
"startCommand" => start_cmd,
"healthcheckPath" => healthcheck_path,
"region" => region,
"numReplicas" => num_replicas,
"restartPolicyType" => restart_policy,
"source" => { "image" => image, "repo" => nil },
"latestDeployment" => { "id" => "dep-1", "status" => "SUCCESS" },
"domains" => {
"customDomains" => domains.map { |d| { "id" => "cd-#{d}", "domain" => d } },
"serviceDomains" => [],
},
},
}
end
def test_build_snapshot_uses_corrected_field_names_and_produces_pinned_entries
fake = FakeGQL.new(
"ProjectServices" => services_list_response,
"EnvVariables" => env_vars_response_with_per_service_keys,
"ServiceInstance" => lambda do |vars|
if vars[:serviceId] == "svc-aimock"
service_instance_response(
image: "ghcr.io/copilotkit/showcase-aimock@sha256:cafef00d",
start_cmd: "node /app/dist/cli.js",
domains: ["aimock.showcase.copilotkit.ai"],
)
else
service_instance_response(
image: "ghcr.io/copilotkit/showcase-shell@sha256:beef1234",
domains: [],
)
end
end,
)
cmd = Railway::SnapshotCommand.new(["--env", "production", "--dry-run"])
cmd.instance_variable_set(:@gql, fake)
snap = cmd.build_snapshot(Railway::PRODUCTION_ENV_ID)
assert_equal 2, snap["version"]
assert_equal 2, snap["services"].length
aimock = snap["services"].find { |s| s["name"] == "aimock" }
assert_equal "ghcr.io/copilotkit/showcase-aimock@sha256:cafef00d", aimock["image"]
assert_equal "sha256:cafef00d", aimock["digest"]
assert_equal "ghcr.io/copilotkit/showcase-aimock", aimock["image_tag"]
assert_equal "node /app/dist/cli.js", aimock["start_command"]
assert_equal ["aimock.showcase.copilotkit.ai"], aimock["custom_domains"]
assert_equal %w[NODE_OPTIONS PORT], aimock["env_keys"]
assert_equal "dep-1", aimock["latest_deployment_id"]
shell = snap["services"].find { |s| s["name"] == "shell" }
assert_equal ["API_KEY"], shell["env_keys"]
assert_equal [], shell["custom_domains"]
end
def test_lint_prod_marks_mutable_tag_when_image_is_unpinned
fake = FakeGQL.new(
"ProjectServices" => services_list_response,
"EnvVariables" => env_vars_response_with_per_service_keys,
"ServiceInstance" => lambda do |vars|
# Both return an unpinned :latest tag.
service_instance_response(
image: "ghcr.io/copilotkit/#{vars[:serviceId]}:latest",
)
end,
)
snap_cmd = Railway::SnapshotCommand.new(["--env", "production", "--dry-run"])
snap_cmd.instance_variable_set(:@gql, fake)
snap = snap_cmd.build_snapshot(Railway::PRODUCTION_ENV_ID)
# All services are mutable-tag because none have @sha256:.
snap["services"].each do |svc|
refute svc["image"].include?("@sha256:"), "expected mutable tag for #{svc['name']}"
assert_nil svc["digest"], "expected nil digest for #{svc['name']}"
end
end
def test_build_snapshot_queries_use_only_supported_fields
# Regression guard: the previous version of this tool referenced
# `Project.domains` and `Service.serviceInstances`, both of which do
# NOT exist in Railway's public GraphQL schema. Ensure those tokens
# never reappear in the query constants.
sources = [
Railway::SERVICES_LIST_QUERY,
Railway::SERVICE_INSTANCE_QUERY,
Railway::ENVIRONMENT_VARIABLES_QUERY,
]
sources.each do |q|
refute_match(/project\s*\([^)]*\)\s*\{[^}]*\bdomains\b/m, q,
"Project has no `domains` field — use serviceInstance.domains or the top-level domains query.")
refute_match(/\bserviceInstances\b/m, q,
"Service has no `serviceInstances` field — use serviceInstance(serviceId, environmentId) directly.")
end
end
def test_redeploy_mutation_uses_serviceInstanceRedeploy_not_serviceInstanceDeployV2_with_image
# serviceInstanceDeployV2 has signature (commitSha, environmentId, serviceId).
# It does NOT accept an `image` argument. Pinning must go through
# serviceInstanceUpdate (source.image) + serviceInstanceRedeploy.
refute_match(/serviceInstanceDeployV2\s*\([^)]*\bimage\b/m,
Railway::RestoreCommand::UPDATE_IMAGE_MUTATION,
"Restore must not call serviceInstanceDeployV2 with image arg.")
assert_match(/serviceInstanceUpdate\s*\(/, Railway::RestoreCommand::UPDATE_IMAGE_MUTATION)
assert_match(/source:\s*\{\s*image:/, Railway::RestoreCommand::UPDATE_IMAGE_MUTATION)
assert_match(/serviceInstanceRedeploy\s*\(/, Railway::RestoreCommand::REDEPLOY_MUTATION)
end
def test_snapshot_v2_captures_healthcheck_region_replicas_restart_policy
# P6 parity-matrix needs these four fields on every snapshot service.
# Snapshot schema is v2; SnapshotCommand#build_snapshot must map them
# from the new SERVICE_INSTANCE_QUERY selection set.
fake = FakeGQL.new(
"ProjectServices" => services_list_response,
"EnvVariables" => env_vars_response_with_per_service_keys,
"ServiceInstance" => lambda do |vars|
if vars[:serviceId] == "svc-aimock"
service_instance_response(
image: "ghcr.io/copilotkit/showcase-aimock@sha256:cafef00d",
start_cmd: "node /app/dist/cli.js",
domains: ["aimock.showcase.copilotkit.ai"],
healthcheck_path: "/healthz",
region: "us-west2",
num_replicas: 2,
restart_policy: "ON_FAILURE",
)
else
service_instance_response(
image: "ghcr.io/copilotkit/showcase-shell@sha256:beef1234",
healthcheck_path: "/health",
region: "us-east1",
num_replicas: 1,
restart_policy: "ALWAYS",
)
end
end,
)
cmd = Railway::SnapshotCommand.new(["--env", "production", "--dry-run"])
cmd.instance_variable_set(:@gql, fake)
snap = cmd.build_snapshot(Railway::PRODUCTION_ENV_ID)
aimock = snap["services"].find { |s| s["name"] == "aimock" }
assert_equal "/healthz", aimock["healthcheck_path"]
assert_equal "us-west2", aimock["region"]
assert_equal 2, aimock["replicas"]
assert_equal "ON_FAILURE", aimock["restart_policy"]
shell = snap["services"].find { |s| s["name"] == "shell" }
assert_equal "/health", shell["healthcheck_path"]
assert_equal "us-east1", shell["region"]
assert_equal 1, shell["replicas"]
assert_equal "ALWAYS", shell["restart_policy"]
end
def test_snapshot_io_read_accepts_v1_and_v2_snapshots
# rollback-commit replays historical snapshots from arbitrary git SHAs,
# so SnapshotIO.read MUST stay backwards-compat with v1 even though
# all NEW snapshots are written as v2.
require "tempfile"
[1, 2].each do |ver|
Tempfile.create(["snap-v#{ver}", ".yaml"]) do |f|
f.write(YAML.dump("version" => ver, "services" => []))
f.flush
snap = Railway::SnapshotIO.read(f.path)
assert_equal ver, snap["version"]
end
end
end
def test_deploymentRollback_mutation_has_no_selection_set_because_it_returns_boolean
# deploymentRollback's return type is Boolean (scalar). GraphQL
# forbids a selection set on scalar fields.
refute_match(/deploymentRollback\s*\([^)]*\)\s*\{/m,
Railway::RollbackCommand::ROLLBACK_MUTATION,
"deploymentRollback returns Boolean; no selection set allowed.")
end
end