env CUE_REGISTRY=foo.com/bar=myregistry.example/foo,other.org=none

exec cue mod resolve somethingelse.bar.com/abc@v1.2.3
cmp stdout want-resolve0

exec cue mod resolve somethingelse.bar.com/abc
cmp stdout want-resolve1

exec cue mod resolve foo.com/bar/baz@v0.1.2
cmp stdout want-resolve2

! exec cue mod resolve other.org/a/b
cmp stderr want-resolve3

env CUE_REGISTRY=file:registry-config.cue

exec cue mod resolve a.com/b
cmp stdout want-resolve4

exec cue mod resolve a.com/foo/bar/baz@v1.2.3
cmp stdout want-resolve5

exec cue mod resolve a.com/foo/bar/baz@v1.2.3
cmp stdout want-resolve5

exec cue mod resolve stripped.org/bar/baz/p@v0.1.2
cmp stdout want-resolve6

exec cue mod resolve
cmp stdout want-resolve7

exec cue mod resolve .
cmp stdout want-resolve7

# Test --deps flag with no dependencies (should produce no output)
exec cue mod resolve --deps
! stdout .

# Now test with a module that has dependencies
cd has-deps
env CUE_REGISTRY=file:registry-config.cue

exec cue mod resolve --deps
cmp stdout want-resolve-all

# Test that --deps with arguments produces an error
! exec cue mod resolve --deps a.com/b
stderr 'cannot specify module arguments with --deps flag'

# Test that regular resolve still works
exec cue mod resolve
cmp stdout want-resolve-current

# Test that --deps continues on errors
cd ../has-deps-with-errors
env CUE_REGISTRY=file:registry-config.cue

! exec cue mod resolve --deps
cmp stdout want-resolve-deps-with-errors-stdout
cmp stderr want-resolve-deps-with-errors-stderr

-- cue.mod/module.cue --
module: "a.com/foo/bar"
language: version: "v0.9.2"
-- has-deps/cue.mod/module.cue --
module: "example.com/mymodule"
language: version: "v0.9.2"
deps: {
	"a.com/foo/bar@v1": v: "v1.2.3"
	"stripped.org/bar/baz@v0": v: "v0.1.2"
	"somethingelse.bar.com/abc@v1": v: "v1.2.3"
}
-- has-deps/registry-config.cue --
moduleRegistries: {
	"a.com": {
		registry: "r1.example/a/b+insecure"
	}
	"a.com/foo/bar": {
		registry: "r2.example/xxx"
		pathEncoding: "hashAsRepo"
		prefixForTags: "cue-"
	}
	"stripped.org/bar": {
		registry: "r3.example/repo"
		stripPrefix: true
	}
	"badmodules.org": {
		registry: "none"
	}
}
-- registry-config.cue --
moduleRegistries: {
	"a.com": {
		registry: "r1.example/a/b+insecure"
	}
	"a.com/foo/bar": {
		registry: "r2.example/xxx"
		pathEncoding: "hashAsRepo"
		prefixForTags: "cue-"
	}
	"stripped.org/bar": {
		registry: "r3.example/repo"
		stripPrefix: true
	}
	"badmodules.org": {
		registry: "none"
	}
}
-- want-resolve0 --
registry.cue.works/somethingelse.bar.com/abc:v1.2.3
-- want-resolve1 --
registry.cue.works/somethingelse.bar.com/abc
-- want-resolve2 --
myregistry.example/foo/foo.com/bar/baz:v0.1.2
-- want-resolve3 --
no registry found for module "other.org/a/b"
-- want-resolve4 --
r1.example/a/b/a.com/b
-- want-resolve5 --
r2.example/xxx/0764601cd4e1d9d84efc2f4ddc23191e599fa9826f46b16b84269b6fe3679184:cue-v1.2.3
-- want-resolve6 --
r3.example/repo/baz/p:v0.1.2
-- want-resolve7 --
r2.example/xxx/7c9813cd44d6d9348eb223e71d608e491805730547b8ff6f4b1ccb964b913148
-- has-deps/want-resolve-all --
a.com/foo/bar@v1: r2.example/xxx/7c9813cd44d6d9348eb223e71d608e491805730547b8ff6f4b1ccb964b913148:cue-v1.2.3
somethingelse.bar.com/abc@v1: registry.cue.works/somethingelse.bar.com/abc:v1.2.3
stripped.org/bar/baz@v0: r3.example/repo/baz:v0.1.2
-- has-deps/want-resolve-current --
registry.cue.works/example.com/mymodule
-- has-deps-with-errors/cue.mod/module.cue --
module: "example.com/test"
language: version: "v0.9.2"
deps: {
	"a.com/foo/bar@v1": v: "v1.2.3"
	"badmodules.org/fail@v0": v: "v0.1.0"
	"somethingelse.bar.com/abc@v1": v: "v1.2.3"
}
-- has-deps-with-errors/registry-config.cue --
moduleRegistries: {
	"a.com": {
		registry: "r1.example/a/b+insecure"
	}
	"a.com/foo/bar": {
		registry: "r2.example/xxx"
		pathEncoding: "hashAsRepo"
		prefixForTags: "cue-"
	}
	"badmodules.org": {
		registry: "none"
	}
}
-- has-deps-with-errors/want-resolve-deps-with-errors-stdout --
a.com/foo/bar@v1: r2.example/xxx/7c9813cd44d6d9348eb223e71d608e491805730547b8ff6f4b1ccb964b913148:cue-v1.2.3
somethingelse.bar.com/abc@v1: registry.cue.works/somethingelse.bar.com/abc:v1.2.3
-- has-deps-with-errors/want-resolve-deps-with-errors-stderr --
badmodules.org/fail@v0: no registry found for module "badmodules.org/fail@v0"
