/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/*
 * Tests that we create a new, migrated profile when running out of a package.
 */

add_task(async () => {
  // Create a default profile to migrate from.
  let dir = makeRandomProfileDir("default");
  let profileData = {
    options: {
      startWithLastProfile: true,
    },
    profiles: [
      {
        name: "default",
        path: dir.leafName,
        default: true,
      },
    ],
  };
  writeProfilesIni(profileData);
  checkProfileService(profileData);

  // Create a lockfile to make it look like this profile has been used recently.
  let lockFile = dir.clone();
  lockFile.append("parent.lock");
  // Don't fail if the file already exists.
  try {
    lockFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o777);
  } catch (_e) {}
  await IOUtils.touch(lockFile.path);

  // We need to have a compatibility.ini present for the package logic's
  // downgrade check to pass.
  writeCompatibilityIni(dir);

  // Switch on simulated package mode and try to select a profile.
  simulateWinPackageEnvironment();

  let { rootDir, profile, didCreate } = selectStartupProfile();
  checkStartupReason("firstrun-migrated-default");

  Assert.ok(!didCreate, "Should not have created a new profile yet.");
  Assert.ok(profile, "Should have selected an existing profile.");
  Assert.ok(rootDir.equals(dir), "Should have selected our root dir.");
  // The localDir will likely be different, but it doesn't really matter.
});
