 
 
 # Simulate previewing a comment on the server side
-#
-# URL we're commenting on: https://fanfare.metafilter.com/2212/Forever-Hitler-on-the-Half-Shell
 exports['simulate server-side preview of a comment'] = (test) ->
   sessionStorage = makeSessionStorage()
 
-  sr = saveRestore.makeSaveRestore
-    sessionStorage: sessionStorage
-    location: makeLocation
-      hostname: 'fanfare.metafilter.com'
-      pathname: '/2212/Forever-Hitler-on-the-Half-Shell'
-
-  # Simulate clicking the preview button
   mdCommentText = "I really liked this show and/or movie!"
-  linkId = 2212
-  sr.saveMarkdownForPreview
+  simulateSave
+    sessionStorage: sessionStorage
+    site: 'fanFare'
+    type: 'comment'
     mdCommentText: mdCommentText
-    mdExtendedText: null
-    linkId: linkId
-    formSubmitUrl: '/contribute/post_comment_preview.mefi#commentpreview'
 
   test.strictEqual(sessionStorage.length, 1, "Comment was saved")
 
-
-  # Load the markdown on the preview page
-  sr = saveRestore.makeSaveRestore
-    sessionStorage: sessionStorage
-    location: makeLocation
-      hostname: 'fanfare.metafilter.com'
-      pathname: '/contribute/post_comment_preview.mefi'
-      hash:     '#commentpreview'
-
-
-  savedDataJson = sr.restoreMarkdown 
-    linkId: linkId
-    formSubmitUrl: 'post_comment_preview.mefi#commentpreview'
-
+  loadResults = simulateLoad
+    sessionStorage: sessionStorage
+    site: 'fanFare'
+    type: 'comment'
+  savedDataJson = loadResults.savedDataJson
+  sr            = loadResults.sr
+  storageKey    = loadResults.storageKey
 
   test.strictEqual(mdCommentText, savedDataJson.comment, "comment was restored correctly")
   test.strictEqual(null,          savedDataJson.extended, "extended was restored correctly")
 
-  storageKey = sr.getSessionStorageKey linkId, 'post_comment_preview.mefi#commentpreview'
-  keyValue = sessionStorage.getItem(storageKey)
-  test.strictEqual(keyValue, null, "Data was removed from session storage")
+  storedItem = sessionStorage.getItem(storageKey)
+  test.strictEqual(storedItem, null, "Data was removed from session storage")
   test.strictEqual(sessionStorage.length, 0, "Nothing left in session storage")  
-
-  test.done()
-
-exports['categorize urls'] = (test) ->
-  testCases = [
-    # The new post page isn't a preview page (its preview is at a separate URL)
-    url: 'https://www.metafilter.com/contribute/post_good.mefi?pid=205463'
-    isPostPreview: false
-    isCommentPreview: false
-  ,
-    # form submit URL for the MeFi FPP page
-    url: 'post_preview.mefi'
-    isPostPreview: true
-    isCommentPreview: false
-  , 
-    # MeFi FPP preview with a hash. I haven't seen one of these
-    # in the wild, but we should be prepared for it.
-    url: 'post_preview.mefi#foobar'
-    isPostPreview: true
-    isCommentPreview: false
-  ,
-
-    # The FanFare new post page. Not a preview page.
-    url: 'https://fanfare.metafilter.com/contribute/post.cfm'
-    isPostPreview: false
-    isCommentPreview: false
-  ,
-
-    # FanFare post preview page. Its URL ends in .cfm,
-    # wereas the corresponding page on the blue ends in
-    # .mefi
-    url: 'post_preview.cfm'    
-    isPostPreview: true
-    isCommentPreview: false
-  ,
-
-    # Again, match up the .cfm URL with a hash.
-    # Haven't seen one in the wild, but be prepared
-    # for one to exist.
-    url: 'post_preview.cfm#foobar'    
-    isPostPreview: true
-    isCommentPreview: false
-  ,
-
-    # MeFi comment preview page
-    url: '/contribute/post_comment_preview.mefi#commentpreview'
-    isPostPreview: false
-    isCommentPreview: true
-  ,
-
-    # MeFi comment preview w/o the hash.
-    # Haven't seen one in the wild.
-    url: '/contribute/post_comment_preview.mefi'
-    isPostPreview: false
-    isCommentPreview: true
-  ]
-
-  testCases.forEach (testCase) ->
-    test.strictEqual(testCase.isPostPreview,    
-                    saveRestore.isPostPreviewPage(testCase.url), 
-                    testCase.url)
-    test.strictEqual(testCase.isCommentPreview, 
-                    saveRestore.isCommentPreviewPage(testCase.url), 
-                    testCase.url)
 
   test.done()
 


  test.done()



# Simulate doing one preview of a MeFi FPP

# Create a mock window.location, with
# sensible MeFi defaults.
makeLocation = (location) ->
  location ?= {}

  # Abort if we accidentally used a camelcase name.
  ['hostName', 'pathName'].forEach (key) ->
    if location.hasOwnProperty(key) 
      throw new Error("makeLocation: Invalid location key: #{key}")

  location['protocol'] ?= "https:"
  location['hostname'] ?= "www.metafilter.com"
  location['pathname'] ?= "/"
  if !/^\//.test(location['pathname'])
    throw new Error("makeLocation: pathname needs to start with a slash: " +
      location['pathname'])
  if /#/.test(location['pathname'])
    throw new Error("makeLocation: Get that hash out of the pathname")

  location['hash']     ?= ''
  if !/(^$)|^#/.test(location['hash'])
    throw new Error("makeLocation: Hash needs to be empty string or start with #: " +
      location['hash'])

  location['href']     ?= location.protocol + location.hostname + location.pathname
  return location

exports['test sessionStorage mock'] = (test) ->
