var fs = require('fs');
var ga = require('../src/gitlet-api');
var testUtil = require('./test-util');

describe('checkout', function() {
  beforeEach(testUtil.createEmptyRepo);
  beforeEach(testUtil.pinDate);
  afterEach(testUtil.unpinDate);

  it('should throw if not in repo', function() {
    expect(function() { ga.checkout(); })
      .toThrow("fatal: Not a gitlet repository (or any of the parent directories): .gitlet");
  });

  it('should throw if pass ref that does not resolve to a hash', function() {
    ga.init();
    expect(function() { ga.checkout("woo"); })
      .toThrow("error: pathspec woo did not match any file(s) known to gitlet.");
  });

  it('should throw if passed ref points to blob', function() {
    testUtil.createStandardFileStructure();
    ga.init();
    ga.add("1a/filea");
    ga.commit({ m: "first" });
    expect(function() { ga.checkout("5ceba65") })
      .toThrow("fatal: reference is not a tree: 5ceba65")
  });

  it('should throw if passed ref points to tree', function() {
    testUtil.createStandardFileStructure();
    ga.init();
    ga.add("1a/filea");
    ga.commit({ m: "first" });
    expect(function() { ga.checkout("17653b6d") })
      .toThrow("fatal: reference is not a tree: 17653b6d")
  });

  it('should throw if file has changes w/o common orig content with c/o branch', function() {
    testUtil.createStandardFileStructure();
    ga.init();

    ga.add("1a/filea");
    ga.commit({ m: "first" });

    ga.branch("other");

    fs.writeFileSync("1a/filea", "fileachange1");
    ga.add("1a/filea");
    ga.commit({ m: "second" });

    fs.writeFileSync("1a/filea", "fileachange2");

    expect(function() { ga.checkout("other"); })
      .toThrow("error: Aborting. Your local changes to these files would be overwritten:\n" +
	             "1a/filea\n");
  });

  // it('should remove commited files in previous working copy', function() {
  //   ga.init();
  it('should list all files that would be overwritten when throwing', function() {
    testUtil.createStandardFileStructure();
    ga.init();

    ga.add("1a/filea");
    ga.add("1b/fileb");
    ga.add("1b/2a/filec");
    ga.commit({ m: "first" });

    ga.branch("other");

    fs.writeFileSync("1a/filea", "fileachange1");
    fs.writeFileSync("1b/fileb", "fileachange1");
    fs.writeFileSync("1b/2a/filec", "fileachange1");
    ga.add("1a/filea");
    ga.add("1b/fileb");
    ga.add("1b/2a/filec");
    ga.commit({ m: "second" });

    fs.writeFileSync("1a/filea", "fileachange2");
    fs.writeFileSync("1b/fileb", "fileachange2");
    fs.writeFileSync("1b/2a/filec", "fileachange2");

    expect(function() { ga.checkout("other"); })
      .toThrow("error: Aborting. Your local changes to these files would be overwritten:\n" +
	             "1a/filea\n1b/fileb\n1b/2a/filec\n");
  });

  it('should not throw if file has changes w/ common orig content w/ c/o branch', function() {
    testUtil.createStandardFileStructure();
    ga.init();

    ga.add("1a/filea");
    ga.commit({ m: "first" });

    ga.branch("other");
    fs.writeFileSync("1a/filea", "fileachange2");

    ga.checkout("other"); // does not throw
  });
  //   testUtil.createStandardFileStructure();

  //   ga.add("1a/filea");
  //   ga.commit({ m: "first" });
  //   ga.branch("other");

  //   ga.add("1b/fileb");
  //   ga.commit({ m: "second" });

  //   ga.checkout("other");
  //   expect(fs.existsSync("1b/fileb")).toEqual(false);
  // });

  // it('should allow a commit hash to be passed', function() {

  // });


  // it('should point head at passed ref', function() {

  // });

  // it('should point head at passed commit hash', function() {

  // });

  // it('should warn that leaving detached head behind if checkout from det head', function() {

  // });

  // it('should abort on staged changed file w dif content from checkout(ref)', function() {

  // });

  // it('should abort on unstaged changed file w dif content from checkout(ref)', function() {

  // });

  // it('should checkout from both tree ref and commit ref', function() {

  // });

});

@@ -1819,104 +1819,8 @@
 );%0A%0A
-  // it('should remove commited files in previous working copy', function() %7B%0A  //   ga.init();%0A
   it
@@ -3043,20 +3043,145 @@
 w%0A  %7D);%0A
+%0A
   
-//
+describe('successful checkout', function() %7B%0A    it('should remove committed files in previous working copy', function() %7B%0A   
    testU
@@ -3214,22 +3214,38 @@
 cture();
+%0A      ga.init();
 %0A%0A  
-//
+ 
    ga.ad
@@ -3257,29 +3257,28 @@
 /filea%22);%0A  
-//
    
+ 
 ga.commit(%7B 
@@ -3290,26 +3290,25 @@
 irst%22 %7D);%0A  
-//
+ 
    ga.branch
@@ -3317,26 +3317,25 @@
 other%22);%0A%0A  
+ 
-//
    ga.add(%221
@@ -3347,18 +3347,17 @@
 eb%22);%0A  
-//
+ 
    ga.co
@@ -3382,18 +3382,17 @@
  %7D);%0A%0A  
-//
+ 
    ga.ch
@@ -3410,18 +3410,17 @@
 er%22);%0A  
-//
+ 
    expec
@@ -3464,16 +3464,117 @@
 false);%0A
+    %7D);%0A  %7D);%0A%0A  // it('should keep local changes to files w common original content', function() %7B%0A%0A
   // %7D);

Test checkout removes files not in passed tree hash