# Test what happens when a replaceWith field points to a version
# that's also required by another dependency at a different version.

exec cue mod tidy

# The resulting dependencies end up with the larger
# of the two versions and is present in the regular
# dependencies. The version in the replacement is
# canonicalized to the major version only.
cmp cue.mod/module.cue want-module
cmp cue.mod/local-module.cue want-local

exec cue export .
cmp stdout want-stdout

exec cue mod tidy --check

-- want-module --
module: "main.org@v0"
language: {
	version: "v0.17.0"
}
deps: {
	"example.com/dep@v0": {
		v: "v0.1.0"
	}
	"example.com/orig@v0": {
		v: "v0.1.0"
	}
	"example.com/target@v0": {
		v: "v0.0.2"
	}
}
-- want-local --
deps: {
	"example.com/dep@v0": {}
	"example.com/orig@v0": {
		replaceWith: "example.com/target@v0"
	}
	"example.com/target@v0": {}
}
-- want-stdout --
{
    "fromOrig": "target v0.0.2",
    "fromDep": "target v0.0.2"
}
-- cue.mod/module.cue --
module: "main.org@v0"
language: version: "v0.17.0"
-- cue.mod/local-module.cue --
deps: "example.com/orig@v0": {
	v:       "v0.1.0"
	replaceWith: "example.com/target@v0.0.1"
}
-- main.cue --
package main

import (
	"example.com/orig@v0:lib"
	"example.com/dep@v0:dep"
)

fromOrig: lib.value
fromDep:  dep.value
-- _registry/example.com_orig_v0.1.0/cue.mod/module.cue --
module: "example.com/orig@v0"
language: version: "v0.9.0"
-- _registry/example.com_orig_v0.1.0/lib.cue --
package lib

value: "from orig (should not see this)"
-- _registry/example.com_target_v0.0.1/cue.mod/module.cue --
module: "example.com/target@v0"
language: version: "v0.9.0"
-- _registry/example.com_target_v0.0.1/lib.cue --
package lib

value: "target v0.0.1"
-- _registry/example.com_target_v0.0.2/cue.mod/module.cue --
module: "example.com/target@v0"
language: version: "v0.9.0"
-- _registry/example.com_target_v0.0.2/lib.cue --
package lib

value: "target v0.0.2"
-- _registry/example.com_dep_v0.1.0/cue.mod/module.cue --
module: "example.com/dep@v0"
language: version: "v0.9.0"
deps: "example.com/target@v0": {
	v: "v0.0.2"
}
-- _registry/example.com_dep_v0.1.0/dep.cue --
package dep

import "example.com/target@v0:lib"

value: lib.value
