function assert(bool, msg) {
  if (!bool) throw new Error("TEST FAILED: " + msg)
}

function assert_equal(a, b) {
  assert(a.toString() == b.toString(), "Failed to match: <"+a+"> <"+b+">")
}

assert(name_matches('Brett603', 'Brett603'), 'exact match')
assert(name_matches('Rifkin', 'Adam Rifkin'), 'lastname')
assert(name_matches('Rifkin', 'Adam.Rifkin'), 'dotted address')
assert(name_matches('Rifkin', 'Adam_Rifkin'), 'underscore')
assert(name_matches('Rifkin', 'Adam+Rifkin69@gmail.com'), 'numeric suffix in email addr')
assert(name_matches('Nara', 'cbe_nara'), 'underscore in username')
assert(name_matches('Nara', 'cbenara'), 'suffix match')
assert(!name_matches('Nara', 'ara'), 'suffix match when not found')
assert(name_matches('jonm', 'jonm839'), 'prefix match with digits')
assert(!name_matches('Brett603', 'Brett999'), 'not ignoring digit mismatch')
assert(name_matches('Brett603', 'B.Brett603'), 'splitting on punctuation')

_example_account = {
  fn:         "Adam Rifkin",
  photo:      "http://a3.twimg.com/profile_images/54927781/panda_casual_normal.jpg",
  url:        "http://twitter.com/ifindkarma",
  account_id: 1688,
  nickname:   "ifindkarma"
}
assert(friend_matches_search('Rifkin')(_example_account), 'Rifkin')
assert(!friend_matches_search('Rohit')(_example_account), 'Rohit')
assert(friend_matches_search('ifindkarma')(_example_account), 'ifindkarma')
assert(friend_matches_search('findkarma')(_example_account), 'findkarma') /* now a suffix match */

// This example is kind of fake; I'm guessing what some of the fields
// will look like.
_linkedin_example = {
  fn:            'Kragen Sitaker',
  'given-name':  'Kragen',
  'family-name': 'Sitaker',
  title:         'Programmer',
  url:           'http://www.linkedin.com/pub/kragen-sitaker/0/56/116',
  account_id:    114210
}
assert(friend_matches_search('kragen')(_linkedin_example), 'kragen')
assert(!friend_matches_search('reagan')(_linkedin_example), 'reagan')

// This example is from Facebook.
_example_luis = {
  fn:            "Luis Villa",
  'given-name':  "Luis",
  'family-name': "Villa",
  photo:         "http://profile.ak.fbcdn.net/v225/1461/21/q121954_6870.jpg",
  url:           "http://www.facebook.com/lvilla",
  bday:          "1978-2-26",
  nickname:      "lvilla",
  account_id:    121954,
  websites:      ["http://tieguy.org/"]
}
assert(friend_matches_search('luis')(_example_luis), 'luis')
assert(friend_matches_search('tieguy')(_example_luis), 'tieguy')
assert(friend_matches_search('ieguy')(_example_luis), 'ieguy') /* suffix match ok when websites split by domain */
assert(friend_matches_search('tiegu')(_example_luis), 'tiegu')
assert(!friend_matches_search('iegu')(_example_luis), 'iegu')
assert(!friend_matches_search('http')(_example_luis), 'http')
assert(friend_matches_search('org')(_example_luis), 'org') /* will match a domain name part. Should TLDs count? */

// From Twitter.
_example_rohit = {
    fn: "rohitkhare",
    'given-name': "rohitkhare",
    'family-name': "rohitkhare",
    photo: "http://a3.twimg.com/profile_images/53307499/180px-Rohit-sq_normal.jpg",
    socnet: "twitter",
    favicon: "http://twitter.com/favicon.ico",
    url: "http://twitter.com/rohitkhare",
    account_id: 14527804,
    nickname: "rohitkhare",
    lastTweet: {
        id: 6065559932,
        created_at: "Thu Nov 26 00:00:38 +0000 2009",
        text: "I'm #thankfulfor all of the wonderful birthday greetings from around the... what? \"Around the graph\"? Blessed to know you all!"
    }
}
assert(friend_matches_search('rohit')(_example_rohit), 'rohit')
assert(friend_matches_search('rohit ')(_example_rohit), 'rohit with space')

_example_mpfrank = {
    fn: "Michael Patrick Frank",
    "given-name": "Michael", 
    "family-name": "Frank", 
    photo: "http://profile.ak.fbcdn.net/v226/547/75/q829833185_3377.jpg",
    socnet: "facebook",
    favicon: "http://www.facebook.com/favicon.ico",
    url: "http://www.facebook.com/m.p.frank",
    bday: "1969-10-31",
    nickname: "m.p.frank",
    account_id: 829833185,
    profile_blurb: "ta.gd/MFrank\nFacebook.com/M.P.Frank\nTwitter.com/MikePFrank\nmichael.patrick.frank@gmail.com \n(includes google voice chat & sometimes video chat)\nmikepfrank@yahoo.com\nGoogle Voice: 413-842-6670",
    lastTweet: {
        text: "wonders if any of the global warming deniers on Facebook honestly buy the BS smear campaign against modern science that is waged every day by the likes of Fox News and Rush Limbaugh, and all the dime-a-dozen crap websites put up by paranoid conspiracy theorists.",
        created_at: "Mon Nov 23 2009 11:38:06 GMT-0300 (ART)",
        id: "180109319004"
    }
}

assert(friend_matches_search('michael frank')(_example_mpfrank), 'michael frank')
